-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
341 lines (304 loc) · 8.56 KB
/
game.js
File metadata and controls
341 lines (304 loc) · 8.56 KB
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// game.js
class Game {
constructor() {
this.currentLevel = 0;
this.squares = [];
this.pieces = [];
this.capturedPieces = [];
this.totalCapturedPieces = [];
this.moveHistory = [];
this.totalMoveHistory = [];
this.background = new Background('black');
this.player = null;
this.playerName = '';
this.checks = 0;
this.totalChecks = 0;
this.hud = new Hud();
this.serverOnline = false;
this.menu = new Menu();
this.check = {
checked: false,
checkX: 200,
checkY: 200,
};
this.gameOver = false;
this.gameStarted = false;
this.music = new Sound('./utils/music2.mp3');
}
setup() {
if (!this.serverOnline) {
wakeUpServer();
}
this.background.setup();
// Reset arrays when (re)starting the level
this.squares = [];
this.pieces = [];
// Generate the board for the current level
const board = maps[this.currentLevel].generate();
for (let i = 0; i < BOARD_SIZE; i++) {
for (let j = 0; j < BOARD_SIZE; j++) {
const cellValue = board[i][j];
// Create a piece if there is one on this cell
const newPiece = getMapPiece(cellValue, i, j);
if (newPiece) {
if (newPiece.name === 'player') {
this.player = newPiece;
} else {
this.pieces.push(newPiece);
}
}
// Create a square at this grid location with determined color
const squareColorInfo = getSquareInfo(i, j, this.currentLevel);
const square = new Square(
i * SQUARE_SIZE,
j * SQUARE_SIZE,
squareColorInfo
);
this.squares.push(square);
}
}
// Setup all non-player pieces (sorting so goals are processed first)
this.pieces
.sort((a, b) => (a.name === 'goal' ? -1 : 1))
.forEach((piece) => piece.setup());
if (this.player) {
this.player.setup();
}
}
draw() {
if (!this.gameStarted) {
this.menu.draw();
return;
}
this.background.draw();
// Draw board squares
this.squares.forEach((square) => {
square.draw();
// For certain levels, reset square colors if pieces were captured
if (
[1, 8, 12, 16, 18].includes(this.currentLevel) &&
this.capturedPieces.length
) {
square.resetColor();
}
});
// Draw pieces and check for collisions
this.pieces.forEach((piece) => {
piece.draw();
if (piece.name === 'goal' && piece.collisionCheck(this.player)) {
this.handleNewLevel();
}
if (piece.collisionCheck(this.player)) {
piece.tempExpandImage();
this.handleCheckCollision(this.player.x, this.player.y);
}
});
// Process captures
this.pieces.forEach((piece) => {
if (this.captureCheck(piece, this.player) && piece.name !== 'goal') {
this.handleCapture(piece);
}
});
// Draw the player last so they appear on top
this.player.draw();
this.hud.draw();
// Display check notification if active
if (this.check.checked) {
textSize(32);
fill('tomato');
text('CHECK', this.check.checkX, this.check.checkY);
}
}
handleCheckCollision(x, y) {
this.player.knockBack();
this.hud.blinkCheck();
this.handleCheck(x, y);
aa.play('check'); // Play check sound
this.blinkTile(x, y);
}
handleCheck(x, y) {
this.check.checked = true;
let helper = 150;
this.check.checkX = x;
this.check.checkY = y;
this.checks += 1;
this.totalChecks += 1;
const checkInterval = setInterval(() => {
this.check.checkY -= 0.15;
helper--;
if (helper < 0) {
clearInterval(checkInterval);
this.check.checked = false;
}
}, 1);
}
handleNewLevel() {
levelComplete({
capturedPieces: this.capturedPieces.length,
moveHistory: this.moveHistory,
checks: this.checks,
timer: this.hud.timer,
playerName: this.playerName,
currentLevel: this.currentLevel,
});
this.currentLevel++;
this.pieces = [];
this.squares = [];
this.capturedPieces = [];
this.moveHistory = [];
this.checks = 0;
this.player = null;
this.hud.saveTime();
this.hud.timer = 15;
this.setup();
}
resetLevel() {
this.pieces = [];
this.squares = [];
this.capturedPieces = [];
this.moveHistory = [];
this.checks = 0;
this.player = null;
this.hud.timer = 15;
this.setup();
}
captureCheck(piece, player) {
// Simple AABB collision check
if (
player.x + player.width <= piece.x ||
piece.x + piece.width <= player.x
) {
return false;
}
if (
piece.y >= player.y + piece.height ||
player.y >= piece.y + piece.height
) {
return false;
}
if (player.isMoving) {
return false;
}
return true;
}
handleCapture(piece) {
const pieceId = piece.id;
if (this.capturedPieces.indexOf(pieceId) > -1) {
return;
}
aa.play('capture');
this.capturedPieces.push(pieceId);
this.totalCapturedPieces.push(pieceId);
piece.handleDead();
this.hud.blinkCapture();
}
blinkTile(x, y) {
const trueX = x - 25;
const trueY = y - 25;
const checkSquare = this.squares.find(
(s) => s.x === trueX && s.y === trueY
);
if (!checkSquare) {
console.log('yikes');
this.resetLevel(); // fail-safe in case of an error
} else {
checkSquare.blinkSquare();
}
}
stopGame() {
this.gameOver = true;
this.player.isMoving = true;
this.music.stop();
// noLoop(); // Uncomment if you wish to stop the p5.js draw loop
}
}
// ------------------------------------------------------------
// Helper Functions
// ------------------------------------------------------------
/**
* Create a new game piece based on the board identifier.
* Returns null if the cell is empty.
*/
function getMapPiece(pieceIdentifier, i, j) {
if (pieceIdentifier === ' ') return null;
const yNotation = String.fromCharCode(65 + j);
const xNotation = 8 - i;
const posX = j * SQUARE_SIZE; // x position based on column
const posY = i * SQUARE_SIZE; // y position based on row
const offset = 25; // offset to center the piece
const pieces = {
G: new FinishTile(posX, posY, `${yNotation}${xNotation}`),
P: new Pawn(posX + offset, posY + offset, `${yNotation}${xNotation}`),
R: new Rook(posX + offset, posY + offset, `${yNotation}${xNotation}`),
B: new Bishop(posX + offset, posY + offset, `${yNotation}${xNotation}`),
N: new Knight(posX + offset, posY + offset, `${yNotation}${xNotation}`),
S: new Spikes(posX + offset, posY + offset, `${yNotation}${xNotation}`),
Q: new Queen(posX + offset, posY + offset, `${yNotation}${xNotation}`),
A: new Player(posX + offset, posY + offset, 'player'),
};
return pieces[pieceIdentifier];
}
/**
* Determine the square color based on its position and the current level.
* Returns an array: [color (RGB array), shade, helperSquare flag].
*/
function getSquareInfo(row, col, level) {
let color;
let helperSquare = false;
let isLight = false;
let r;
let g;
let b;
if ((row % 2 === 0 && col % 2 === 0) || (row % 2 === 1 && col % 2 === 1)) {
isLight = true;
r = Math.random() * 4 + 245;
g = Math.random() * 4 + 245;
b = Math.random() * 4 + 220;
} else {
r = Math.random() * 4 + 222;
g = Math.random() * 4 + 184;
b = Math.random() * 4 + 135;
}
if (helperLevelShowGuardedTile(row, col, level)) {
r = 222;
g = 134;
b = 85;
helperSquare = true;
}
color = [r, g, b];
return { color, helperSquare, isLight };
}
/**
* Determines whether to show a helper (guarded) tile based on level.
*/
function helperLevelShowGuardedTile(row, col, level) {
const showHelperLevels = {
1: { 2: [3], 4: [3] },
8: { 1: [3], 2: [3], 3: [1, 2, 4, 5], 4: [3], 5: [3], 6: [3] },
12: { 0: [7], 1: [6], 2: [1, 5], 3: [2, 4], 5: [2, 4], 6: [1], 7: [0] },
16: { 2: [2, 4], 3: [1, 5], 5: [1, 5], 6: [2, 4] },
18: { 4: [3] },
};
if (showHelperLevels[level] && showHelperLevels[level][row]) {
return showHelperLevels[level][row].includes(col);
}
return false;
}
/**
* Sound constructor.
* Creates an audio element for playback.
*/
function Sound(src) {
this.sound = document.createElement('audio');
this.sound.src = src;
this.sound.setAttribute('preload', 'auto');
this.sound.setAttribute('controls', 'none');
this.sound.style.display = 'none';
document.body.appendChild(this.sound);
this.play = function () {
this.sound.play();
};
this.stop = function () {
this.sound.pause();
};
}