-
Notifications
You must be signed in to change notification settings - Fork 2
/
bundle.js
701 lines (686 loc) · 20.6 KB
/
bundle.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
// lib/vector.js
var require_vector = __commonJS({
"lib/vector.js"(exports, module2) {
var Vec = class {
constructor(x, y) {
this.x = x;
this.y = y;
}
plus(other) {
return new Vec(this.x + other.x, this.y + other.y);
}
times(factor) {
return new Vec(this.x * factor, this.y * factor);
}
};
module2.exports = Vec;
}
});
// lib/player.js
var require_player = __commonJS({
"lib/player.js"(exports, module2) {
var Vec = require_vector();
var Player = class {
constructor(pos, velocity) {
this.pos = pos;
this.velocity = velocity;
this.speed = 6;
}
get type() {
return "player";
}
static create(pos) {
return new Player(pos, new Vec(0, 0));
}
update(time, state, keys) {
let xVelocity = 0;
if (keys.ArrowLeft)
xVelocity -= this.speed;
if (keys.ArrowRight)
xVelocity += this.speed;
let yVelocity = 0;
if (keys.ArrowUp)
yVelocity -= this.speed;
if (keys.ArrowDown)
yVelocity += this.speed;
let pos = this.pos;
let movedX = pos.plus(new Vec(xVelocity * time, 0));
if (!state.level.touchesWall(movedX, this.size)) {
pos = movedX;
}
let movedY = pos.plus(new Vec(0, yVelocity * time));
if (!state.level.touchesWall(movedY, this.size)) {
pos = movedY;
}
return new Player(pos, new Vec(xVelocity, yVelocity));
}
};
Player.prototype.size = new Vec(1, 1);
module2.exports = Player;
}
});
// lib/state.js
var require_state = __commonJS({
"lib/state.js"(exports, module2) {
var State = class {
constructor(level2, actors, status = "playing", miniGameStatus = null) {
this.level = level2;
this.actors = actors;
this.status = status;
this.miniGameStatus = miniGameStatus;
}
static start(level2) {
return new State(level2, level2.startActors);
}
get player() {
return this.actors.find((a) => a.type == "player");
}
get cookieJars() {
return this.actors.filter((actor) => actor.type == "cookieJar");
}
get cookieCount() {
return this.cookieJars.map((cj) => cj.cookies).reduce((a, b) => a + b);
}
update = function(time, keys) {
let newState = new State(
this.level,
this.actors,
this.status,
this.miniGameStatus
);
newState = this.#updatePlayer(newState, time, keys);
newState = this.#resetMiniGameStatus(newState);
newState = this.#checkCollisions(newState);
return newState;
};
#checkCollisions(state) {
for (let actor of state.actors) {
if (actor != state.player && this.#checkOverlap(actor, state.player)) {
return actor.collide(state);
}
}
return state;
}
#updatePlayer(state, time, keys) {
if (state.miniGameStatus != "playing") {
state.actors = state.actors.map((actor) => {
return actor.type == "player" ? actor.update(time, state, keys) : actor;
});
}
return state;
}
#resetMiniGameStatus = (state) => {
if (state.cookieJars.every((cj) => !this.#checkOverlap(cj, state.player)) && (state.miniGameStatus == "won" || state.miniGameStatus == "lost")) {
state.miniGameStatus = null;
}
return state;
};
#checkOverlap = function(actor1, actor2) {
return actor1.pos.x + actor1.size.x > actor2.pos.x && actor1.pos.x < actor2.pos.x + actor2.size.x && actor1.pos.y + actor1.size.y > actor2.pos.y && actor1.pos.y < actor2.pos.y + actor2.size.y;
};
};
module2.exports = State;
}
});
// lib/cookieMonster.js
var require_cookieMonster = __commonJS({
"lib/cookieMonster.js"(exports, module2) {
var Vec = require_vector();
var State = require_state();
var CookieMonster = class {
constructor(pos, speed) {
this.pos = pos;
this.speed = speed;
}
get type() {
return "cookieMonster";
}
static create(pos) {
return new CookieMonster(pos, new Vec(0, 0));
}
collide(state) {
let newState = new State(
state.level,
state.actors,
state.status,
state.miniGameStatus
);
newState = this.#checkCookies(newState);
return newState;
}
#checkCookies(state) {
if (state.cookieJars.every((cj) => cj.cookies < 1)) {
this.#speak("Give me cookies!");
} else if (state.cookieJars.some((cj) => cj.cookies < 1)) {
this.#speak(
`Give me more cookies! You have: ${"\u{1F36A}".repeat(state.cookieCount)}`
);
} else {
this.#speak("Mmmm delicious! Now, escape before it's too late!");
state.level = state.level.switch("pass");
}
return state;
}
#speak(message) {
document.getElementById("text").textContent = message;
}
};
CookieMonster.prototype.size = new Vec(1, 1);
module2.exports = CookieMonster;
}
});
// lib/cookieJar.js
var require_cookieJar = __commonJS({
"lib/cookieJar.js"(exports, module2) {
var Vec = require_vector();
var State = require_state();
var CookieJar = class {
constructor(pos, speed, miniGameLocator, storedState = null, cookies = 0) {
this.pos = pos;
this.speed = speed;
this.MiniGameConsturctor = miniGameLocator.getGame();
this.storedState = storedState;
this.cookies = cookies;
}
get type() {
return "cookieJar";
}
static create(pos, miniGameLocator) {
return new CookieJar(pos, new Vec(0, 0), miniGameLocator);
}
collide(state) {
if (state.miniGameStatus == null) {
this.storedState = new State(
state.level,
state.actors,
state.status,
"playing"
);
const miniGame = new this.MiniGameConsturctor();
miniGame.run(this.miniGameCallback);
}
return this.storedState;
}
miniGameCallback = (result) => {
if (result === "lost") {
this.storedState.miniGameStatus = "lost";
} else if (result === "won") {
this.cookies += 1;
this.storedState.miniGameStatus = "won";
}
};
};
CookieJar.prototype.size = new Vec(1, 1);
module2.exports = CookieJar;
}
});
// lib/exit.js
var require_exit = __commonJS({
"lib/exit.js"(exports, module2) {
var Vec = require_vector();
var State = require_state();
var Exit = class {
constructor(pos, speed) {
this.pos = pos;
this.speed = speed;
}
get type() {
return "exit";
}
static create(pos) {
return new Exit(pos, new Vec(0, 0));
}
collide(state) {
const newState = new State(
state.level,
state.actors,
state.status,
state.miniGameStatus
);
this.#clearSpeech();
newState.level = newState.level.switch("escape");
newState.actors = newState.level.startActors;
newState.status = "won";
return newState;
}
#clearSpeech(message) {
document.getElementById("text").textContent = "";
}
};
Exit.prototype.size = new Vec(1, 1);
module2.exports = Exit;
}
});
// lib/levelTypes.js
var require_levelTypes = __commonJS({
"lib/levelTypes.js"(exports, module2) {
var Player = require_player();
var CookieMonster = require_cookieMonster();
var CookieJar = require_cookieJar();
var Exit = require_exit();
var levelTypes = {
".": "empty",
"#": "wall",
"M": CookieMonster,
"@": Player,
"*": Exit,
"!": CookieJar
};
module2.exports = levelTypes;
}
});
// lib/level.js
var require_level = __commonJS({
"lib/level.js"(exports, module2) {
var Vec = require_vector();
var levelTypes = require_levelTypes();
var Level2 = class {
constructor(plans, MiniGameLocator2) {
this.plans = plans;
this.startActors = [];
this.MiniGameLocator = new MiniGameLocator2();
this.#makeMatrix(this.plans["start"]);
}
switch = (stage) => {
this.startActors = [];
this.#makeMatrix(this.plans[stage]);
return this;
};
#makeMatrix = (levelPlan) => {
this.rows = levelPlan.trim().split("\n").map((l) => [...l]);
this.height = this.rows.length;
this.width = this.rows[0].length;
this.#mapActors();
};
#mapActors = () => {
this.rows = this.rows.map((row, y) => {
return row.map((index, x) => {
let type = levelTypes[index];
if (this.#isBackground(type))
return type;
this.startActors.push(type.create(new Vec(x, y), this.MiniGameLocator));
return "empty";
});
});
};
#isBackground = (type) => {
return typeof type == "string";
};
touchesWall = function(pos, size) {
let xStart = Math.floor(pos.x);
let xEnd = Math.ceil(pos.x + size.x);
let yStart = Math.floor(pos.y);
let yEnd = Math.ceil(pos.y + size.y);
for (let y = yStart; y < yEnd; y++) {
for (let x = xStart; x < xEnd; x++) {
let isOutside = x < 0 || x >= this.width || y < 0 || y >= this.height;
let here = isOutside ? "wall" : this.rows[y][x];
if (here == "wall")
return true;
}
}
return false;
};
};
module2.exports = Level2;
}
});
// lib/levelPlans.js
var require_levelPlans = __commonJS({
"lib/levelPlans.js"(exports, module2) {
var start = `
..................
.................!
..............####
..########....#..*
..#......#....#...
..#......#......#.
..#...#..#....M.#.
..#...#.........#.
.@#..!#.......#...`;
var pass = `
..................
..................
..............####
..########....#...
..#......#....#...
..#......#........
..#...#..#........
..#...#...........
..#...#.......#...`;
var escape = `
..................
..................
..................
.................@
..................
..................
..................
..................
..................`;
var levelPlans2 = {
"start": start,
"pass": pass,
"escape": escape
};
module2.exports = levelPlans2;
}
});
// lib/canvasDisplay.js
var require_canvasDisplay = __commonJS({
"lib/canvasDisplay.js"(exports, module2) {
var CanvasDisplay2 = class {
constructor(parent, level2) {
this.scale = 40;
this.parent = parent;
this.addCanvas(level2);
this.#setSprites();
this.drawBackground(level2);
}
addCanvas(level2) {
this.canvas = document.createElement("canvas");
this.canvas.id = "main-game";
this.canvas.width = level2.width * this.scale;
this.canvas.height = level2.height * this.scale;
this.parent.appendChild(this.canvas);
this.cx = this.canvas.getContext("2d");
}
syncState(state) {
if (state.miniGameStatus == "playing") {
this.canvas.style.display = "none";
} else {
this.canvas.style.display = "inline";
this.resetDisplay(state.status);
this.drawBackground(state.level);
this.drawActors(state.actors);
}
}
resetDisplay = function(status) {
let pattern = this.cx.createPattern(
this.backgroundSprites[status],
"repeat"
);
this.cx.fillStyle = pattern;
this.cx.fillRect(0, 0, this.canvas.width, this.canvas.height);
};
drawBackground = function(level2) {
for (let y = 0; y < level2.height; y++) {
for (let x = 0; x < level2.width; x++) {
let tile = level2.rows[y][x];
if (tile == "empty")
continue;
this.cx.drawImage(
this.backgroundSprites[tile],
x * this.scale,
y * this.scale,
this.scale,
this.scale
);
}
}
};
drawActors = function(actors) {
for (let actor of actors) {
let width = actor.size.x * this.scale;
let height = actor.size.y * this.scale;
let x = actor.pos.x * this.scale;
let y = actor.pos.y * this.scale;
this.cx.drawImage(this.actorSprites[actor.type], x, y, width, height);
}
};
#setSprites = () => {
const cookieJarSprite = document.createElement("img");
cookieJarSprite.src = "../img/cookieJar.png";
const playerSprite = document.createElement("img");
playerSprite.src = "../img/player.png";
const cookieMonsterSprite = document.createElement("img");
cookieMonsterSprite.src = "img/cookieMonster2.png";
const exitSprite = document.createElement("img");
exitSprite.src = "img/diamond.png";
const backgroundSprite = document.createElement("img");
backgroundSprite.src = "img/background-tile.jpeg";
const wonBackgroundSprite = document.createElement("img");
wonBackgroundSprite.src = "img/clouds.jpeg";
const wallSprite = document.createElement("img");
wallSprite.src = "img/wall.png";
this.backgroundSprites = {
"playing": backgroundSprite,
"won": wonBackgroundSprite,
"wall": wallSprite
};
this.actorSprites = {
"cookieMonster": cookieMonsterSprite,
"player": playerSprite,
"exit": exitSprite,
"cookieJar": cookieJarSprite
};
};
};
module2.exports = CanvasDisplay2;
}
});
// lib/jumpGame.js
var require_jumpGame = __commonJS({
"lib/jumpGame.js"(exports, module2) {
var JumpGame = class {
constructor(cookieJarId) {
this.character = document.getElementById("character");
this.block = document.getElementById("block");
this.jumpCounter = 0;
this.callback;
this.started = false;
}
run = (callback) => {
window.addEventListener("keydown", this.#keysEventListener);
this.#checkIfDead();
this.#displayMessage(
"Jump over the meteorites! Press [Enter] to start and [Space Bar] to jump"
);
this.callback = callback;
document.getElementById("block_jump_game_container").style.display = "inline";
};
#keysEventListener = (event) => {
if (event.key === "Enter") {
event.preventDefault();
this.#start();
}
if (event.key === " ") {
event.preventDefault();
this.#jump();
}
};
#start = () => {
this.started = true;
this.block.style.animation = "block 1s infinite linear";
};
#checkIfDead = () => {
this.setInterval = setInterval(() => {
var characterTop = parseInt(
window.getComputedStyle(this.character).getPropertyValue("top")
);
var blockLeft = parseInt(
window.getComputedStyle(this.block).getPropertyValue("left")
);
if (blockLeft < 20 && blockLeft > 0 && characterTop >= 290) {
this.#lose();
}
}, 10);
};
#jump = () => {
if (this.character.classList != "animate") {
this.character.classList.add("animate");
if (this.started)
this.#increment();
if (this.jumpCounter > 4)
this.#win();
}
setTimeout(function() {
this.character.classList.remove("animate");
}, 500);
};
#increment = () => {
this.jumpCounter += 1;
this.#displayMessage(`Almost there... ${this.jumpCounter}`);
};
#win = () => {
setTimeout(() => {
this.#end();
this.#displayMessage("You won! \u{1F36A}");
this.callback("won");
}, 500);
};
#lose = () => {
this.#end();
this.#displayMessage("You lost!");
this.callback("lost");
};
#end = () => {
document.getElementById("block_jump_game_container").style.display = "none";
this.jumpCounter = 0;
clearInterval(this.setInterval);
window.removeEventListener("keydown", this.#keysEventListener);
this.block.style.animation = "none";
};
#displayMessage = (message) => {
document.getElementById("text").textContent = message;
};
};
module2.exports = JumpGame;
}
});
// lib/matrixGame.js
var require_matrixGame = __commonJS({
"lib/matrixGame.js"(exports, module2) {
var MatrixGame = class {
constructor() {
this.callback;
}
run = (callback) => {
this.callback = callback;
this.displayMessage(
"Make your choice. Press [R] for the red pill, [B] for the blue pill."
);
this.imageOne = this.addImage("img/matrix.png", "matrix");
this.imageTwo = this.addImage("img/pills.png", "pills");
window.addEventListener("keydown", this.keyHandlerFunction);
};
end = () => {
this.imageOne.remove();
this.imageTwo.remove();
window.removeEventListener("keydown", this.keyHandlerFunction);
};
keyHandlerFunction = (event) => {
if (event.key == "r") {
this.end();
this.displayMessage("You won! \u{1F36A}");
this.callback("won");
}
if (event.key == "b") {
this.end();
this.displayMessage("You lost!");
this.callback("lost");
}
};
displayMessage = (message) => {
document.getElementById("text").textContent = message;
};
addImage = (url, id) => {
const image = document.createElement("img");
image.src = url;
image.id = id;
document.body.appendChild(image);
return image;
};
};
module2.exports = MatrixGame;
}
});
// lib/miniGameLocator.js
var require_miniGameLocator = __commonJS({
"lib/miniGameLocator.js"(exports, module2) {
var JumpGame = require_jumpGame();
var MatrixGame = require_matrixGame();
var MiniGameLocator2 = class {
constructor() {
this.games = [JumpGame, MatrixGame];
}
getGame() {
const index = this.#getRandomIndex();
const game2 = this.games[index];
this.games.splice(index, 1);
return game2;
}
#getRandomIndex() {
if (this.games.length < 1) {
throw Error("No more mini-games left to assign!");
}
return Math.floor(Math.random() * this.games.length);
}
};
module2.exports = MiniGameLocator2;
}
});
// lib/game.js
var require_game = __commonJS({
"lib/game.js"(exports, module2) {
var State = require_state();
var Game2 = class {
constructor(level2, Display) {
this.level = level2;
this.display = new Display(document.body, level2);
this.state = State.start(level2);
this.arrowKeysTracker = this.#trackKeys([
"ArrowLeft",
"ArrowRight",
"ArrowUp",
"ArrowDown"
]);
}
run() {
this.#runAnimation();
}
#runAnimation() {
let lastTime = null;
const animationCallback = (time) => {
if (lastTime != null) {
let timeStep = Math.min(time - lastTime, 100) / 1e3;
this.#updateFrame(timeStep);
}
lastTime = time;
requestAnimationFrame(animationCallback);
};
requestAnimationFrame(animationCallback);
}
#updateFrame = (time) => {
this.state = this.state.update(time, this.arrowKeysTracker);
this.display.syncState(this.state);
};
#trackKeys(keys) {
let down = /* @__PURE__ */ Object.create(null);
function track(event) {
if (keys.includes(event.key)) {
down[event.key] = event.type == "keydown";
event.preventDefault();
}
}
window.addEventListener("keydown", track);
window.addEventListener("keyup", track);
return down;
}
};
module2.exports = Game2;
}
});
// index.js
var Level = require_level();
var levelPlans = require_levelPlans();
var CanvasDisplay = require_canvasDisplay();
var MiniGameLocator = require_miniGameLocator();
var Game = require_game();
var level = new Level(levelPlans, MiniGameLocator);
var game = new Game(level, CanvasDisplay);
game.run();