-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperFighters.js
1666 lines (1578 loc) · 37.3 KB
/
SuperFighters.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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
@title: SuperFighters
@author: retrooper
@tags: ['shooter', 'platformer', 'fighting', 'singleplayer']
@addedOn: 2024-08-15
*/
// Constants
const gameTitle = "SuperFighters";
const gameTitleColor = { y: 1, color: color`3` };
const startingLives = 5;
const movementDelay = 150;
const shootDelay = 1000;
const soundDelay = 100;
const jumpDuration = 280;
// Game data
let level = 0;
let inMenu = true;
let jumping = false;
let changingLevels = false;
let lives = startingLives;
let gravityDown = true;
let hasMagnet = false;
let hasGun = false;
// Internal variables
let lastParticleSound = Date.now();
let lastLeftMovement = Date.now();
let lastRightMovement = Date.now();
let lastShot = Date.now();
let lastPlayerShot = Date.now();
// Sprite data
const rightPunchingPlayer = "r";
const leftPunchingPlayer = "n";
const rightFacingMagnetPlayer = "H";
const leftFacingMagnetPlayer = "O";
const npcFacingRight = "P";
const npcFacingLeft = "L";
const npcEvilLeft = "k";
const npcEvilRight = "K";
const box = "b";
const bedrock = "B";
const ladder = "c";
const fireParticle = "f";
const bullet_left = "m";
const bullet_right = "M";
const heart = "h";
const magnet = "0";
const gun = "1";
const player_bullet_left = "2";
const player_bullet_right = "3";
const sky_light = "4";
const sky_dark = "5";
const sky_black = "6";
const leftFacingGunPlayer = "7";
const rightFacingGunPlayer = "8";
const rightFacingPlayer_A = "p";
const leftFacingPlayer_A = "l";
const rightFacingPlayer_B = "i";
const leftFacingPlayer_B = "y";
let RIGHT_FACING_PLACEHOLDER = rightFacingPlayer_A;
let LEFT_FACING_PLACEHOLDER = leftFacingPlayer_A;
let player = null;
let happySound = tune`3`;
let explosionSound = tune`
500: D5^500,
15500`;
let victoryMusic = tune`
140.8450704225352: D4~140.8450704225352 + D5/140.8450704225352 + A5/140.8450704225352 + F5/140.8450704225352,
140.8450704225352,
140.8450704225352: F4~140.8450704225352,
140.8450704225352,
140.8450704225352: A4~140.8450704225352,
140.8450704225352,
140.8450704225352: C5~140.8450704225352,
140.8450704225352,
140.8450704225352: E4~140.8450704225352 + E5/140.8450704225352 + G5/140.8450704225352 + B5/140.8450704225352,
140.8450704225352,
140.8450704225352: G4~140.8450704225352,
140.8450704225352,
140.8450704225352: B4~140.8450704225352,
140.8450704225352,
140.8450704225352: D5~140.8450704225352,
140.8450704225352,
140.8450704225352: F4~140.8450704225352 + F5/140.8450704225352 + A5/140.8450704225352 + C5/140.8450704225352,
140.8450704225352,
140.8450704225352: A4~140.8450704225352,
140.8450704225352,
140.8450704225352: C5~140.8450704225352,
140.8450704225352,
140.8450704225352: E5~140.8450704225352,
140.8450704225352,
140.8450704225352: G5~140.8450704225352 + G4/140.8450704225352 + E4/140.8450704225352 + C4/140.8450704225352,
140.8450704225352: E5~140.8450704225352,
140.8450704225352: C5~140.8450704225352,
140.8450704225352,
140.8450704225352: G4~140.8450704225352,
140.8450704225352,
140.8450704225352: C5~140.8450704225352 + C4/140.8450704225352 + G4/140.8450704225352 + E4/140.8450704225352 + E5/140.8450704225352,
140.8450704225352`;
let loadingMusic = victoryMusic;
let gameMusic = tune`
140.8450704225352: D5~140.8450704225352 + D4/140.8450704225352 + F4/140.8450704225352 + A4^140.8450704225352,
140.8450704225352,
140.8450704225352: F5~140.8450704225352 + A4^140.8450704225352,
140.8450704225352: A5~140.8450704225352,
140.8450704225352,
140.8450704225352: A4^140.8450704225352,
140.8450704225352: F5~140.8450704225352,
140.8450704225352,
140.8450704225352: C5~140.8450704225352 + C4/140.8450704225352 + E4/140.8450704225352 + G4^140.8450704225352,
140.8450704225352,
140.8450704225352: E5~140.8450704225352 + G4^140.8450704225352,
140.8450704225352: G5~140.8450704225352,
140.8450704225352,
140.8450704225352: G4^140.8450704225352,
140.8450704225352: E5~140.8450704225352,
140.8450704225352,
140.8450704225352: A4~140.8450704225352 + C4/140.8450704225352 + E4/140.8450704225352 + G4^140.8450704225352,
140.8450704225352,
140.8450704225352: C5~140.8450704225352 + G4^140.8450704225352,
140.8450704225352: E5~140.8450704225352,
140.8450704225352,
140.8450704225352: G4^140.8450704225352,
140.8450704225352: C5~140.8450704225352,
140.8450704225352,
140.8450704225352: G4~140.8450704225352 + D4/140.8450704225352 + D5/140.8450704225352 + A4^140.8450704225352,
140.8450704225352: B4~140.8450704225352,
140.8450704225352: B4~140.8450704225352,
140.8450704225352: D5~140.8450704225352,
140.8450704225352: B4~140.8450704225352 + D4/140.8450704225352 + F5/140.8450704225352 + G4^140.8450704225352,
140.8450704225352: G4~140.8450704225352,
140.8450704225352: G5~140.8450704225352,
140.8450704225352: G4~140.8450704225352`;
let playback = playTune(loadingMusic, Infinity);
setLegend(
[
rightFacingPlayer_A,
bitmap`
.....000000.....
....00CCCC00....
....00020020....
....0C00C00.....
..0000CCCC0000..
..000000000000..
..000022222000..
..000002002000..
..000CC0002CC0..
..000CC0000CC0..
...00000000000..
......00000.....
......000000....
.....000...00...
.....00....00...
.....000...000..`,
],
[
leftFacingPlayer_A,
bitmap`
.....000000.....
.....0CCCC00....
.....2002000....
.....00C00C0....
..0000CCCC0000..
..000000000000..
..000222220000..
..000200200000..
..000200000000..
..000000000000..
..CCC000000CCC..
.....00000......
....000000......
...00...000.....
...00....00.....
..000...000.....`,
],
[
rightFacingPlayer_B,
bitmap`
.....33888H.....
....99CCCCHH....
....9002002H....
....9C00C00.....
..0000CCCC0000..
..000000000000..
..000022222000..
..000002002000..
..000CC0002CC0..
..000CC0000CC0..
...00005555000..
.7....55555.....
606...005500....
606..000...00...
.....00....00...
.....000...000..`,
],
[
leftFacingPlayer_B,
bitmap`
.....H88833.....
....HHCCCC99....
....H2002009....
.....00C00C9....
..0000CCCC0000..
..000000000000..
..000222220000..
..000200200000..
..0CC2000CC000..
..0CC0000CC000..
..00055550000...
.....55555....7.
....005500...606
...000..000..606
...00....00.....
..000...000.....`,
],
[
rightFacingMagnetPlayer,
bitmap`
...000000.......
..00C0CC00......
..00020020....00
..0C00C00....030
0000CCCC000.0331
0000000000003331
0000222220003330
0000020020CC7330
000CC00020CC7730
000CC00000007771
.0000000000.0771
....00000....000
....000000......
...000...00.....
...00....00.....
...000...000....`,
],
[
leftFacingMagnetPlayer,
bitmap`
.......000000...
......00CC0C00..
00....02002000..
030....00C00C0..
1330.000CCCC0000
1333000000000000
0333000222220000
0337CC0200200000
0377CC02000CC000
17770000000CC000
1770.0000000000.
000....00000....
......000000....
.....000..00....
.....00....00...
....000...000...`,
],
[
rightFacingGunPlayer,
bitmap`
...000000.......
..00C0CC00......
..00020020......
..0C00C00.......
0000CCCC0..0000.
000000000..0LLL0
000022222000LLL0
0000020020CCL00.
000CC00020CC0...
000CC0000000....
.0000000000.....
....00000.......
....000000......
...000...00.....
...00....00.....
...000...000....`,
],
[
leftFacingGunPlayer,
bitmap`
.......000000...
......00CC0C00..
......02002000..
.......00C00C0..
.0000..0CCCC0000
0LLL0..000000000
0LLL000222220000
.00LCC0200200000
...0CC02000CC000
....0000000CC000
.....0000000000.
.......00000....
......000000....
.....00...000...
.....00....00...
....000...000...`,
],
[
rightPunchingPlayer,
bitmap`
.....000000.....
....00CCCC00....
....00030030....
....0C00C00.....
..000CCCCC00000.
..00000000000000
..00002222200CCC
..0000020000.CCC
..0CC0000000....
..0CC0000000....
..000000000.....
.....000000.....
.....0000000....
.....00...00....
.....00...00....
.....000..000...`,
],
[
leftPunchingPlayer,
bitmap`
.....000000.....
.....0CCCC00....
.....3003000....
.....00C00C0....
CC0000CCCCC0....
CC000000000000..
CC0002222200000.
.....0002000000.
.....0000000CC0.
.....0000000CC0.
.....000000.00..
.....000000.....
.....000000.....
......00.000....
......00..000...
.....000...00...`,
],
[
npcFacingRight,
bitmap`
....00000000....
....05555550....
....05555550....
....05225220....
000055555550....
055555555550....
055555555550000.
055000055555550.
055555055555550.
055555055555550.
000000055555550.
......055500000.
.....0555500....
.....0555550....
....05555550....
...0055555500...`,
],
[
npcFacingLeft,
bitmap`
....00000000....
....05555550....
....05555550....
....022522500...
....055555550...
00000555555500..
055505555555500.
055555555005550.
005555555055550.
..0055555055500.
...00555505000..
....00555050....
....0055550.....
....0555550.....
....05555550....
...0055555500...`,
],
[
npcEvilLeft,
bitmap`
....00000000....
....05555550....
....05555550....
....033533500...
....055555550...
97000555555500..
777505555555500.
075555555005550.
005555555055550.
..0055555055500.
...00555505000..
....00555050....
....0055550.....
....0555550.....
....05555550....
...0055555500...`,
],
[
npcEvilRight,
bitmap`
....00000000....
....05555550....
....05555550....
...005335330....
...055555550..00
..00555555500000
.005555555505736
.055500555555573
.055550555555500
.0055505555500..
..00050555500...
....05055500....
.....0555500....
.....0555550....
....05555550....
...0055555500...`,
],
[
box,
bitmap`
0000000000000000
0..............0
0.HHHHHHHHHHHH.0
0.H0000000000H.0
0.H0999999090H.0
0.H0999990900H.0
0.H0999909090H.0
0.H0999090990H.0
0.H0990909990H.0
0.H0909099990H.0
0.H0090999990H.0
0.H0909999990H.0
0.H0000000000H.0
0.HHHHHHHHHHHH.0
0..............0
0000000000000000`,
],
[
bedrock,
bitmap`
0000LLLL00LLL000
000LL000LLLLL100
0LLLLLLLLLLLL110
LLLLLLLLLLLLLL0L
LL00LLLLL00LL0LL
L0LLLLLLLLL00LLL
LLLLLLLLLLL0LLLL
LLLLLLLLLL0LLLLL
LLLLL0LLL0LLL0LL
LLL00LLL0LLLL0LL
LL0LLLLLLLLL0LLL
00LLLLLLLLLLLLLL
0LLLLLLL000LLLLL
LLLLL000LLLLLLLL
00LLLLLLLLLLLLL0
000LLLLLLLLLL000`,
],
[
ladder,
bitmap`
.LLLL......LLLL.
.LLLL222LLLLLLL.
.LLLLLLLLLLLL2L.
.L1LL......LL2L.
.L1LL......LL2L.
.L1LLLLLLLLLLLL.
.LLLLLLLLLLLLLL.
.LLLL......LLLL.
.LLLL......L2LL.
.LLLLLLLLLLL2LL.
.LLLLLLLLLLL2LL.
.LLLL......L2LL.
.LLLL......LLLL.
.LL2LLLLLLLLLLL.
.LL2LLLLLLLLLLL.
.LL2L......LLLL.`,
],
[
fireParticle,
bitmap`
..9....99....9..
....999999......
...99966699.....
..9966626699....
..9662266669....
..9996662669....
.996696662699...
.996269662669.99
..96622966699..9
..9666666969....
..99966666999...
...999966699....
.6..9999999.....
..6...9969......
...6....9.......
......6.........`,
],
[
bullet_left,
bitmap`
................
................
................
................
................
................
................
......96F6......
......F666......
................
................
................
................
................
................
................`,
],
[
bullet_right,
bitmap`
................
................
................
................
................
................
................
......6F69......
......666F......
................
................
................
................
................
................
................`,
],
[
heart,
bitmap`
.0000000.0000003
0332283303322280
0388888333888830
0888888338888830
0888888888888830
0888288888888880
0888288888888880
0388828888888880
0388888888888830
0338888888888830
.033888888888330
..03888888883330
..0338888883300.
.H.0388888830H..
....03388830....
H....000000...H.`,
],
[
sky_light,
bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`,
],
[
sky_dark,
bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`,
],
[
sky_black,
bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`,
],
[
magnet,
bitmap`
................
................
................
................
000000....00000.
0LLL00...0LLLL0.
0L0000...00000L0
003330...0777700
023320...0722270
022320...0727770
0232200.00772770
0233230.07777270
0333333077722270
0333333377777770
.03333337777770.
..000000000000..`,
],
[
gun,
bitmap`
..6.......6.....
..6.......6.....
................
............0...
...00000000000..
..0LLLLLLLLLL0..
.00LLLLLLLLLL0..
..00LL00000000..
66.0LL0.0.......
...0LL00.....66.
..0LLL0.........
..00000..6......
..........6.....
.6..............
6...............
................`,
],
[
player_bullet_left,
bitmap`
................
................
................
................
................
...77777...77...
..7.............
..7.96F6.666....
..7.F666...666..
..7.............
...77777...7....
................
................
................
................
................`,
],
[
player_bullet_right,
bitmap`
................
................
................
................
................
.77...77777.....
...........7....
..666.6F69.7....
666...666F.7....
...........7....
..7...77777.....
................
................
................
................
................`,
]
);
setBackground(sky_dark);
setSolids([
rightFacingPlayer_A,
leftFacingPlayer_A,
rightFacingPlayer_B,
leftFacingPlayer_B,
leftPunchingPlayer,
rightPunchingPlayer,
rightFacingMagnetPlayer,
leftFacingMagnetPlayer,
leftFacingGunPlayer,
rightFacingGunPlayer,
npcEvilLeft,
npcEvilRight,
npcFacingLeft,
npcFacingRight,
box,
bedrock,
]);
const levels = [
map`
...........
...........
...i.p.h...
...........
...........`,
map`
................
p......LP.......
..b..........cb.
..b..........cb.
..b..........cb.`,
map`
.............
.............
.............
.cB..........
pcB........P.`,
map`
...............
...............
..............B
.........b...BB
.......b...bbBB
p.bbbbbbb...LBB`,
map`
..............
..1...........
..............
..............
..............
p...........L.`,
map`
BBBBBBBBB
.........
......BBB
......B..
......B..
......B..
......B..
p.0...B..
BBBBBBB..`,
map`
p..............
bbbbbbbbbbbbbbb
...............
...............
...............`,
map`
......
......
......
......
......
.....b
......
....b.
......
...b..
p....B
....BB
BBBBBB`,
map`
....B......
p...BP.....
BBB.Bbb....
..B.1......
..B........
..BBBBBBBBB`,
map`
bbbbbbbbbb
.....bb...
p.0..bb...
bbbb....bb
...b....bb
...b....bb
...bbbbbbb`,
map`
BBBBBBBBBBBBBB
BBBBBBBBBBBBBB
..............
..............
..............
..............
1.............
b.p.......K.P.
BBBBBBBBBBBBBB`,
map`
...............
...............
...............
...............
...............
...............
...............
p..............
bbbbbbbbbbbbbbb`,
];
setMap(levels[level]);
setPushables({
[rightFacingPlayer_A]: [],
[leftFacingPlayer_A]: [],
[rightFacingPlayer_B]: [],
[leftFacingPlayer_B]: [],
});
// Add game title text as the player loads into the game.
addText(gameTitle, gameTitleColor);
addText("Select a character", { y: 4, color: color`2` });
//Add menu icons
addText("<__>", { y: 9, color: color`3` });
//Add menu text
addText("Press", { x: 2, y: 11, color: color`0` });
addText(" S ", { x: 7, y: 11, color: color`3` });
addText("to start!", { x: 10, y: 11, color: color`0` });
function copy(source, deep) {
var o, prop, type;
if (typeof source != "object" || source === null) {
// What do to with functions, throw an error?
o = source;
return o;
}
o = new source.constructor();
for (prop in source) {
if (source.hasOwnProperty(prop)) {
type = typeof source[prop];
if (deep && type == "object" && source[prop] !== null) {
o[prop] = copy(source[prop]);
} else {
o[prop] = source[prop];
}
}
}
return o;
}
/**
* Is the given type a pickable item?
*/
function isItem(type) {
return type == gun || type == magnet;
}
/**
* Is the given type a living entity? (meaning gravity applies to it)
* @param {*} type Entity type
* @returns true or false
*/
function isEntity(type) {
return isNPC(type) || isPlayer(type);
}
/**
* Is the given type an NPC?
* @param {*} type Entity type
* @returns true or false
*/
function isNPC(type) {
return (
type == npcFacingLeft ||
type == npcFacingRight ||
type == npcEvilLeft ||
type == npcEvilRight
);
}
/**
* Is the given type a player?
* @param {*} type Entity type
* @returns true or false
*/
function isPlayer(type) {
return (
type == leftFacingPlayer_A ||
type == rightFacingPlayer_A ||
type == rightFacingPlayer_B ||
type == leftFacingPlayer_B ||
type == leftPunchingPlayer ||
type == rightPunchingPlayer ||
type == rightFacingMagnetPlayer ||
type == leftFacingMagnetPlayer ||
type == leftFacingGunPlayer ||
type == rightFacingGunPlayer
);
}
/**
* Calculate the distance to the ground
* @param {*} player entity
* @returns distance to the ground
*/
function distanceToGround(player) {
let playerY = player.y;
let playerX = player.x;
// Check tiles below the player
for (let i = playerY + 1; i < height(); i++) {
const tiles = getTile(playerX, i);
if (tiles.length != 0) {
for (let tile in tiles) {
if (!isPlayer(tile.type) && !isItem(tile.type)) {
return i - playerY - 1;
}
}
}
}
return height() - playerY - 1;
}
/**
* Is the given entity on the ground?
* In other words, is their distance to the ground equal to zero/
* @param {*} player player
* @returns is the player on the ground
*/
function isOnGround(player) {
return distanceToGround(player) == 0;
}
/**
* Initiate an attack at a position
* @param {*} player player
* @param {*} entityX target X
* @param {*} entityY target Y
*/
function attackEntity(player, entityX, entityY) {
// Check sprite next to the player
getTile(entityX, entityY).forEach((tile) => {
// Confirm they are not destroying themselves!
if (isPlayer(tile.type)) return;
//Confirm they are not destroying an item!
if (isItem(tile.type)) return;
//If it bedrock, we cannot break it (easily)
if (tile.type == bedrock) {
spawnParticle(entityX, entityY, 0);
addSprite(entityX, entityY, bedrock);
} else {
// Destroy block next to them
clearTile(entityX, entityY);
spawnParticle(entityX, entityY, 0);
}
return;
});
}
/**
* Calculate the distance between two entities.
* @param {*} player player
* @param {*} entityX target entity X
* @param {*} entityY target entity Y
* @returns
*/
function distance(player, entityX, entityY) {
//d = sqrt(x^2 + y^2)
let playerX = player.x;
let playerY = player.y;