-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
342 lines (299 loc) · 10.4 KB
/
code.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// grid size
const gridWidth = 32;
const gridHeight = 24;
// emoji codes
const DOOR = '🚪';
const WALL = '🟦';
const BUNNY = '🐇';
const BOX = '🟩';
const CARROT = '🥕';
const PLACED_CARROT = '🟧';
const EMPTY = '⬜';
function obstacle(c) { return (c === WALL || c === PLACED_CARROT || c === BOX); }
/* GAME STATE */
// the game field
let gameField = [];
// the bunny position
let bunnyPosition = {x: 0, y: 0};
let lastMove = 1; // 1 is right, -1 is left
let collectedCarrots = 0;
/* END OF GAME STATE */
let history = [];
// canvas and context
let canvas = document.getElementById('gameCanvas');
let ctx = canvas.getContext('2d');
// size of each cell in pixels
let cellSize = 32;
// moving disabled, because we are doing gravity check
let gravityInMotion = false;
// initialize game field
function stringToGameField(inputString) {
// Prepare the empty game field
let gameField = [];
for (let y = 0; y < gridHeight; y++) {
gameField[y] = [WALL];
}
// Split the input by new lines
let lines = inputString.trimRight().split('\n');
for (let x = 0; x < gridWidth; x++) {
gameField[0][x] = WALL;
gameField[gridHeight - 1][x] = WALL;
}
// Loop through each line
if (lines.length + 2 != gridHeight) throw("Bad number of lines: " + lines.length);
let bx = -1, by = -1;
for (let y = 1; y < lines.length + 1; y++) {
let line = lines[y - 1];
// Split the line into characters
let chars = line.split('');
// Convert each character to a game element and add it to the game field
if (chars.length + 2 != gridWidth) throw("Bad number of columns: " + chars.length);
for (let x = 1; x < chars.length + 1; x++) {
let char = chars[x - 1];
switch (char) {
case '#':
gameField[y][x] = WALL;
break;
case 'p':
gameField[y][x] = BOX;
break;
case 'b':
gameField[y][x] = BUNNY;
by = y;
bx = x;
break;
case ' ':
gameField[y][x] = EMPTY;
break;
case 'g':
gameField[y][x] = DOOR;
break;
case 'c':
gameField[y][x] = CARROT;
break;
default:
throw('Unrecognized character: ' + char);
break;
}
}
gameField[y][gridWidth - 1] = WALL;
}
if (by === -1 || bx === -1) throw('no bunny');
return { gameField: gameField, bunnyX: bx, bunnyY: by };
}
async function initializeGameField() {
try {
let r = (await fetch(window.location.search.substring(1)));
let field = (await r.text());
let ret = stringToGameField(field);
gameField = ret.gameField;
bunnyPosition.y = ret.bunnyY;
bunnyPosition.x = ret.bunnyX;
return;
} catch(e) {
console.log("Reading game field from URL parameter failed.");
console.log(e);
}
for (let y = 0; y < gridHeight; y++) {
gameField[y] = [];
for (let x = 0; x < gridWidth; x++) {
if (y === 0 || x === 0 || y === gridHeight - 1 || x === gridWidth - 1) {
// put wall around the field
gameField[y][x] = WALL;
} else {
// put random walls inside
gameField[y][x] = Math.random() < 0.2 ? WALL : EMPTY;
}
}
}
// place a bunny at a random position
bunnyPosition = {x: Math.floor(Math.random() * (gridWidth - 2) + 1), y: Math.floor(Math.random() * (gridHeight - 2) + 1)};
gameField[bunnyPosition.y][bunnyPosition.x] = BUNNY;
// place some boxes at random positions
for (let i = 0; i < 35; i++) {
let boxPosition = {x: Math.floor(Math.random() * (gridWidth - 2) + 1), y: Math.floor(Math.random() * (gridHeight - 2) + 1)};
gameField[boxPosition.y][boxPosition.x] = BOX;
let carrotPosition = {x: Math.floor(Math.random() * (gridWidth - 2) + 1), y: Math.floor(Math.random() * (gridHeight - 2) + 1)};
gameField[carrotPosition.y][carrotPosition.x] = CARROT;
}
}
async function applyGravity() {
drawGameField();
gravityInMotion = true;
while (true) {
let gravityWasApplied = false;
for (let y = gridHeight - 2; y >= 0; y--) {
for (let x = 0; x < gridWidth; x++) {
if (gameField[y][x] === BOX || gameField[y][x] === BUNNY) {
// check if the space below is empty
if (gameField[y + 1][x] === EMPTY || gameField[y + 1][x] === CARROT) {
// move the box or the bunny down
gravityWasApplied = true;
if (gameField[y][x] === BUNNY) {
if (gameField[y + 1][x] === CARROT) collectedCarrots++;
bunnyPosition.y++;
}
gameField[y + 1][x] = gameField[y][x];
gameField[y][x] = EMPTY;
}
}
}
}
if (!gravityWasApplied) {
gravityInMotion = false;
break;
} else {
await new Promise(r => setTimeout(r, 100));
drawGameField();
}
};
}
// draw the game field
function drawGameField() {
ctx.clearRect(-100, -100, canvas.width + 100, canvas.height + 100);
for (let y = 0; y < gridHeight; y++) {
for (let x = 0; x < gridWidth; x++) {
let emoji = gameField[y][x];
switch (emoji) {
case BUNNY:
if (lastMove === 1) {
// if moving right, bunny has to be mirrored (in the font it's facing left)
ctx.save(); // save the current state
ctx.scale(-1, 1); // flip the canvas
ctx.fillText(emoji, (-1.2 - x) * cellSize, y * cellSize);
ctx.restore(); // restore to the previous state
} else {
// if moving left, it has to be shifted a little bit
ctx.fillText(emoji, (-0.1 + x) * cellSize, y * cellSize);
}
break;
case BOX:
case WALL:
case PLACED_CARROT:
ctx.save();
ctx.filter = emoji === BOX ? 'brightness(80%)' : 'brightness(60%)';
ctx.fillText(emoji, x * cellSize, y * cellSize);
ctx.restore();
break;
case CARROT:
case DOOR:
ctx.fillText(emoji, x * cellSize, y * cellSize);
}
}
}
ctx.save();
ctx.fillStyle = "#e70";
ctx.font = `20px sans-serif`;
ctx.fillText(`Collected carrots: ${collectedCarrots}`, 10, 3);
ctx.restore();
}
function tryMove(dx, dy) {
if (dy === -1) {
// if carrot on top, jump means to only eat it
if (gameField[bunnyPosition.y - 1][bunnyPosition.x] === CARROT) {
collectedCarrots++;
gameField[bunnyPosition.y - 1][bunnyPosition.x] = EMPTY;
return;
}
// during jump, nothing can be on top of us
if (obstacle(gameField[bunnyPosition.y - 1][bunnyPosition.x])) return;
// we can only jump if there is obstacle in front
if (!obstacle(gameField[bunnyPosition.y][bunnyPosition.x + lastMove])) return;
}
let newX = bunnyPosition.x + dx;
let newY = bunnyPosition.y + dy;
if (gameField[newY][newX] === BOX) {
if (dy === -1) {
// no box pushing while jumping
return;
}
if (gameField[newY-1][newX] === BOX) {
// no pushing stacked boxes
return;
}
// the new position is a box
let beyondX = newX + dx;
if (gameField[newY][beyondX] === EMPTY) {
// the position beyond the box is empty
// move the box
gameField[newY][beyondX] = BOX;
gameField[newY][newX] = EMPTY;
} else {
// the box can't be moved
return;
}
}
// the new position is a wall
if (obstacle(gameField[newY][newX])) return;
if (gameField[newY][newX] === DOOR) alert("Congratulations");
// move the bunny
if (gameField[newY][newX] === CARROT) collectedCarrots++;
gameField[bunnyPosition.y][bunnyPosition.x] = EMPTY;
bunnyPosition.x = newX;
bunnyPosition.y = newY;
gameField[newY][newX] = BUNNY;
}
function placeCarrot() {
if (collectedCarrots > 0) {
// Determine the position to place the carrot
let placeX = bunnyPosition.x + lastMove;
let placeY = bunnyPosition.y;
if (gameField[placeY][placeX] === EMPTY) {
gameField[placeY][placeX] = PLACED_CARROT;
collectedCarrots--;
drawGameField();
}
}
}
// handle keyboard input
window.addEventListener('keydown', async function(e) {
if (gravityInMotion) return;
if (e.key !== 'u') {
let newHistory = JSON.stringify([gameField, bunnyPosition, lastMove, collectedCarrots]);
if (history.length == 0 || history[history.length-1] !== newHistory) history.push(newHistory);
}
switch (e.key) {
case 'ArrowUp':
tryMove(lastMove, -1);
break;
case 'ArrowLeft':
case 'ArrowRight':
let moveNow = e.key === 'ArrowLeft' ? -1 : 1;
if (lastMove === moveNow)
tryMove(moveNow, 0);
else
lastMove = moveNow;
break;
case ' ':
placeCarrot();
break;
case 'u':
if (history.length > 0) {
let oldHistory = history.pop();
[gameField, bunnyPosition, lastMove, collectedCarrots] = JSON.parse(oldHistory);
}
break;
}
await applyGravity();
});
// setup canvas
canvas.width = cellSize * gridWidth;
canvas.height = cellSize * gridHeight;
ctx.translate(-2, 4); // the wall in the font is not correctly centered
ctx.textBaseline = 'top';
ctx.font = `${cellSize - 3}px 'Noto Color Emoji', serif`;
function resizeCanvas() {
let scale = Math.min(
window.innerHeight / (cellSize * gridHeight),
window.innerWidth / (cellSize * gridWidth)
);
canvas.style.transform = `scale(${scale})`;
}
window.addEventListener('resize', resizeCanvas);
resizeCanvas();
// start the game
async function startGame() {
await initializeGameField();
await applyGravity();
}
startGame();