-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
250 lines (213 loc) · 6.96 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
import Gameboard from './Gameboard.js';
import Modal from './Modal.js';
import Storage from './Storage.js';
import GameController from './GameController.js';
import MainEventHandler from './MainEventHandler.js';
import levelFiles from './levelFiles.js';
import SoundMachine from './SoundMachine.js';
import Options from './Options.js';
import Services from './Services.js';
import PlayTimer from './PlayTimer.js';
import Scoreboard from './Scoreboard.js';
import Messages from './Messages.js';
const services = new Services();
services.add('modal', new Modal('modal-1'));
services.add('scoreboard', new Scoreboard());
services.add('mainEventHandler', new MainEventHandler(new GameController()));
services.add('soundPlayer', new SoundMachine());
services.add('playTimer', new PlayTimer(services.soundPlayer));
const messagesServices = new Services();
messagesServices.add('modal', services.modal);
messagesServices.add('scoreboard', services.scoreboard);
services.add('messages', new Messages(messagesServices));
const gameboard = new Gameboard('gameboard', services);
let storage = undefined;
let currentLevel = null; // this will be stored as a string
const main = document.getElementsByTagName('main')[0];
const options = new Options(services);
async function loadLevel(levelStr, details = {}) {
currentLevel = String(levelStr);
storage.set('currentLevel', currentLevel);
if (currentLevel === '0') {
document.getElementById('main').dataset.columns = 0;
document.getElementById('gameboard').innerHTML = '';
document.getElementById('welcome').classList.add('show');
document.getElementById('gameboard-buttons').innerHTML = '';
main.dataset.level = 0;
return;
}
document.getElementById('welcome').classList.remove('show');
let module;
try {
const levelFile = levelFiles[currentLevel];
module = await import(`./levels/${levelFile}.js`);
}
catch (e) {
console.log(`Couldn\'t load level ${levelStr}: ` + e.message);
console.trace();
document.getElementById('test-controls').classList.remove('hide');
if (document.getElementById('main').dataset.level === "0") {
document.getElementById('welcome').classList.add('show');
}
return;
}
console.log(`module = `, module);
console.log(`services.messages = `, services.messages);
let level = new module[levelFiles[currentLevel]](details, services.messages);
level.level = currentLevel;
gameboard.level = level;
gameboard.init();
main.dataset.level = levelStr;
main.dataset.columns = gameboard.columns;
}
function loadNextLevel() {
currentLevel = gameboard.level.nextLevel || String(parseInt(currentLevel) + 1);
try {
storage.set('currentLevel', currentLevel);
}
catch (e) {
console.log('error setting currentLevel in storage: ', e);
}
loadLevel(currentLevel);
}
function stopPlayTimer() {
services.playTimer.stop();
}
document.getElementById('enter-game').addEventListener('click', function () {
document.getElementById('welcome').classList.remove('show');
loadLevel(1);
});
services.mainEventHandler.register(document.body, 'dementia-event', 'onBody', handleDementiaEvents);
function handleDementiaEvents(e) {
if (e.detail.type === 'timer-end') {
services.modal.show({
message: e.detail.message,
onClose: () => {
loadLevel(currentLevel);
}
});
return;
}
if (e.detail.type === 'message') {
services.modal.show({
message: e.detail.message,
onClose: () => {
e.detail.from.focus();
}
});
return;
}
if (e.detail.type === 'rotate-gameboard') {
gameboard.deactivateClickHandler();
gameboard.rotate(e.detail.degrees, e.detail.speed);
return;
}
stopPlayTimer();
if (e.detail.type === 'clicked-out') {
services.modal.show({
message: e.detail.message,
onClose: () => {
loadLevel(currentLevel, e.detail);
}
});
return;
}
if (e.detail.type === 'level-completed') {
try {
storage.set('currentLevel', String(parseInt(currentLevel) + 1));
}
catch (e) {
console.log(`error setting currentLevel in storage: `, e);
}
return;
}
if (e.detail.type === 'goto-next-level') {
loadNextLevel();
return;
}
if (e.detail.type === 'halt-play') {
gameboard.deactivateClickHandler();
return;
}
if (e.detail.type === 'replay-level') {
if (e.detail.message) {
services.modal.show({
message: e.detail.message,
onClose: () => {
loadLevel(currentLevel);
}
});
}
else {
loadLevel(currentLevel);
}
return;
}
if (e.detail.type === 'goto-level') {
if (e.detail.message) {
services.modal.show({
message: e.detail.message,
onClose: () => {
loadLevel(e.detail.level);
}
});
}
else {
loadLevel(e.detail.level);
}
return;
}
if (e.detail.type === 'bomb-explode') {
gameboard.explode(e.detail.bomb);
return;
}
console.log(`Error handling dementia-event: don't know how to handle type: `, e);
}
try {
storage = new Storage();
console.log('storage ready');
}
catch (e) {
console.log(e);
}
// check to see if user has already advanced to a certain level
try {
currentLevel = storage.get('currentLevel');
if (typeof currentLevel === 'string') {
if (!(/^[1-9]/.test(currentLevel))) {
currentLevel = null;
}
else {
currentLevel = String(parseInt(currentLevel));
}
}
}
catch (e) {
currentLevel = null;
console.log('error getting currentLevel from storage: ', e);
}
console.log(`currentLevel = ${currentLevel}`);
if (currentLevel !== null && currentLevel !== 0) {
//document.getElementById('welcome').innerHTML = "";
loadLevel(currentLevel);
}
else {
document.getElementById('welcome').classList.add('show');
}
document.getElementById('start-over').addEventListener('click', function () {
document.getElementById('start-over').style.removeProperty('display');
loadLevel('0');
});
document.getElementById('gotolevel').addEventListener('change', function (e) {
stopPlayTimer();
loadLevel(e.target.value);
});
const select = document.getElementById('gotolevel');
const levelFilesKeys = Object.keys(levelFiles);
const levelFilesSet = new Set(levelFilesKeys.map(key => parseInt(key)));
for (let levelNum = 1; levelNum <= levelFilesSet.size; levelNum++) {
const option = document.createElement('option');
option.value = levelNum;
option.innerHTML = levelNum;
select.appendChild(option);
}