-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathCraftingPanel.js
203 lines (161 loc) · 5.81 KB
/
CraftingPanel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Created by Jerome on 20-11-17.
// */
import BigButton from './BigButton'
import Client from './Client'
import Engine from './Engine'
import IngredientSlot from './IngredientSlot'
import Panel from './Panel'
import UI from './UI'
import Utils from '../shared/Utils'
import abilitiesData from '../assets/data/abilities'
import itemsData from '../assets/data/items.json'
function CraftingPanel(x,y,width,height,title){
Panel.call(this,x,y,width,height,title);
this.buttons = [];
this.addInterface();
this.lastCraft = Date.now();
}
CraftingPanel.prototype = Object.create(Panel.prototype);
CraftingPanel.prototype.constructor = CraftingPanel;
CraftingPanel.prototype.isFinancial = function(){
return !Engine.currentBuiling.isOwned();
};
CraftingPanel.prototype.addInterface = function(){
this.craftSlot = new IngredientSlot(this.x+115,this.y+30,320,80,true); // true = show price
this.craftSlot.hide();
this.noitem = this.addText(this.width/2,80,'Select a recipe to begin',Utils.colors.white,16);
this.noitem.setOrigin(0.5);
this.ingredientSlots = [];
var w = 250;
for(var i = 0; i < 4; i++){
var x = this.x + 15 + +(i%2)*(w+10);
var y = this.y + 150 + Math.floor(i/2)*90;
var slot = new IngredientSlot(x,y,w,80);
slot.hide();
this.ingredientSlots.push(slot)
}
var count = UI.scene.add.text(this.x+this.width/2,this.y+130, '1', { font: '16px belwe', fill: '#ffffff', stroke: '#000000', strokeThickness: 3 });
count.setOrigin(0.5,0.5);
count.setVisible(false);
count.setDepth(2);
count.setScrollFactor(0);
this.content.push(count);
this.countText = count;
this.okBtn = new BigButton(this.x + this.width/2,this.y + 350,'Craft',this.requestCraft.bind(this));
this.okBtn.hide();
this.abilityneeded = this.addText(this.width/2,this.y + 380,'',Utils.colors.red,16);
this.abilityneeded.setOrigin(0.5);
// this.addButton(x+92,y+13,'green','ok',this.requestCraft.bind(this),'Craft');
this.minusBtn = this.addButton(count.x - this.x - 35,count.y - this.y - 10, 'blue','minus',this.decreaseAmount.bind(this),'Decrease').btn;
this.plusBtn = this.addButton(count.x - this.x + 13,count.y - this.y - 10, 'blue','plus',this.increaseAmount.bind(this),'Increase').btn;
this.craftItem = {
id: -1,
count: 0,
price: 0,
recipe: null
};
};
CraftingPanel.prototype.display = function(){
Panel.prototype.display.call(this);
this.displayInterface();
};
CraftingPanel.prototype.displayInterface = function(){
this.buttons.forEach(function(b){
b.btn.disable();
});
this.hideButtons();
this.noitem.setVisible(true);
};
CraftingPanel.prototype.getPrice = function(){
return Engine.currentBuiling.getPrice(this.craftItem.id, 'sell');
};
CraftingPanel.prototype.setUp = function(itemID){
var data = itemsData[itemID];
this.craftItem.id = itemID;
this.craftItem.count = 1;
this.craftItem.recipe = data.recipe;
this.craftItem.price = this.getPrice();
this.craftSlot.display();
this.craftSlot.setUp(itemID,-1);
this.noitem.setVisible(false);
this.displayButtons();
this.updateIngredients();
this.countText.setVisible(true);
var output = (itemsData[this.craftItem.id].output || 1);
this.countText.setText(this.craftItem.count*output);
this.okBtn.display();
this.manageButtons();
if(data.ability) this.abilityneeded.setText(abilitiesData[data.ability].name+' ability needed');
var lackingAbility = (data.ability && !Engine.player.hasAbility(data.ability));
this.abilityneeded.setVisible(lackingAbility);
};
CraftingPanel.prototype.updateIngredients = function(){
if(this.craftItem.id == -1) return;
this.craftSlot.setUp(this.craftItem.id,-1);
var data = itemsData[this.craftItem.id];
this.ingredientSlots.forEach(function(slot){
slot.hide();
});
var i = 0;
for(var ing in data.recipe){
var slot = this.ingredientSlots[i++];
slot.display();
slot.setUp(ing,data.recipe[ing]*this.craftItem.count);
}
this.craftItem.price = this.craftItem.count*this.getPrice();
this.manageButtons();
};
CraftingPanel.prototype.manageButtons = function(){
if(this.craftItem.count == 1){
this.minusBtn.disable();
}else{
this.minusBtn.enable();
}
if(this.craftItem.count == 999){
this.plusBtn.disable();
}else{
this.plusBtn.enable();
}
if(Engine.player.canCraft(this.craftItem.id, this.craftItem.count)
&& (!this.isFinancial() || Engine.player.gold >= this.craftItem.price)){
this.okBtn.enable();
}else{
this.okBtn.disable();
}
this.buttons.last().btn.enable();
};
CraftingPanel.prototype.increaseAmount = function(){
this.changeAmount(1);
};
CraftingPanel.prototype.decreaseAmount = function(){
this.changeAmount(-1);
};
CraftingPanel.prototype.changeAmount = function(inc){
var output = (itemsData[this.craftItem.id].output || 1);
this.craftItem.count = Utils.clamp(this.craftItem.count+inc,1,999);
this.craftItem.price = this.craftItem.count*this.getPrice();
this.countText.setText(this.craftItem.count*output);
this.updateIngredients();
this.manageButtons();
};
CraftingPanel.prototype.hide = function(){
Panel.prototype.hide.call(this);
this.craftSlot.hide();
this.ingredientSlots.forEach(function(slot){
slot.hide();
});
this.okBtn.hide();
this.reset();
};
CraftingPanel.prototype.reset = function(){
this.craftItem.id = -1;
this.craftItem.count = 0;
this.craftItem.recipe = null;
};
CraftingPanel.prototype.requestCraft = function(){
if(Date.now() - this.lastCraft < 200) return;
Client.sendCraft(this.craftItem.id,this.craftItem.count);
this.lastCraft = Date.now();
};
export default CraftingPanel