-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.js
1007 lines (864 loc) · 31.4 KB
/
field.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
'use strict';
import { stateEnum, difficultyEnum, colorEnum } from './enums.js';
import { Dirt, Diamond, Gem, Rock, Exit, Dozer, Explosion, Grenade, DroppedGrenade, Bug, spriteEnum, classArray } from './objects.js';
import { EmeraldHunt } from './hunt.js';
class Field {
// Canvas context for drawing
#ctx;
// Horizontal field size
#fieldX;
// Vertical field size
#fieldY;
// Game difficulty enum
#difficulty;
// Field data array
#grid;
// Dozer instance
#dozer;
// Boolean to flag if a new game has just started
#fieldInitialising;
// Amount of score accumulated in this game
#gameScore = 0;
// Amount of score available at the start of the game
#availableScore = 0;
// Target score to unlock the exit
#targetScore = 0;
// Callback to the Hunt object playerDying function
#playerDyingCallback;
// Callback to the Hunt object playerWon function
#playerWonCallback;
// Audio context
#audioContext;
// Audio gain (volume)
#gainNode;
// Multidimensional array containing distribution of objects, indexed by difficulty enum
#difficultyDistribution = {};
// Array of score multiplication factors (to determine target score from total available field score) indexed by difficulty enum
#difficultyScoreFactor = {};
// Maximum gain to set audio to
#audioLevel = EmeraldHunt.STARTVOLUME;
// Flag to track whether audio is currently playing, to ensure only one sound plays at a time
#audioPlaying = false;
constructor(c, diff, dyingCallback, wonCallback) {
this.#ctx = c;
this.#fieldX = EmeraldHunt.DEFAULTFIELDX;
this.#fieldY = EmeraldHunt.DEFAULTFIELDY;
this.#difficulty = diff;
this.#gameScore = 0;
this.#playerDyingCallback = dyingCallback;
this.#playerWonCallback = wonCallback;
this.#audioContext = new (window.AudioContext || window.webkitAudioContext);
this.#gainNode = this.#audioContext.createGain();
this.#gainNode.gain.setValueAtTime(this.#audioLevel, this.#audioContext.currentTime);
this.#gainNode.connect(this.#audioContext.destination);
// Types are stored in the same array order as the sprites
/*
BLANK: 0,
DIRT: 1,
ROCK: 2,
EMERALD: 3,
BRICK: 4,
BOMB: 5,
EXIT: 6,
DOZER: 7,
COBBLE: 8,
BUG: 9,
DIAMOND: 10,
SLIME: 11,
EXPLOSION: 12,
GRENADE: 13,
NOTUSED: 14,
ALTDOZER: 15
*/
// Numbers are for a default 40x20 field (800 tiles).
// Actual items will be scaled for field size.
// Set difficulty values
this.#difficultyDistribution[difficultyEnum.EASY] = [ 0, 173, 125, 219, 80, 0, 0, 0, 0, 0, 0, 0, 0, 26, 0, 0];
// [ 0, 185, 126, 215, 78, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0];
this.#difficultyDistribution[difficultyEnum.MEDIUM] = [ 0, 153, 105, 191, 101, 18, 0, 0, 42, 0, 0, 0, 0, 24, 0, 0];
// [ 0, 197, 101, 173, 83, 30, 0, 0, 34, 0, 0, 0, 0, 18, 0, 0];
this.#difficultyDistribution[difficultyEnum.HARD] = [ 0, 199, 120, 140, 84, 25, 0, 0, 65, 0, 49, 0, 0, 12, 0, 0];
// [ 0, 173, 103, 133, 95, 40, 0, 0, 67, 0, 53, 0, 0, 18, 0, 0];
this.#difficultyDistribution[difficultyEnum.HARDER] = [ 0, 128, 115, 0, 68, 31, 0, 0, 128, 0, 201, 0, 0, 20, 0, 0];
// [ 0, 134, 123, 0, 60, 33, 0, 0, 100, 0, 208, 0, 0, 21, 0, 0];
this.#difficultyDistribution[difficultyEnum.HARDEST] = [ 0, 98, 122, 55, 95, 27, 0, 0, 93, 24, 111, 0, 0, 19, 0, 0];
// [ 0, 101, 120, 54, 87, 32, 0, 0, 107, 24, 104, 0, 0, 17, 0, 0];
this.#difficultyScoreFactor[difficultyEnum.EASY] = 0.90;
this.#difficultyScoreFactor[difficultyEnum.MEDIUM] = 0.915;
this.#difficultyScoreFactor[difficultyEnum.HARD] = 0.945;
this.#difficultyScoreFactor[difficultyEnum.HARDER] = 0.96;
this.#difficultyScoreFactor[difficultyEnum.HARDEST] = 0.96;
this.initField();
this.#fieldInitialising = true;
}
setVolume(n) {
this.#audioLevel = n;
this.#gainNode.gain.setValueAtTime(this.#audioLevel, this.#audioContext.currentTime);
}
initField() {
// Move to storing the field in a 1D array
this.#grid = new Array(this.#fieldX * this.#fieldY).fill(spriteEnum.BLANK);
// If this field is being used for a menu background, leave it blank
// It will self-populate
if (this.#difficulty === stateEnum.MENU)
return;
let requiredTypes = [
spriteEnum.DIRT,
spriteEnum.ROCK,
spriteEnum.EMERALD,
spriteEnum.BRICK,
spriteEnum.BOMB,
spriteEnum.COBBLE,
spriteEnum.BUG,
spriteEnum.DIAMOND,
spriteEnum.GRENADE
];
requiredTypes.forEach(this.populateFieldWithType.bind(this));
}
playGemFall() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 24ms, 26 cycles, 1,083Hz for 24ms
Second tone, 28ms, 25 cycles: 892Hz for 28ms
Third tone, 27ms, 19 cycles: 700Hz for 27ms
Fourth tone, 80ms, 8 cycles: 100Hz for 80ms
Fifth tone, 2ms, 1/2 cycle: 250Hz for 4ms
Total time: 163ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(1083, this.#audioContext.currentTime);
osc.frequency.setValueAtTime(892, this.#audioContext.currentTime + 0.024);
osc.frequency.setValueAtTime(700, this.#audioContext.currentTime + 0.052);
osc.frequency.setValueAtTime(100, this.#audioContext.currentTime + 0.079);
osc.frequency.setValueAtTime(250, this.#audioContext.currentTime + 0.159);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 163);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
this.#audioPlaying = false;
}
}
playExplosion() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 22ms, 13 cycles: 590Hz for 23ms
Second tone, 53ms, 17 cycles: 320Hz for 54ms
Third tone, 25ms, 2 cycles: 80Hz for 33ms
Fourth tone, 17ms, 2 cycles: 117Hz for 25ms
Off for 17ms
Fifth tone, 17ms, 1/2 cycle: 30Hz for 33ms
Total time: 185ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(590, this.#audioContext.currentTime);
osc.frequency.setValueAtTime(320, this.#audioContext.currentTime + 0.023);
osc.frequency.setValueAtTime(80, this.#audioContext.currentTime + 0.077);
osc.frequency.setValueAtTime(117, this.#audioContext.currentTime + 0.110);
this.#gainNode.gain.setValueAtTime(0, this.#audioContext.currentTime + 0.135);
osc.frequency.setValueAtTime(30, this.#audioContext.currentTime + 0.140);
this.#gainNode.gain.setValueAtTime(this.#audioLevel, this.#audioContext.currentTime + 0.152);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 185);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
this.#audioPlaying = false;
}
}
playDiamondCrushed() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 2ms, 1 cycle, 500Hz for 2ms
Second tone, 26ms, 63 cycles: 2,400Hz for 26ms
Third tone, 27ms, 22 cycles: 815Hz for 27ms
Fourth tone, 27ms, 41 cycles: 1,500Hz for 28ms
Fifth tone, 26ms, 21 cycles: 800Hz for 27ms
Sixth tone, 53ms, 64 cycles: 1,200Hz for 54ms
Total time: 164ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(500, this.#audioContext.currentTime);
osc.frequency.setValueAtTime(2400, this.#audioContext.currentTime + 0.002);
osc.frequency.setValueAtTime(815, this.#audioContext.currentTime + 0.028);
osc.frequency.setValueAtTime(1500, this.#audioContext.currentTime + 0.055);
osc.frequency.setValueAtTime(800, this.#audioContext.currentTime + 0.083);
osc.frequency.setValueAtTime(1200, this.#audioContext.currentTime + 0.110);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 165);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
this.#audioPlaying = false;
}
}
playPlayerDie() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 25ms, 10 cycles: 400Hz for 25ms
Second tone, 27ms, 24 cycles: 889Hz for 27ms
Third tone, 28ms, 14 cycles: 500Hz for 28ms
Fourth tone, 25ms, 5 cycles: 200Hz for 30ms
Fifth tone, 20ms, 1 cycle: 50Hz for 50ms
Total time: 160ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(400, this.#audioContext.currentTime);
osc.frequency.setValueAtTime(889, this.#audioContext.currentTime + 0.025);
osc.frequency.setValueAtTime(500, this.#audioContext.currentTime + 0.052);
osc.frequency.setValueAtTime(200, this.#audioContext.currentTime + 0.080);
osc.frequency.setValueAtTime(50, this.#audioContext.currentTime + 0.110);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 160);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
}
}
playDestroyDirt() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 30ms, 3 cycles: 100Hz for 30ms
Second tone, 26ms, 13 cycles: 500Hz for 26ms
Third tone, 50ms, 1 cycle: 20Hz for 50ms
Fourth tone, 4ms, 2 cycles: 500Hz for 4ms
Fifth tone, 40ms, 2 cycles: 50Hz for 60ms
Total time: 170ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(100, this.#audioContext.currentTime);
osc.frequency.setValueAtTime(500, this.#audioContext.currentTime + 0.030);
osc.frequency.setValueAtTime(20, this.#audioContext.currentTime + 0.056);
osc.frequency.setValueAtTime(500, this.#audioContext.currentTime + 0.106);
osc.frequency.setValueAtTime(50, this.#audioContext.currentTime + 0.110);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 170);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
this.#audioPlaying = false;
}
}
playRockFall() {
if (this.#audioPlaying) return;
this.#audioPlaying = true;
/*
First tone, 23ms, 7 cycles, 304Hz for 25ms
Off for 48ms
Second tone, 80ms, 7 cycles: 87Hz for 87ms
Total time: 160ms
*/
let osc = new OscillatorNode(this.#audioContext);
osc.connect(this.#gainNode);
osc.type = 'square';
osc.frequency.setValueAtTime(304, this.#audioContext.currentTime);
this.#gainNode.gain.setValueAtTime(0, this.#audioContext.currentTime + 0.025);
osc.frequency.setValueAtTime(87.5, this.#audioContext.currentTime + 0.03);
this.#gainNode.gain.setValueAtTime(this.#audioLevel, this.#audioContext.currentTime + 0.073);
this.startTone(osc);
setTimeout(disconnectOscillator.bind(this), 160);
function disconnectOscillator() {
osc.disconnect(this.#gainNode);
this.#audioPlaying = false;
}
}
startTone(osc) {
if (osc.start) {
osc.start(0);
} else {
osc.noteOn(0);
}
}
finaliseField() {
// Get the list of empty cells in the field
let emptyCells = this.findAllCellsOfType(spriteEnum.BLANK);
// Pick one cell at random
let rnd = Math.floor(Math.random() * emptyCells.length);
// Remove it from the array of empty cells
let index = emptyCells.splice(rnd, 1)[0];
// Place the player at that index
this.#dozer = new Dozer(index);
this.#grid[index] = this.#dozer;
// If on hardest level, surround player in dirt to protect from bugs
if (this.#difficulty == difficultyEnum.HARDEST) {
let rStart = this.checkEdgeTop(index) ? 0 : -1;
let rEnd = this.checkEdgeBottom(index) ? 0 : 1;
let cStart = this.checkEdgeLeft(index) ? 0 : -1;
let cEnd = this.checkEdgeRight(index) ? 0 : 1;
// Create a 3x3 explosion grid
for (let r = rStart; r <= rEnd; r++) {
for (let c = cStart; c <= cEnd; c++) {
let checkCell = index + (r * this.#fieldX) + c;
// If the surrounding cell is empty or a bug, replace with Dirt
if (!this.#grid[checkCell] || this.#grid[checkCell] instanceof Bug) {
this.#grid[checkCell] = new Dirt();
}
}
}
}
// Get fresh list of empty cells
emptyCells = this.findAllCellsOfType(spriteEnum.BLANK);
// Pick another empty cell at random
rnd = Math.floor(Math.random() * emptyCells.length);
index = emptyCells.splice(rnd, 1)[0];
// Place the exit at that index
this.#grid[index] = new Exit();
this.#availableScore = this.getRemainingScore();
this.#targetScore = Math.floor(this.#availableScore * this.#difficultyScoreFactor[this.#difficulty]);
console.log(`Available score is ${this.#availableScore}, target score is ${this.#targetScore}`);
// Remove flag that was holding up gameplay
this.#fieldInitialising = false;
}
getRemainingScore() {
let emeralds = this.findAllCellsOfType(spriteEnum.EMERALD).length;
let diamonds = this.findAllCellsOfType(spriteEnum.DIAMOND).length;
return (diamonds * 5) + emeralds;
}
updateField() {
// We get ordering issues when moving items around.
// If we do a single pass through the field, items can be updated twice (two movements in one tick of time)
// If an item moves to the right (in order to fall down), and we are iterating left-to-right,
// it will be updated twice (right and down)
// Track if there are any field changes
// so we know when the board has settled and we can place
// the dozer and exit.
let changes = false;
for (let c = this.#grid.length - 1; c >= 0; c--) {
let obj = this.#grid[c];
if (obj === spriteEnum.BLANK)
continue;
// Check if cell is an explosion
if (obj instanceof Explosion) {
changes = true;
if (obj.isNewExplosion) {
obj.isNewExplosion = false;
} else {
this.#grid[c] = spriteEnum.BLANK;
}
continue;
}
// Check if cell is a DroppedGrenade
if (obj instanceof DroppedGrenade) {
if (obj.tick()) {
this.createExplosion(c);
this.playExplosion();
}
}
if (!obj.gravity)
continue;
// Deal with items on the bottom row
if (this.checkEdgeBottom(c)) {
// If item is falling and explosive
if (obj.isFalling) {
obj.isFalling = false;
if (obj instanceof Gem) {
this.playGemFall();
}
if (obj instanceof Rock) {
this.playRockFall();
}
if (obj.isExplosive) {
changes = true;
this.createExplosion(c);
this.playExplosion();
}
}
// Don't bother checking other items on bottom row
continue;
}
let cellBelow = c + this.#fieldX;
let objBelow = this.#grid[cellBelow];
// Check if cell below is empty, OR if item is falling and item below can be crushed
if (!objBelow || obj.isFalling && objBelow.canBeCrushed) {
changes = true;
// If item below is explosive, go bang!
if (objBelow && objBelow.isExplosive) {
this.createExplosion(cellBelow);
this.playExplosion();
continue;
}
// If item below is dozer, die
else if (objBelow == this.#dozer) {
this.playPlayerDie();
this.#playerDyingCallback('You got crushed!');
}
if (objBelow instanceof Diamond) {
this.playDiamondCrushed();
}
// Propogate item down
obj.isFalling = true;
this.#grid[cellBelow] = obj;
this.#grid[c] = spriteEnum.BLANK;
}
// Else check if item is falling and explosive (already ruled out empty cell below)
else if (obj.isFalling && obj.isExplosive) {
changes = true;
this.createExplosion(c);
this.playExplosion();
}
// Else check if item below is uneven and it can fall left (cell left and below left are empty)
// If we move item to the left, skip it (decrement the counter) so it doesn't get processed twice
else if (!this.checkEdgeLeft(c) && objBelow.isUneven && !this.#grid[c - 1] && !this.#grid[c - 1 + this.#fieldX]) {
if (obj.isFalling && obj instanceof Gem) {
this.playGemFall();
}
if (obj.isFalling && obj instanceof Rock) {
this.playRockFall();
}
changes = true;
obj.isFalling = true;
this.#grid[c - 1] = obj;
this.#grid[c] = spriteEnum.BLANK;
c--;
}
// Else check if item below is uneven and it can fall right (cell right and below right are empty)
else if (!this.checkEdgeRight(c) && objBelow.isUneven && !this.#grid[c + 1] && !this.#grid[c + 1 + this.#fieldX]) {
if (obj.isFalling && obj instanceof Gem) {
this.playGemFall();
}
if (obj.isFalling && obj instanceof Rock) {
this.playRockFall();
}
changes = true;
obj.isFalling = true;
this.#grid[c + 1] = obj;
this.#grid[c] = spriteEnum.BLANK;
}
// Else check if item below is solid (can't be crushed) to disable falling.
else if (obj.isFalling) {
if (obj instanceof Gem) {
this.playGemFall();
}
if (obj instanceof Rock) {
this.playRockFall();
}
changes = true;
obj.isFalling = false;
}
}
// Move the bugs separately, since they can move in any direction.
// Too tricky to avoid moving them twice in a single update (frame)
// Move only after all other updates. Otherwise they dodge items being dropped on them.
this.moveBugs();
if (this.#fieldInitialising && !changes)
this.finaliseField();
}
moveBugs() {
var bugArray = this.findAllCellsOfType(spriteEnum.BUG);
var movementFunctions = new Array(this.moveBugUp.bind(this), this.moveBugRight.bind(this), this.moveBugDown.bind(this), this.moveBugLeft.bind(this));
bugArray.forEach((c) => {
let bug = this.#grid[c];
let moved = false;
// Always try to go "left" (relative to current direction) first,
for (const i of new Array(3, 0, 1, 2)) {
let tryDirection = (bug.Direction + i) % 4;
if (movementFunctions[tryDirection](c, false)) {
bug.Direction = tryDirection;
moved = true;
break;
}
}
// If the bug didn't move, try again without following edges (fix floating bug issue, move freely)
if (!moved) {
for (const i of new Array(3, 0, 1, 2)) {
let tryDirection = (bug.Direction + i) % 4;
if (movementFunctions[tryDirection](c, true)) {
bug.Direction = tryDirection;
break;
}
}
}
});
}
moveBugUp(c, ignoreFollowEdge) {
// Move UP case
// (ignore edge following OR (on left edge or left cell is not empty or top-left cell is not empty))
// AND
// (cell above is not edge and is empty)
if ((ignoreFollowEdge || (this.checkEdgeLeft(c) || this.#grid[c-1] || (!this.checkEdgeTop(c) && this.#grid[c-this.#fieldX-1])))
&&
(!this.checkEdgeTop(c) && !this.#grid[c-this.#fieldX])) {
let newCell = c-this.#fieldX;
this.#grid[newCell] = this.#grid[c];
this.#grid[c] = 0;
this.checkBugDozerProximity(newCell);
return true;
}
return false;
}
moveBugRight(c, ignoreFollowEdge) {
// Move RIGHT case
// (ignore edge following OR (on top edge or top cell is not empty or top-right cell is not empty))
// AND
// (cell right is not edge and is empty)
if ((ignoreFollowEdge || (this.checkEdgeTop(c) || this.#grid[c-this.#fieldX] || (!this.checkEdgeRight(c) && this.#grid[c-this.#fieldX+1])))
&&
(!this.checkEdgeRight(c) && !this.#grid[c+1])) {
let newCell = c+1;
this.#grid[newCell] = this.#grid[c];
this.#grid[c] = 0;
this.checkBugDozerProximity(newCell);
return true;
}
return false;
}
moveBugDown(c, ignoreFollowEdge) {
// Move DOWN case
// (ignore edge following OR (on right edge or right cell is not empty or bottom-right cell is not empty))
// AND
// (cell below is not edge and is empty)
if ((ignoreFollowEdge || (this.checkEdgeRight(c) || this.#grid[c+1] || (!this.checkEdgeBottom(c) && this.#grid[c+this.#fieldX+1])))
&&
(!this.checkEdgeBottom(c) && !this.#grid[c+this.#fieldX])) {
let newCell = c+this.#fieldX;
this.#grid[newCell] = this.#grid[c];
this.#grid[c] = 0;
this.checkBugDozerProximity(newCell);
return true;
}
return false;
}
moveBugLeft(c, ignoreFollowEdge) {
// Move LEFT case
// (ignore edge following OR (on bottom edge or bottom cell is not empty or bottom-left cell is not empty))
// AND
// (cell left is not edge and is empty)
if ((ignoreFollowEdge || (this.checkEdgeBottom(c) || this.#grid[c+this.#fieldX] || (!this.checkEdgeLeft(c) && this.#grid[c+this.#fieldX-1])))
&&
(!this.checkEdgeLeft(c) && !this.#grid[c-1])) {
let newCell = c-1;
this.#grid[newCell] = this.#grid[c];
this.#grid[c] = 0;
this.checkBugDozerProximity(newCell);
return true;
}
return false;
}
checkBugDozerProximity(n) {
// If the passed in cell contains a Bug, we are looking for Dozer
let lookingFor = null;
if (this.#grid[n] instanceof Bug) {
lookingFor = Dozer;
} else if (this.#grid[n] instanceof Dozer) {
lookingFor = Bug;
} else {
return;
}
let rStart = this.checkEdgeTop(n) ? 0 : -1;
let rEnd = this.checkEdgeBottom(n) ? 0 : 1;
let cStart = this.checkEdgeLeft(n) ? 0 : -1;
let cEnd = this.checkEdgeRight(n) ? 0 : 1;
// Create a 3x3 explosion grid
for (let r = rStart; r <= rEnd; r++) {
for (let c = cStart; c <= cEnd; c++) {
let checkCell = n + (r * this.#fieldX) + c;
if (this.#grid[checkCell] instanceof lookingFor) {
// Center the explosion on the bug
let explosionCell = (this.#grid[checkCell] instanceof Bug) ? checkCell : n;
this.createExplosion(explosionCell);
}
}
}
}
checkEdgeLeft(n) {
return (n % this.#fieldX) === 0;
}
checkEdgeRight(n) {
return (n % this.#fieldX) === (this.#fieldX - 1);
}
checkEdgeTop(n) {
return Math.floor(n / this.#fieldX) === 0;
}
checkEdgeBottom(n) {
return Math.floor(n / this.#fieldX) === (this.#fieldY - 1);
}
convertTupleToSingle(x, y) {
return (y * this.#fieldX) + x;
}
convertSingleToTuple(n) {
return {
x: n % this.#fieldX,
y: Math.floor(n / this.#fieldX)
};
}
createExplosion(cellNum) {
// Clear center square contents (eg grenade/bomb) to avoid infinite recursion.
this.#grid[cellNum] = 0;
let rStart = this.checkEdgeTop(cellNum) ? 0 : -1;
let rEnd = this.checkEdgeBottom(cellNum) ? 0 : 1;
let cStart = this.checkEdgeLeft(cellNum) ? 0 : -1;
let cEnd = this.checkEdgeRight(cellNum) ? 0 : 1;
// Create a 3x3 explosion grid
for (let r = rStart; r <= rEnd; r++) {
for (let c = cStart; c <= cEnd; c++) {
let checkCell = cellNum + (r * this.#fieldX) + c;
// Can't check grid, since if we sit on a dropped grenade we don't exist in the grid
// If it contains dozer, die
if (!this.#fieldInitialising && checkCell === this.#dozer.pos) {
this.playPlayerDie();
this.#playerDyingCallback('You got asploded!');
}
// If cell contains an object and it's explosive, recurse
if (this.#grid[checkCell] && this.#grid[checkCell].isExplosive) {
this.createExplosion(checkCell);
continue;
}
// If cell is empty OR object can be destroyed.
if (!this.#grid[checkCell] || this.#grid[checkCell].canBeDestroyed) {
this.#grid[checkCell] = new Explosion();
}
}
}
}
populateFieldWithType(t) {
// Get an array of all the empty field cells
let emptyCells = this.findAllCellsOfType(spriteEnum.BLANK);
// Default field size is 800
// Calculate a scaling factor for this field, based on size
let scale = (this.#fieldX * this.#fieldY) / 800;
let desiredQty = Math.round(this.#difficultyDistribution[this.#difficulty][t] * scale);
for (let i = 0; i < desiredQty; i++) {
// Pick a random value from the array of empty cells
let rnd = Math.floor(Math.random() * emptyCells.length);
// Splice deletes 1 cell from the empty array, at position random
// And returns the value removed
let index = emptyCells.splice(rnd, 1)[0];
this.#grid[index] = new classArray[t]();
}
}
findAllCellsOfType(t) {
// https://stackoverflow.com/a/41271541/5329728
// e for element, i for index
return this.#grid.map(function(e, i){
if(t === spriteEnum.BLANK)
{
// Check if grid cell contains "blank" (0)
return e === t ? i : '';
}
// Check if grid cell is an instance of the specified class type
return e instanceof classArray[t] ? i : '';
}).filter(String);
}
addRandomDiamond() {
let rnd = Math.floor(Math.random() * this.#fieldX);
this.#grid[rnd] = new Diamond();
}
handleInput(e) {
// If we are still initialising the field
// and haven't placed the dozer yet, ignore input
if(this.#fieldInitialising)
return;
let dozerPos = this.#dozer.pos;
let sittingOnGrenade = this.#grid[dozerPos] instanceof DroppedGrenade;
var key = e.key || e.keyCode;
switch (key) {
// Up key
case 'ArrowUp':
case 38:
e.preventDefault();
// If we're on the top edge already, give up
if (this.checkEdgeTop(dozerPos))
return;
var objAbove = this.#grid[dozerPos - this.#fieldX];
// Check if cell above is either empty or can pass through
if (!objAbove || objAbove.canPassThrough) {
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos -= this.#fieldX;
} else if (objAbove instanceof Exit && this.#gameScore >= this.#targetScore) {
this.#grid[dozerPos] = spriteEnum.BLANK;
this.#dozer.pos -= this.#fieldX;
console.log('You win!');
this.#playerWonCallback();
}
break;
// Down key
case 'ArrowDown':
case 40:
e.preventDefault();
// If we're on the bottom edge already, give up
if (this.checkEdgeBottom(dozerPos))
return;
var objBelow = this.#grid[dozerPos + this.#fieldX];
// Check if cell is either empty or can pass through
if (!objBelow || objBelow.canPassThrough) {
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos += this.#fieldX;
} else if (objBelow instanceof Exit && this.#gameScore >= this.#targetScore) {
this.#grid[dozerPos] = spriteEnum.BLANK;
this.#dozer.pos += this.#fieldX;
console.log('You win!');
this.#playerWonCallback();
}
break;
// Left key
case 'ArrowLeft':
case 37:
e.preventDefault();
// If we're on the left edge already, give up
if (this.checkEdgeLeft(dozerPos))
return;
var objLeft = this.#grid[dozerPos - 1];
// Check if cell is either empty or can pass through
if (!objLeft || objLeft.canPassThrough) {
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos -= 1;
}
// If we are at least 2 squares from left edge (square to our left is not the edge)
// and item to left is pushable AND item to left of that is empty
else if (!this.checkEdgeLeft(dozerPos - 1) && objLeft.isPushable && !this.#grid[dozerPos - 2]) {
// Push the item left
this.#grid[dozerPos - 2] = this.#grid[dozerPos - 1];
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos -= 1;
} else if (objLeft instanceof Exit && this.#gameScore >= this.#targetScore) {
this.#grid[dozerPos] = spriteEnum.BLANK;
this.#dozer.pos -= 1;
console.log('You win!');
this.#playerWonCallback();
}
break;
// Right key
case 'ArrowRight':
case 39:
e.preventDefault();
// If we're on the right edge already, give up
if (this.checkEdgeRight(dozerPos))
return;
var objRight = this.#grid[dozerPos + 1];
// Check if cell is either empty or can pass through
if (!objRight || objRight.canPassThrough) {
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos += 1;
}
// If we are at least 2 squares from right edge (square to our right is not the edge)
// and item to right is pushable AND item to right of that is empty
else if (!this.checkEdgeRight(dozerPos + 1) && objRight.isPushable && !this.#grid[dozerPos + 2]) {
// Push the item right
this.#grid[dozerPos + 2] = this.#grid[dozerPos + 1];
if (!sittingOnGrenade) {
this.#grid[dozerPos] = spriteEnum.BLANK;
}
this.#dozer.pos += 1;
} else if (objRight instanceof Exit && this.#gameScore >= this.#targetScore) {
this.#grid[dozerPos] = spriteEnum.BLANK;
this.#dozer.pos += 1;
console.log('You win!');
this.#playerWonCallback();
}
break;
// Space key
case ' ':
case 32:
e.preventDefault();
if (this.#dozer.hasGrenades()) {
this.#dozer.useGrenade();
this.#grid[dozerPos] = new DroppedGrenade();
}
break;
}
let newPosObj = this.#grid[this.#dozer.pos];
if ((newPosObj instanceof Grenade) && !(newPosObj instanceof DroppedGrenade)) {
this.#dozer.pickupGrenade();
} else if (newPosObj instanceof Gem) {
this.#gameScore += newPosObj.score;
// Dozer will be placed over top of gem below, but it affects remaining gem score
// Delete Gem that we just ate
this.#grid[this.#dozer.pos] = 0;
console.log(`Ate a gem, score ${this.#gameScore}/${this.#targetScore} (${this.getRemainingScore()} remaining)`);
}
if (newPosObj instanceof Dirt) {
this.playDestroyDirt();
}
// Set grid location to dozer, unless a grenade was dropped
if (!(newPosObj instanceof DroppedGrenade)) {
this.#grid[this.#dozer.pos] = this.#dozer;
}
this.checkBugDozerProximity(this.#dozer.pos);
// Explicitly clear the old dozer position
// Otherwise we need to wait for the next game tick to clear the canvas
this.clearCell(dozerPos);
// Perform an additional render on each keystroke to keep the game responsive
this.renderField();
}
clearCell(n) {
let p = this.convertSingleToTuple(n);
let s = EmeraldHunt.SPRITESIZE;
let x = p.x * s;
let y = p.y * s;
this.#ctx.fillStyle = colorEnum.BLACK;
this.#ctx.fillRect(x, y, s+1, s+1);
}
renderField() {
this.#grid.forEach((e, i) => {
if (!e)
return;
let x = EmeraldHunt.SPRITESIZE * (i % this.#fieldX);
let y = EmeraldHunt.SPRITESIZE * Math.floor(i / this.#fieldX);
this.#ctx.drawImage(e.image, x, y);
});
this.renderScoreBar();
}
renderScoreBar() {
// Wipe scorebar first to prevent flicker
this.#ctx.fillStyle = colorEnum.BLACK;
this.#ctx.fillRect(0, (EmeraldHunt.SPRITESIZE * this.#fieldY)-1, EmeraldHunt.SPRITESIZE * this.#fieldX, EmeraldHunt.SPRITESIZE * 1.5);
// Draw horizontal line
this.#ctx.strokeStyle = colorEnum.WHITE;
this.#ctx.beginPath();
this.#ctx.moveTo(0, EmeraldHunt.SPRITESIZE * this.#fieldY);
this.#ctx.lineTo(EmeraldHunt.SPRITESIZE * this.#fieldX, EmeraldHunt.SPRITESIZE * this.#fieldY);
this.#ctx.stroke();
let y = EmeraldHunt.SPRITESIZE * (this.#fieldY+1);
this.#ctx.font = EmeraldHunt.FONT;
// Write score
this.#ctx.fillStyle = colorEnum.WHITE;
this.#ctx.fillText('score :', 1, y);
// Ensure targetscore has value, then compare to gamescore
this.#ctx.fillStyle = this.#targetScore && this.#gameScore >= this.#targetScore ? colorEnum.GREEN : colorEnum.RED;
this.#ctx.fillText(this.#gameScore, 62, y);
// Write goal
this.#ctx.fillStyle = colorEnum.WHITE;
this.#ctx.fillText('goal :', 100, y);
this.#ctx.fillStyle = colorEnum.RED;
this.#ctx.fillText(this.#targetScore, 152, y);
// Write remaining available score
this.#ctx.fillStyle = colorEnum.WHITE;
this.#ctx.fillText('remaining :', 190, y);
let remaining = this.#targetScore && this.getRemainingScore();
this.#ctx.fillStyle = this.#targetScore && this.#targetScore < (this.#gameScore + remaining) ? colorEnum.GREEN : colorEnum.RED;
this.#ctx.fillText(remaining, 282, y);
// Write grenades
let grenades = 0;
if(this.#dozer) {
grenades = this.#dozer.numGrenades;
}
this.#ctx.fillStyle = colorEnum.WHITE;
this.#ctx.fillText('grenades :', 320, y);
this.#ctx.fillStyle = colorEnum.RED;
this.#ctx.fillText(grenades, 405, y);