-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
317 lines (267 loc) · 10.5 KB
/
main.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
307
308
309
310
311
312
313
314
315
316
317
import * as CLASSES from './modules/constants/classes.js';
import * as SKILLS from './modules/constants/skills.js';
import * as Game from './modules/game.js';
import * as Html from './modules/html.js';
import * as Storage from './modules/storage.js';
const classNotesName = document.getElementById('class-notes-name');
const classNotesDescription = document.getElementById('class-notes-description');
const contentNode = document.getElementById('scene-content');
const chosenClassInfo = document.getElementById('chosen-class-info');
const chosenClassNode = document.getElementById('chosen-class');
const confirmClassButton = document.getElementById('confirm-class-button');
const footerNode = document.getElementById('scene-footer');
const addSkillButton = (parent, skill, addSkillEventListeners) => {
const skillButton = createSkillButton();
const splitName = skill.name.split(' ');
skillButton.append(splitName[0]);
if (splitName.length === 1) {
skillButton.classList.add('skill-button-single-word');
} else {
for (let i = 1; i < splitName.length; i++) {
skillButton.append(document.createElement('br'));
skillButton.append(splitName[i]);
}
skillButton.style.lineHeight = (50 / splitName.length) + 'px';
}
addSkillEventListeners(skillButton, skill);
parent.appendChild(skillButton);
};
const chooseClass = (el, chosenClass) => {
if (Game.getClass() !== null) {
if (chosenClass === Game.getClass()) {
el.classList.remove('selected-box');
Game.setClass(null);
chosenClassNode.innerText = 'None';
} else {
for (const el of document.getElementsByClassName('class-button')) {
el.classList.remove('selected-box');
}
Game.setClass(chosenClass);
chosenClassNode.innerText = chosenClass.displayName;
el.classList.add('selected-box');
}
} else {
Game.setClass(chosenClass);
chosenClassNode.innerText = chosenClass.displayName;
el.classList.add('selected-box');
}
if (Game.getClass() === null) {
confirmClassButton.disabled = true;
} else {
confirmClassButton.disabled = false;
}
};
const confirmClass = () => {
if (Game.getClass() != null) {
Game.confirmClass();
transitionScene();
}
};
const createClassButton = characterClass => {
const nodeClass = characterClass.cssName + '-class';
const node = document.getElementById(nodeClass);
const setNotes = () => setClassNotes(characterClass.displayName, characterClass.description);
node.addEventListener('mouseover', setNotes);
node.addEventListener('mouseout', resetClassNotes);
node.addEventListener('click', () => chooseClass(node, characterClass));
};
const createSkillButton = () => {
const div = document.createElement('div');
div.classList.add('skill-button', 'hover-pointer');
return div;
};
const createSkillContainers = () => {
const [skillName, skillNameSpan] = createValueContainer('Skill: ', 'skill-name', 'None');
const [skillDescription, skillDescriptionSpan] = createValueContainer('Description: ', 'skill-description', 'N/A');
const [skillTree, skillTreeSpan] = createValueContainer('Skill Tree: ', 'skill-tree', 'N/A');
const [skillCost, skillCostSpan] = createValueContainer('Skill Cost: ', 'skill-cost', 'N/A');
return [
skillName,
skillNameSpan,
skillDescription,
skillDescriptionSpan,
skillTree,
skillTreeSpan,
skillCost,
skillCostSpan
];
};
const createValueContainer = (label, baseId, defaultValue) => {
const container = document.createElement('p');
container.setAttribute('id', baseId + '-container');
const containerLabel = document.createElement('strong');
containerLabel.setAttribute('id', baseId + '-label');
containerLabel.append(label);
const containerSpan = document.createElement('span');
containerSpan.setAttribute('id', baseId);
containerSpan.append(defaultValue);
container.append(containerLabel, containerSpan);
return [container, containerSpan];
};
const finishIntroduction = () => {
Game.finishIntroduction();
transitionScene();
};
const resetClassNotes = () => {
if (Game.getClass() === null) {
setClassNotes('None', 'N/A');
}
};
const setClassNotes = (name, description) => {
classNotesName.innerText = name;
classNotesDescription.innerText = description;
};
const transitionScene = () => {
const currentScene = Game.getScene();
Html.removeAllChildren(contentNode);
Html.removeAllChildren(footerNode);
if (currentScene === "INTRODUCTION") {
contentNode.appendChild(Html.h2('Introduction'));
contentNode.appendChild(Html.pStrongFirst('Necromancer currently supports only limited functionality: character skill management and random battles.'));
contentNode.appendChild(Html.p('Choose your name to begin skill selection.'));
const firstDiv = document.createElement('div');
firstDiv.style.marginBottom = '0.5vh';
const firstLabel = document.createElement('label');
firstLabel.append('First Name: ');
firstDiv.appendChild(firstLabel);
const firstInput = document.createElement('input');
let firstNameFilled = false;
firstInput.setAttribute('id', 'first-name-input');
firstDiv.appendChild(firstInput);
contentNode.appendChild(firstDiv);
const lastDiv = document.createElement('div');
lastDiv.style.marginBottom = '1vh';
const lastLabel = document.createElement('label');
lastLabel.append('Last Name: ');
lastDiv.appendChild(lastLabel);
const lastInput = document.createElement('input');
lastInput.style.marginLeft = '2px';
let lastNameFilled = false;
lastInput.setAttribute('id', 'last-name-input');
lastDiv.appendChild(lastInput);
contentNode.appendChild(lastDiv);
const next = document.createElement('button');
firstInput.addEventListener('input', e => {
const value = e.target.value;
if (value && value.length > 0) {
firstNameFilled = true;
Game.setFirstName(value);
} else {
firstNameFilled = false;
}
if (firstNameFilled && lastNameFilled) {
next.disabled = false;
} else {
next.disabled = true;
}
});
lastInput.addEventListener('input', e => {
const value = e.target.value;
if (value && value.length > 0) {
lastNameFilled = true;
Game.setLastName(value);
} else {
lastNameFilled = false;
}
if (firstNameFilled && lastNameFilled) {
next.disabled = false;
} else {
next.disabled = true;
}
});
next.disabled = true;
next.setAttribute('id', 'finish-introduction-button');
next.classList.add('standard-button', 'hover-pointer');
next.append('Next');
next.addEventListener('click', () => {
if (next.disabled) {
return;
}
finishIntroduction();
});
contentNode.appendChild(next);
} else if (currentScene === "CHARACTER") {
contentNode.appendChild(Html.h2('Skills'));
contentNode.appendChild(Html.pStrongFirst(`Choose the first skill for ${Game.getFullName()}, the novice ${Game.getClass().displayName}.`));
const [
skillName,
skillNameSpan,
skillDescription,
skillDescriptionSpan,
skillTree,
skillTreeSpan,
skillCost,
skillCostSpan
] = createSkillContainers();
footerNode.appendChild(skillName);
footerNode.appendChild(skillDescription);
footerNode.appendChild(skillTree);
footerNode.appendChild(skillCost);
let skillTextLocked = false;
let skillTextLockedBy = null;
const lockSkillText = skill => {
skillTextLocked = true;
skillTextLockedBy = skill;
};
const unlockSkillText = () => {
skillTextLocked = false;
skillTextLockedBy = null;
};
const setSkillText = (name, description, tree, cost) => {
skillNameSpan.innerText = name;
skillDescriptionSpan.innerText = description;
skillTreeSpan.innerText = tree;
skillCostSpan.innerText = cost;
};
const clearSkillText = () => {
if (skillTextLocked) {
return;
}
setSkillText('None', 'N/A', 'N/A', 'N/A');
};
const addSkillEventListeners = (el, skill) => {
el.addEventListener(
'mouseover',
() => setSkillText(
skill.name,
skill.description,
skill.skillType.name,
skill.cost + ' skill ' + (skill.cost === 1 ? ' point' : ' points')
)
);
el.addEventListener(
'mouseout',
clearSkillText
);
el.addEventListener(
'click',
() => {
if (skillTextLocked && skill.name === skillTextLockedBy) {
unlockSkillText();
el.classList.remove('selected-box');
Game.setSkill(null);
return;
}
if (skillTextLocked) {
for (const el of document.getElementsByClassName('selected-box')) {
el.classList.remove('selected-box');
}
}
lockSkillText(skill.name);
el.classList.add('selected-box');
Game.setSkill(skill);
}
);
};
for (const skill of Game.getStartingSkills()) {
addSkillButton(contentNode, skill, addSkillEventListeners);
}
}
};
Object.values(CLASSES).forEach(createClassButton);
confirmClassButton.addEventListener('click', confirmClass);
document.addEventListener('mousedown', e => {
if (e.detail > 1) {
e.preventDefault();
}
}, false);