forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini_maze_and_puzzle.js
680 lines (603 loc) · 12.7 KB
/
mini_maze_and_puzzle.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
/*
@title: mini_maze_and_puzzle
@author: muhammad tsaqif mukhayyar
@tags: ['strategy']
@addedOn: 2022-09-16
Instructions:
Welcome to Find Home and Avoid Trap!!!
Here's the mission:
1. Find the door
2. Move block to its medium
How to play:
1. Beware of trap and lava
2. Use WASD to moving
*/
const createArray = (size) => [...Array(size).keys()];
const wait = (time) => new Promise((resolve) => setTimeout(resolve, time));
const player = "p";
const trap = "t";
const lava = "l";
const goal = "g";
const fake_goal = "f";
const wall = "w";
const barrier = "r";
const medium = "m";
const box = "b";
const decoration = "d";
const background = "a";
const button = "n";
const killables = [lava,trap];
let button_status = "unpressed";
let status = "play";
let jumps = 0;
let uBarrierY = 0;
let uBarrierX = 0;
let barrierY = 0;
let barrierX = 0;
const playerDead = [
player,
bitmap`
................
................
................
................
................
................
................
....00.0..0.....
.....000000.....
..44000000044...
...4003333040...
...0000000000...
...0........0...
................
................
................`,
];
const playerAlive = [
player,
bitmap`
................
................
................
....44...44.....
....0400040.....
....0000000.....
....0033330.....
....40000004....
....40000004....
....40000004....
....0000000.....
....0000000.....
....0.....0.....
....0.....0.....
................
................`,
];
const playerAliveLeft = [
player,
bitmap`
................
................
................
....44...44.....
....0400040.....
....0000000.....
....0333300.....
...40000004.....
...40000004.....
...40000004.....
....0000000.....
....0000000.....
....0.....0.....
....0.....0.....
................
................`,
];
const objects = [
[ trap, bitmap`
2222222222222222
2222222222222222
2442222222244422
2244888884448822
2224222444244442
2228444444422822
2228222282424442
2228444284242844
2444888884884822
2428224242224422
2428224242222422
2228222444222442
2228222482442842
2224444888844822
4442222222224222
2222222222222222`],
[ goal, bitmap`
2222222222222222
2264444444444422
2264444444444422
2264444222444422
2264442222244422
2264422222224422
2264422222224422
2264422222224422
2264422222224422
2264442222244422
2264442222244422
2264444444444422
2264444444444422
2266666666666622
2266666666666622
2222222222222222`],
[ fake_goal, bitmap`
2222222222222222
2264444444444422
2264444444444422
2264444222444422
2264422222224422
2264422222224422
2264422222224422
2264422222224422
2264422222224422
2264422222224422
2264442222244422
2264444444444422
2264444444444422
2266666666666622
2266666666666622
2222222222222222`],
[ wall, bitmap`
1111111111111111
1LLLLLLLLLLLLLL1
1LLLLLLLLLLLLLL1
1LLLLLLLLLLLLLL1
11111LLLLLL11111
LLLLL111111LLLLL
L11111111111111L
L11111111111111L
L11111111111111L
LLLLL111111LLLLL
11111LLLLLL11111
1LLLLLLLLLLLLLL1
1LLLLLLLLLLLLLL1
1LLLLLLLLLLLLLL1
1LLLLLLLLLLLLLL1
1111111111111111`],
[ lava, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
[ box, bitmap`
2222222222222222
2222222222222222
2222222222222222
2255555555555522
2255555555555522
2257777555555522
2257777555555522
2257777557777522
2255555557777522
2255555557777522
2255555555555522
2255555555555522
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
[ medium, bitmap`
2222222222222222
2222222222222222
0006666666666662
0026222222222062
0026222222222062
0026222222222062
0026222222222062
0226222222222062
0226222222222062
0226222222222062
0026222222222062
0026222222222062
0026222222222062
0026222222222062
0006666666666662
2222222222222222`],
[ button, bitmap`
................
................
................
................
................
................
................
................
................
................
................
................
................
................
...3333333333...
..LLLLLLLLLLLL..`],
[ barrier, bitmap`
LLLL333333333LLL
LLLL111111111LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111000011LLL
LLLL111111111LLL
LLLL111111111LLL
LLLL111111111LLL
LLLL111111111LLL
LLLL333333333LLL
LLLLLLLLLLLLLLLL`],
[ decoration, bitmap`
2222562222222222
2444465222222222
22L664L222225442
22LLLL422226L452
22222222222L4L22
2222222222264L22
2422222222226422
244L222222222222
2L6L222222222222
2LL6522222222222
2225222222222222
222222222LLL4222
222222225L64L222
222222444446L222
22222222256LL222
2222222222222222`],
[ background, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
]
setLegend(
playerAlive,
...objects
);
let level = 0;
const levels = [
map`
aaaaaaaaaaaaaad
aawwwwwwwwwwwag
taaaaaaaaaadwww
wwwwwwwwwwaddaa
dddddaaaaaaaaaa
dddaaaaaaawwwww
ddaaaaaawwwwwww
daaawwaaddwwwww
paaaaaaaadwwwww
wwwwllllllwwwww`,
map`
ddddaaapaaadddd
dddaaddaddaaddd
ddaadddadddaadd
daawwwdadwwwaad
wawwaaaaaaawwaw
wawwtttttttwwaw
wawtttttttttwaw
wawtttwwwtttwaw
wawwwwwgwwwwwaw
waaaaaaaaaaaaaw`,
map`
dddddaataaaaddd
dddttaabaaaaadd
ddtttaaaaaaaaad
wwtmaaawwwwwwaa
wwwwwwwwwmwwaaw
dddaaaadtbaaaww
ddaaaaaddaaawww
pawwwaaaaabaaam
wwwwwllwwwwwwww
wwwwwwwwwwwwwww`,
map`
ttwwwwtwwwwwwww
dddaaaaaaaaaagw
wdaawwwwtwwwwww
wwaaaaaaaaaaafw
wwwaawwwwwwwwww
taaaadddaaaaafw
wwwwaaddwwwwwww
aaaaaaadaaaaafw
aaawwaawwwwwwww
pwwwlllllllllll`,
map`
tttdaawmwaadttt
tdddabwawbadddt
ddaatmwbwmtaadd
waawwwwbwwwwaaw
wwaaadwbwdaaaww
mwwwaawbwaawwwm
bttwwawbwawwttb
aaaaaaaaaaaaaaa
wwwwwwwawwwwwww
maaaaabpbaaaaam`,
map`
gffffffffffffff
gffffffffffffff
gffffffffffffff
...............
wwwwww...w.....
....w.t...w....
.....nttttwwww.
.ww.bwtttt...a.
p.........r....
wwwwwwwwwwwwwww`,
];
const currentLevel = levels[level];
setMap(currentLevel);
setSolids([ player, wall, box, barrier ]);
setPushables({
[player]: [box],
[box]: [box]
});
const walk = tune`
500: a5~500,
15500`;
const win = tune`
96.7741935483871,
96.7741935483871: c4~96.7741935483871 + d4~96.7741935483871,
96.7741935483871: d4~96.7741935483871,
96.7741935483871: f4~96.7741935483871 + e4~96.7741935483871,
96.7741935483871: f4~96.7741935483871,
96.7741935483871: g4~96.7741935483871 + f4~96.7741935483871,
96.7741935483871: a4~96.7741935483871 + b4~96.7741935483871 + c5~96.7741935483871 + d5~96.7741935483871,
96.7741935483871: d5~96.7741935483871 + c5~96.7741935483871,
96.7741935483871: e5~96.7741935483871,
96.7741935483871: e5~96.7741935483871 + f5~96.7741935483871,
2129.032258064516`;
const fake = tune`
250: b5~250 + a5~250 + g5~250 + f5~250 + e5~250,
250: b5~250 + a5~250 + g5~250 + f5~250 + e5~250,
250: e5~250 + d5~250,
250: e5~250 + d5~250 + c5~250,
250: e5~250 + d5~250 + c5~250 + b4~250,
250: e5~250 + d5~250 + c5~250 + b4~250,
250: d5~250 + c5~250 + b4~250 + a4~250,
250: c5~250 + b4~250 + a4~250,
6000`;
// START - PLAYER MOVEMENT CONTROLS
onInput("s", () => {
playTune(walk);
if(jumps) jumps=0;
getFirst(player).y++;
});
onInput("d", () => {
playTune(walk);
setLegend(playerAlive, ...objects);
getFirst(player).x++;
});
onInput("w", () => {
playTune(walk);
if(jumps<0) {jumps=0; getFirst(player).y--;}
if(jumps) return;
jumps++;
jump().then(async () => {
jumps--;
});
});
onInput("a", () => {
playTune(walk);
setLegend(playerAliveLeft, ...objects);
getFirst(player).x--;
});
const jump = async () => {
await createArray(3).reduce(async (promise) => {
await promise;
getFirst(player).y--;
checkIfKillablesWereTouched()
checkDoorArrived()
await wait(100);
}, Promise.resolve());
await resetGravity();
};
const resetGravity = async () => {
await createArray(3).reduce(async (promise) => {
await promise;
getFirst(player).y++;
await wait(130);
}, Promise.resolve());
};
// gravity
setInterval(() => {
checkIfKillablesWereTouched()
checkDoorArrived()
if (jumps || getFirst(player).y === 10) return;
getFirst(player).y++;
}, 130);
const shake = () => {
if (typeof document === "undefined") return;
const gameCanvasContainer = document.querySelector(".game-canvas-container");
gameCanvasContainer.classList.add("shake");
setTimeout(() => {
gameCanvasContainer.classList.remove("shake");
}, 200);
};
const killPlayer = () => {
addText("Oops!", {
y: 4,
color: color`3`,
});
addText("killed yourself :(", {
y: 6,
color: color`3`,
});
shake();
setLegend(playerDead, ...objects);
setTimeout(() => {
setLegend(playerAlive, ...objects);
setMap(levels[level]);
switch(level){
case 0:
getFirst(player).y = 10;
getFirst(player).x = 0;
break;
case 1:
getFirst(player).y = 0;
getFirst(player).x = 7;
break;
case 2:
getFirst(player).y = 10;
getFirst(player).x = 0;
break;
case 3:
getFirst(player).y = 10;
getFirst(player).x = 0;
break;
case 4:
getFirst(player).y = 10;
getFirst(player).x = 7;
break;
case 5:
getFirst(player).y = 10;
getFirst(player).x = 0;
break;
}
clearText();
}, 400);
}
const checkIfKillablesWereTouched = () => {
const { y: playerY, x: playerX } = getFirst(player);
const playerTouchedKillable = getTile(playerX, playerY).some(({ type }) =>
killables.includes(type)
);
if (playerTouchedKillable) killPlayer();
}
const checkDoorArrived = () => {
const playerArrived = tilesWith(player,goal).length;
const playerArrivedFake = tilesWith(player,fake_goal).length;
if(playerArrivedFake)
{
playTune(fake);
addText("Wrong Door!", { y: 4, color: color`5` });
setTimeout(() => {
clearText();
}, 500);
}
if (playerArrived) {
// increase the current level number
level = level + 1;
const currentLevel = levels[level];
// make sure the level exists and if so set the map
if (currentLevel !== undefined) {
setMap(currentLevel);
} else {
if(status=="win") return;
playTune(win);
addText("you win!", { y: 4, color: color`6` });
setTimeout(() => {
clearText();
addText("press k to", { y: 4, color: color`0` });
addText("restart the game!", { y: 6, color: color`0` });
}, 800);
status="win"
}
}
}
// END - PLAYER MOVEMENT CONTROLS
onInput("j", () => {
const currentLevel = levels[level];
if (currentLevel !== undefined) {
clearText("");
setMap(currentLevel);
clearText("");
}
});
onInput("k", () => {
const currentLevel = levels[0];
// console.log(levels[level]);
if (currentLevel !== undefined) {
clearText("");
setMap(currentLevel);
}
});
afterInput(() => {
checkIfKillablesWereTouched();
// count the number of tiles with goals
const targetNumber = tilesWith(medium).length;
// count the number of tiles with goals and boxes
const numberCovered = tilesWith(medium, box).length;
const playerArrived = tilesWith(player,goal).length;
const playerArrivedFake = tilesWith(player,fake_goal).length;
const buttonPressed = tilesWith(button, box).length;
if(getFirst(barrier)){
barrierX = getFirst(barrier).x;
barrierY = getFirst(barrier).y;
}
if(barrierY) {
uBarrierY=barrierY;
uBarrierX=barrierX;
}
if(buttonPressed)
{
if(button_status == "pressed") return;
clearTile(barrierX, barrierY);
button_status="pressed";
} else {
const currentLevelCheck = levels[level];
if(currentLevelCheck == levels[5]){
// console.log("test")
addSprite(uBarrierX, uBarrierY, barrier);
button_status="unpressed";
}
}
if(playerArrivedFake)
{
playTune(fake);
addText("Wrong Door!", { y: 4, color: color`5` });
setTimeout(() => {
clearText();
}, 700);
}
if (playerArrived || (targetNumber == numberCovered && targetNumber != 0 && numberCovered !=0)) {
// increase the current level number
level = level + 1;
const currentLevel = levels[level];
// make sure the level exists and if so set the map
if (currentLevel !== undefined) {
setMap(currentLevel);
} else {
if(status=="win") return;
playTune(win);
addText("you win!", { y: 4, color: color`6` });
setTimeout(() => {
clearText();
addText("press k to", { y: 4, color: color`0` });
addText("restart the game!", { y: 6, color: color`0` });
}, 800);
status = "win";
}
}
});