-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtech-tree.js
306 lines (265 loc) · 9.21 KB
/
tech-tree.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
const res = [`food`, `wood`, `stone`, `gold`, `idea`, `culture`, `morale`];
window.p1TechBtns = Array.from(
document.querySelectorAll(`#p1TechTree .tech-btn`)
);
window.p2TechBtns = Array.from(
document.querySelectorAll(`#p2TechTree .tech-btn`)
);
window.p3TechBtns = Array.from(
document.querySelectorAll(`#p3TechTree .tech-btn`)
);
window.p1TechRes = Array.from(
document.querySelectorAll(`#p1TechTree > .tech-resource > .res`)
);
window.p2TechRes = Array.from(
document.querySelectorAll(`#p2TechTree > .tech-resource > .res`)
);
window.p3TechRes = Array.from(
document.querySelectorAll(`#p3TechTree > .tech-resource > .res`)
);
const resBg = Array.from(document.querySelectorAll(`.res-bg`));
let skillsType = Array.from(
document.querySelectorAll(`#p1TechTree .tech-info > h6`)
);
skillsType = skillsType.map((el) => el.textContent);
const skillConfirmContainer = document.getElementById(`skillConfirmContainer`);
const confirmAdvance = document.getElementById(`confirmAdvance`);
const cancelAdvance = document.getElementById(`cancelAdvance`);
const p1Skills = p1TechBtns.map((el, index) => ({
type: skillsType[index],
id: el,
unlocked: false,
purchased: false,
used: false,
}));
const p2Skills = p2TechBtns.map((el, index) => ({
type: skillsType[index],
id: el,
unlocked: false,
purchased: false,
used: false,
}));
const p3Skills = p3TechBtns.map((el, index) => ({
type: skillsType[index],
id: el,
unlocked: false,
purchased: false,
used: false,
}));
const skillsTop = [];
let clickedSkill;
let clickedSkillIndex;
// CLASS PLAYER //
// creates ONCE in SetPlayer //
class Player {
constructor(name, nr, color, turnActive, action) {
this.type = `player`;
this.actionDone = false;
this.name = name;
this.nr = nr;
this.color = color;
this.turnActive = turnActive;
this.action = action;
this.resource = {
food: 4,
wood: 4,
stone: 4,
gold: 4,
idea: 4,
culture: 4,
morale: 8,
};
Player.prototype.canAfford = function (cost) {
if (true) {
// Check if deducting costs will result in negative resources
if (
this.resource.food - (cost.food || 0) < 0 ||
this.resource.wood - (cost.wood || 0) < 0 ||
this.resource.stone - (cost.stone || 0) < 0 ||
this.resource.gold - (cost.gold || 0) < 0 ||
this.resource.idea - (cost.idea || 0) < 0 ||
this.resource.culture - (cost.culture || 0) < 0 ||
this.resource.morale - (cost.morale || 0) < 0
) {
// Insufficient resources, halt the method
alert(`Masz za mało surowców!`);
return false;
} else return true;
} else return true;
};
Player.prototype.bearTheCost = function (cost, action) {
console.log(`Wlasnie ponosisz koszt `, action);
if (!this.canAfford(cost)) return;
else {
this.resource.food -= cost.food || 0;
this.resource.wood -= cost.wood || 0;
this.resource.stone -= cost.stone || 0;
this.resource.gold -= cost.gold || 0;
this.resource.idea -= cost.idea || 0;
this.resource.culture -= cost.culture || 0;
this.resource.morale -= cost.morale || 0;
this.showPlayerResource();
return true;
}
};
Player.prototype.showPlayerResource = function () {
for (let i = 0; i < res.length; i++) {
window[`p` + this.nr + `GlobalResourceDiv`][i].innerHTML =
this.resource[res[i]];
}
};
this.setSkills = () => {
if (this.nr === 1) return p1Skills;
if (this.nr === 2) return p2Skills;
if (this.nr === 3) return p3Skills;
};
this.checkStart = () => {
if (this.nr == 1) return 0;
else if (this.nr == 2) return 35;
else if (this.nr == 3) return 5;
};
this.skills = this.setSkills();
// Set first 2 skills //
this.skills[0].purchased = true;
this.skills[4].purchased = true;
this.skills[0].id.style.backgroundColor = this.color;
this.skills[4].id.style.backgroundColor = this.color;
this.skills[0].id.disabled = true;
this.skills[4].id.disabled = true;
// Set skillsTop to unclocked //
for (let i = 8; i < this.skills.length; i = i + 4) {
if (i < 36) {
this.skills[i].unlocked = true;
}
skillsTop.push(this.skills[i]);
}
// Set first 8 skills to unclocked //
for (let i = 0; i < 8; i++) {
this.skills[i].unlocked = true;
}
this.start = this.checkStart();
Player.prototype.makeAction = function () {
this.action--;
window[`p` + this.nr + `ActionValue`].textContent = this.action;
console.log(`PLAYER MADE ACTION`);
};
}
}
// CLASS TREE //
// creates ONCE in SetPlayer //
class Tree {
constructor() {
this.player = player;
this.name = player.name;
this.nr = player.nr;
this.color = player.color;
this.player.setSkills();
Tree.prototype.showTechTree = function (player) {
window[`p` + player.nr + `TechTree`].style.display = `block`;
window[
`p` + player.nr + `TechTreeTitle`
].textContent = `Technology Tree ${player.name}`;
Tree.prototype.showResLine(player);
};
Tree.prototype.showResLine = (player) => {
resBg.map((el) => (el.style.display = `none`));
for (let i = 0; i < res.length - 2; i++) {
const techRes = player.resource[res[i]];
let childs;
let foundRes;
window[`p` + player.nr + `TechRes`].forEach((el, index) => {
childs = Array.from(el.children);
foundRes = childs.find((span) =>
span.classList.contains(res[i] + `-icon`)
);
if (index === techRes) return (foundRes.style.display = `block`);
});
}
};
Tree.prototype.showConfirmAdvance = (clickedSkill) => {
skillConfirmContainer.style.display = `block`;
skillConfirmContainer.children[0].innerHTML = `Czy chcesz rozwinąć "${clickedSkill.type}"`;
};
Tree.prototype.hideConfirmAdvance = () =>
(skillConfirmContainer.style.display = `none`);
// ----- show/hide TECH TREE ----- //
p1TreeBtn.addEventListener(`click`, () =>
Tree.prototype.showTechTree(player1)
);
p2TreeBtn.addEventListener(`click`, () =>
Tree.prototype.showTechTree(player2)
);
p3TreeBtn.addEventListener(`click`, () =>
Tree.prototype.showTechTree(player3)
);
// Make an Advance //
this.player.skills.forEach((el, index) => {
el.id.addEventListener(`click`, () => {
if (el.unlocked) {
clickedSkill = el;
clickedSkillIndex = index;
Tree.prototype.showConfirmAdvance(clickedSkill);
} else
alert(
`Aby odblokować tę Kategorię musisz najpierw rozwinąć szczytowe Rozwinięcie`
);
});
});
// Click OK and make Advance //
confirmAdvance.addEventListener(`click`, () => {
if (player.bearTheCost(cost.advance, `Rozwiniecie`)) {
clickedSkill.purchased = true;
clickedSkill.id.style.backgroundColor = player.color;
clickedSkill.id.disabled = true;
Tree.prototype.skillAffect(clickedSkillIndex);
Tree.prototype.showResLine(player);
// Get morale or Culture instantly after Advance //
if (clickedSkill.id.classList.contains(`border-morale`))
this.player.resource.morale++;
if (clickedSkill.id.classList.contains(`border-culture`))
this.player.resource.culture++;
Hud.prototype.refreshCultureMorale();
// Unlock 3 more skills above //
if (skillsTop.includes(clickedSkill)) {
// Make sure to check if there's a next skill in the array.
this.player.skills[clickedSkillIndex + 1].unlocked = true;
this.player.skills[clickedSkillIndex + 2].unlocked = true;
this.player.skills[clickedSkillIndex + 3].unlocked = true;
}
// Unlock each Democracy, Autocracy, Teocracy //
if (
clickedSkillIndex === 15 ||
clickedSkillIndex === 19 ||
clickedSkillIndex === 23
)
this.player.skills[clickedSkillIndex + 21].unlocked = true;
Tree.prototype.hideConfirmAdvance();
clickedSkill = undefined;
clickedSkillIndex = undefined;
player.action--;
window[`p` + player.nr + `ActionValue`].textContent = player.action;
}
});
cancelAdvance.addEventListener(`click`, () => {
Tree.prototype.hideConfirmAdvance();
clickedSkill = undefined;
});
// Advance affects game IMMIDETALLY //
Tree.prototype.skillAffect = (clickedSkillIndex) => {
// Irigation //
if (clickedSkillIndex === 2) {
hexAll.forEach((el) => {
if (el.hex.vis) el.hex.checkCollectible();
});
}
// Fishing //
if (clickedSkillIndex === 8) {
hexAll.forEach((el) => {
if (el.hex.vis) el.hex.checkCollectible();
});
}
};
// end Tree constructor //
}
}