-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
269 lines (225 loc) · 6.09 KB
/
game.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
'use strict';
console.clear();
import { Sandbox } from './scripts/sandbox.js';
import { Snake } from './scripts/snake.js';
import { Ui } from './scripts/ui.js';
import { canvas, canvasSize, ctx } from './scripts/canvasConfig.js';
/* LEVEL PROGRESS */
if (localStorage.getItem('levelProgress') === null) {
console.info('first time playing; setting brand new level progress JSON data');
localStorage.setItem('levelProgress', JSON.stringify([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]));
}
const levelProgress = JSON.parse(localStorage.getItem('levelProgress'));
console.log(levelProgress);
/* GLOB VARIABLES */
let allLevelsJson;
let currentLevel = 1;
export let getCurrentLevelJson = () => JSON.parse(JSON.stringify(allLevelsJson[currentLevel])); //! use gameSandbox.grid to access updated level data instead
export let cellTypesJson;
let gameSandbox;
let snake;
let swipes = 0;
let starsGotten = 0;
export let themeJson;
/* GAME GESTURES */
// eslint-disable-next-line no-undef
const gest = new Hammer(document);
let panGesture = undefined;
let panGestureLock = false;
gest.on('panend', function (event) {
if (event.additionalEvent === undefined || panGestureLock) return;
switch (event.additionalEvent) {
case 'panup':
panGesture = 'N';
break;
case 'pandown':
panGesture = 'S';
break;
case 'panleft':
panGesture = 'W';
break;
case 'panright':
panGesture = 'E';
break;
default:
break;
}
panGestureLock = true;
sfx.swipe.currentTime = 0.05;
sfx.swipe.play();
});
document.addEventListener('keyup', function (event) {
if (panGestureLock) return;
switch (event.code) {
case 'ArrowUp':
case 'KeyW':
panGesture = 'N';
break;
case 'ArrowDown':
case 'KeyS':
panGesture = 'S';
break;
case 'ArrowLeft':
case 'KeyA':
panGesture = 'W';
break;
case 'ArrowRight':
case 'KeyD':
panGesture = 'E';
break;
default:
return;
}
panGestureLock = true;
sfx.swipe.currentTime = 0.05;
sfx.swipe.play();
});
/* CURSOR */
const mouse = {
click: false,
x: canvasSize / 2,
y: canvasSize / 2,
};
const mouseImg = new Image();
mouseImg.src = './assets/fancyCursor.png';
canvas.addEventListener('mousemove', function (event) {
mouse.x = event.offsetX;
mouse.y = event.offsetY;
});
const instructions = document.getElementById('instructions');
instructions.addEventListener('click', function () {
instructions.hidden = true;
});
document.addEventListener('click', function () {
mouse.click = true;
instructions.hidden = true;
});
/* SFX */
const sfx = {
swipe: new Audio('./assets/sfx/8-bit-explosion.mp3'),
endOfLevel: new Audio('./assets/sfx/8-bit-powerup.mp3'),
};
/* GAME */
export const GAME_STATES = {
intro: 'intro',
levelSelect: 'levelSelect',
main: 'main',
levelEnded: 'levelEnded',
};
let currentGameState = GAME_STATES.intro;
function loop() {
//* UI
switch (currentGameState) {
case GAME_STATES.intro: {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvasSize, canvasSize);
currentGameState = Ui.intro(mouse);
if (currentGameState === GAME_STATES.main) {
newLevel(currentLevel);
}
break;
}
case GAME_STATES.levelSelect: {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvasSize, canvasSize);
let level;
[currentGameState, level] = Ui.levelSelect(levelProgress, mouse);
if (currentGameState === GAME_STATES.main) {
newLevel(level);
}
break;
}
case GAME_STATES.levelEnded:
case GAME_STATES.main: {
ctx.fillStyle = '#000';
ctx.fillRect(0, 0, canvasSize, canvasSize);
currentGameState = Ui.main(currentLevel, snake, swipes, getCurrentLevelJson().swipesRequired, getCurrentLevelJson().levelName, mouse, currentGameState);
break;
}
default:
break;
}
//* core
switch (currentGameState) {
case GAME_STATES.main:
case GAME_STATES.levelEnded: {
//* loop the main cores of the game
let endOfMovement = false;
gameSandbox.loop();
if (currentGameState === GAME_STATES.main) {
[panGesture, panGestureLock, endOfMovement, currentGameState, starsGotten] = snake.move(panGesture, currentGameState, swipes);
if (currentGameState === GAME_STATES.levelEnded) {
sfx.endOfLevel.play();
levelProgress[currentLevel - 1] = Math.max(starsGotten, levelProgress[currentLevel - 1]);
localStorage.setItem('levelProgress', JSON.stringify(levelProgress));
}
}
snake.draw();
swipes += +endOfMovement;
break;
}
default:
break;
}
//* apply level ended mask
if (currentGameState === GAME_STATES.levelEnded) {
let nextLevelShortcut = false;
[currentGameState, nextLevelShortcut] = Ui.levelEnded(mouse, starsGotten);
if (nextLevelShortcut) {
if (currentLevel === 9) {
currentGameState = GAME_STATES.levelSelect;
} else {
newLevel(currentLevel + 1);
currentGameState = GAME_STATES.main;
}
}
}
//* draw the mouse
ctx.drawImage(mouseImg, mouse.x, mouse.y, (canvasSize * 158) / 4800, (canvasSize * 254) / 4800);
//* for debug purposes only
window.selfVars = {
snake: snake,
currentGameState: currentGameState,
};
mouse.click = false;
requestAnimationFrame(loop);
}
async function init() {
await fetch('./dictionaries/levels.json')
.then((res) => res.json())
.then((levelsFetched) => {
allLevelsJson = levelsFetched;
console.log(allLevelsJson);
})
.catch((error) => {
console.error('Error loading levels JSON:', error);
});
await fetch('./dictionaries/cellTypes.json')
.then((res) => res.json())
.then((levelsFetched) => {
cellTypesJson = levelsFetched;
console.log(cellTypesJson);
})
.catch((error) => {
console.error('Error loading levels JSON:', error);
});
await fetch('./dictionaries/theme.json')
.then((res) => res.json())
.then((themeFetched) => {
themeJson = themeFetched;
console.log(themeJson);
})
.catch((error) => {
console.error('Error loading levels JSON:', error);
});
loop();
}
init();
function newLevel(level) {
currentLevel = level;
gameSandbox = new Sandbox(getCurrentLevelJson().params.width, getCurrentLevelJson().params.height, getCurrentLevelJson().datum);
snake = new Snake(getCurrentLevelJson().snake, gameSandbox);
panGesture = undefined;
panGestureLock = false;
swipes = 0;
}