-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblaster-02.js
1665 lines (1509 loc) · 53.7 KB
/
blaster-02.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
window.onload = function() {
init();
activeLoop = requestAnimationFrame(initLoop);
}
//------------------------------------------------------//
//Global Variable Club: Cool kids only! //
//------------------------------------------------------//
//
//Preloaded assets counter
let preload = 0;
//
//Web Audio API context
let audioCtx;
//
//Holds the sfx audio buffers
let shotSound, deathSound, ammoUp, bomberSound;
//
//Background Music buffers
let bgm1, bgmGain, bgmSource
//
//Game context
const gameScreen = document.getElementById("screen");
let ctx, statCtx, levelCtx, levelTopCtx;
const canvasHeight = 480;
const canvasWidth = 720;
let bgPos1 = 0, bgPos2 = 0;
//
//Sprite sheets
let spriteSheet, fontSheet, level1, level1Top;
//
//Keboard state
let keyState;
//
//Game loop
let activeLoop, loopCount, endLoop = false;
//
//Arrays of things
let shots = [], enemies = [], enemyShots = [];
//
//Game variables
let player, score;
function init() {
//----------------------------------------------------//
//loads all of the data for the game //
//Preload: 9 //
//----------------------------------------------------//
//
//Load audio data (preload: 5)
(function() {
const AudioContext = window.AudioContext || window. webkitAudioContext;
audioCtx = new AudioContext();
//
//Sound made when the player shoots
fetch("audio/zap4.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
shotSound = decodedData;
preload++;
});
});
//
//Sound made when the player dies
fetch("audio/death5.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
deathSound = decodedData;
preload++;
});
});
//
//Sound made when the ammo is reloaded
fetch("audio/ammoUp.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
ammoUp = decodedData;
preload++;
});
});
//
//Background music
fetch("audio/short-space-loop2.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
bgm1 = decodedData;
preload++;
});
});
//
//Sound made when the bomber enemy fires
fetch("audio/bomberSound.mp3")
.then(function(response) {
return response.arrayBuffer();
})
.then(function(buffer) {
audioCtx.decodeAudioData(buffer, function(decodedData) {
bomberSound = decodedData;
preload++;
});
});
})();
//
//Load image data (preload: 4)
(function() {
//----------------------------------------------------//
//Makes the canvases that show the level background //
//----------------------------------------------------//
//
//The lower background canvas
let levelCanvas = makeElement("canvas", "levelCanvas");
gameScreen.appendChild(levelCanvas);
levelCanvas.setAttribute("width", canvasWidth);
levelCanvas.setAttribute("height", canvasHeight);
levelCtx = levelCanvas.getContext("2d");
levelCtx.imageSmoothingEnabled = false;
//
//The upper background canvas
let levelTopCanvas = makeElement("canvas", "levelTopCanvas");
gameScreen.appendChild(levelTopCanvas);
levelTopCanvas.setAttribute("width", canvasWidth);
levelTopCanvas.setAttribute("height", canvasHeight);
levelTopCtx = levelTopCanvas.getContext("2d");
levelTopCtx.imageSmoothingEnabled = false;
//
//Make the primary canvas upon which the player, enemies,
// and shots are drawn
let gameCanvas = makeElement("canvas", "gameCanvas");
gameScreen.appendChild(gameCanvas);
gameCanvas.setAttribute("height", canvasHeight);
gameCanvas.setAttribute("width", canvasWidth);
ctx = gameCanvas.getContext("2d");
ctx.imageSmoothingEnabled = false;
//
//Make the in-game stats canvas for displaying ammo, score,
// and player lives
let statCanvas = makeElement("canvas", "statCanvas");
gameScreen.appendChild(statCanvas);
statCanvas.setAttribute("width", canvasWidth);
statCanvas.setAttribute("height", 32);
statCtx = statCanvas.getContext("2d");
statCtx.imageSmoothingEnabled = false;
//
//Load the primary sprite sheet
let blastSheet = new Image();
blastSheet.src = "spriteSheet.png";
blastSheet.onload = function() {
spriteSheet = makeElement("canvas", "spriteSheet");
spriteSheet.setAttribute("width", 160);
spriteSheet.setAttribute("height", 544);
let spriteSheetCtx = spriteSheet.getContext("2d");
spriteSheetCtx.drawImage(blastSheet, 0, 0, 160, 544);
preload++
}
//
//Loading the font sheet
let whiteFont = new Image();
whiteFont.src = "8-bitFontWhite.png";
whiteFont.onload = function() {
fontSheet = makeElement("canvas", "fontSheet");
fontSheet.setAttribute("width", 64);
fontSheet.setAttribute("height", 40);
let fontSheetCtx = fontSheet.getContext("2d");
fontSheetCtx.drawImage(whiteFont, 0, 0, 64, 40);
preload++;
}
//
//Loads the lower background level image
let backgroundMap1 = new Image();
backgroundMap1.src = "map1-2.png";
backgroundMap1.onload = function() {
level1 = makeElement("canvas", "level1");
level1.setAttribute("width", 720);
level1.setAttribute("height", 960);
let level1Ctx = level1.getContext("2d");
level1Ctx.drawImage(backgroundMap1, 0, 0, 720, 960);
preload++;
}
//
//Loads the upper background level image
let bgMapTop = new Image();
bgMapTop.src = "map1-clouds.png";
bgMapTop.onload = function() {
level1Top = makeElement("canvas", "level1Top");
level1Top.setAttribute("width", 720);
level1Top.setAttribute("height", 960);
let level1TopCtx = level1Top.getContext("2d");
level1TopCtx.drawImage(bgMapTop, 0, 0, 720, 960);
preload++;
}
})();
}
function makeElement(type, id, ...classes) {
//----------------------------------------------------//
//Returns an HTML element //
//----------------------------------------------------//
//type(string): type of element to be returned //
//id(string): id of the element //
//classes(string): classes to add to the element //
//----------------------------------------------------//
//return(element): element that was made //
//----------------------------------------------------//
let element = document.createElement(type);
if (typeof id === "string") {element.id = id}
classes.forEach(x => element.classList.add(x));
return element;
}
function rnd (floor, ceiling) {
//----------------------------------------------------//
//Generates a random number within a range of numbers //
//----------------------------------------------------//
//floor(integer): lower bound of the random number //
//ceiling(integer): upper bound of the random number //
//----------------------------------------------------//
//return(integer): random number w/in the range //
//----------------------------------------------------//
let range = (ceiling - floor) + 1;
return Math.floor((Math.random() * range) + floor);
}
function write2screen(ctx, xPos, yPos, string, size = 1) {
//----------------------------------------------------//
//Adds sprite based letters to the screen //
//----------------------------------------------------//
//xPos(integer/string): position of the top left //
// corner of the first letter of the string. Can //
// also be a string indicating alignment: "center", //
// "left", or "right" //
//yPos(integer): position of the top left pixel of //
// the first letter //
//string(string): string to be put on screen //
//----------------------------------------------------//
let chars = {
//----------------------------------------------------//
//The locations of each letter within the sprite sheet//
//----------------------------------------------------//
"A": new Sprite(0, 0, 8, 8),
"B": new Sprite(8, 0, 8, 8),
"C": new Sprite(16, 0, 8, 8),
"D": new Sprite(24, 0, 8, 8),
"E": new Sprite(32, 0, 8, 8),
"F": new Sprite(40, 0, 8, 8),
"G": new Sprite(48, 0, 8, 8),
"H": new Sprite(56, 0, 8, 8),
"I": new Sprite(0, 8, 8, 8),
"J": new Sprite(8, 8, 8, 8),
"K": new Sprite(16, 8, 8, 8),
"L": new Sprite(24, 8, 8, 8),
"M": new Sprite(32, 8, 8, 8),
"N": new Sprite(40, 8, 8, 8),
"O": new Sprite(48, 8, 8, 8),
"P": new Sprite(56, 8, 8, 8),
"Q": new Sprite(0, 16, 8, 8),
"R": new Sprite(8, 16, 8, 8),
"S": new Sprite(16, 16, 8, 8),
"T": new Sprite(24, 16, 8, 8),
"U": new Sprite(32, 16, 8, 8),
"V": new Sprite(40, 16, 8, 8),
"W": new Sprite(48, 16, 8, 8),
"X": new Sprite(56, 16, 8, 8),
"Y": new Sprite(0, 24, 8, 8),
"Z": new Sprite(8, 24, 8, 8),
"1": new Sprite(16, 24, 8, 8),
"2": new Sprite(24, 24, 8, 8),
"3": new Sprite(32, 24, 8, 8),
"4": new Sprite(40, 24, 8, 8),
"5": new Sprite(48, 24, 8, 8),
"6": new Sprite(56, 24, 8, 8),
"7": new Sprite(0, 32, 8, 8),
"8": new Sprite(8, 32, 8, 8),
"9": new Sprite(16, 32, 8, 8),
"0": new Sprite(24, 32, 8, 8),
"?": new Sprite(32, 32, 8, 8),
"!": new Sprite(40, 32, 8, 8),
":": new Sprite(48, 32, 8, 8),
" ": new Sprite(56, 32, 8, 8)
};
string = string.toString(10);
if (typeof xPos !== "number") {
if (xPos === "center") xPos = (canvasWidth / 2) - ((string.length * size * 8) / 2);
if (xPos === "left") xPos = size * 8;
if (xPos === "right") xPos = canvasWidth - ((size * 8) + (string.length * size * 8));
}
string.toUpperCase().split("").forEach((x, i) => {
ctx.drawImage(fontSheet, chars[x].x, chars[x].y, chars[x].w, chars[x].h, xPos + (i * size * 8), yPos, size * 8, size * 8);
});
}
function playSfx(buffer) {
//----------------------------------------------------//
//Plays a sound effect //
//----------------------------------------------------//
//buffer(binary): audio buffer to be played //
//----------------------------------------------------//
let source = audioCtx.createBufferSource();
source.buffer = buffer;
source.playbackRate.value = .85 + (rnd(-15, 15) / 100);
source.connect(audioCtx.destination);
source.start(0);
}
function playBGM(buffer) {
//----------------------------------------------------//
//Plays background music //
//----------------------------------------------------//
//buffer(binary): audio buffer to be played //
//----------------------------------------------------//
let source = audioCtx.createBufferSource();
source.buffer = buffer;
let vol = 0;
bgmGain = audioCtx.createGain();
bgmGain.gain.value = vol;
source.connect(bgmGain).connect(audioCtx.destination);
source.loop = true;
source.start(0);
//
//Gently fade in the sound before game
let fadeIn = setInterval(function() {
bgmGain.gain.value = vol;
if (vol >= .5) {
clearInterval(fadeIn);
}
vol += .01;
}, 40);
return source;
}
function drawBg(y, level, ctx) {
//----------------------------------------------------//
//Displays the level background //
//----------------------------------------------------//
//y(integer): the y position of the background //
//level(canvas): which level to draw //
//----------------------------------------------------//
ctx.clearRect(0, 0, 720, 480);
y %= 960;
if (y > 480) {
ctx.drawImage(level, 0, 960 - (y - 480), 720, y - 480, 0, 0, 720, y - 480);
ctx.drawImage(level, 0, 0, 720, 960 - y, 0, y - 480, 720, 960 - y);
} else {
ctx.drawImage(level, 0, 480 - y, 720, 480, 0, 0, 720, 480);
}
}
function showScore(score) {
//----------------------------------------------------//
//Shows the player's score //
//----------------------------------------------------//
//score(integer): score to display //
//----------------------------------------------------//
statCtx.clearRect(8, 12, 96, 8);
//
//Every 25 points the player gets an ammo bonus
if (score % 25 === 0 && score > 0) {
playSfx(ammoUp);
player.ammo += 50;
showAmmo(player.ammo);
}
//
//Every 150 points the player gets an extra life
if (score % 150 === 0 && score > 0) {
showLives(++player.lives);
}
score = score.toString(10).padStart(4, "0");
write2screen(statCtx, "left", 12, `Bounty: ${score}`);
}
function showAmmo(ammo) {
//----------------------------------------------------//
//Shows how much ammo the player has //
//----------------------------------------------------//
//ammo(integer): ammo to display //
//----------------------------------------------------//
statCtx.clearRect(259, 7, 201, 18);
//
//Draw the outline of the guage
statCtx.strokeStyle = "white";
statCtx.lineWidth = 2;
statCtx.strokeRect(259, 7, 201, 18);
//
//Blue ammo bar
statCtx.fillStyle = ammo < 25 ? "red" : "blue";
let blueBar = ammo >= 100 ? 200 : ammo * 2;
statCtx.fillRect(260, 8, blueBar, 16);
//
//Green ammo bar
if (ammo > 100) {
statCtx.fillStyle = "green"
statCtx.fillRect(260, 8, (ammo - 100) * 2, 16);
}
}
function showLives(lives) {
//----------------------------------------------------//
//Shows how many lives the player has left //
//----------------------------------------------------//
//lives(integer): how many lives to display //
//----------------------------------------------------//
statCtx.clearRect(672, 8, 704, 16);
lives = lives < 0 ? 0 : lives;
statCtx.drawImage(spriteSheet, 0, 0, 32, 32, 688, 8, 16, 16);
write2screen(statCtx, 672, 12, lives);
}
function makeEnemy(type) {
//----------------------------------------------------//
//Makes a new enemy and puts them in the enemy array //
//----------------------------------------------------//
//type(string): type of enemy to create //
//----------------------------------------------------//
let xPos;
switch(type) {
case "ray":
xPos = rnd(32, 688);
let ray = new Ray(xPos, -32);
enemies.push(ray);
break;
case "ring":
xPos = rnd(200, 688);
let spinner = new Ring(xPos, -32);
enemies.push(spinner);
break;
case "bomber":
xPos = rnd(32, 688);
let bomber = new Bomber(xPos, -32);
enemies.push(bomber);
break;
case "crawler":
xPos = rnd(32, 688);
let crawler = new Crawler(360, -32);
enemies.push(crawler);
break;
}
}
class Sprite {
//----------------------------------------------------//
//A data structure for holding information about //
// sprites in relation to their position on the //
// sprite sheet //
//----------------------------------------------------//
constructor(x, y, w, h) {
//--------------------------------------------------//
//x, y(integer): the top left corner of the sprite //
// on the sprite sheet //
//w, h(integer): the height and width of the sprite //
//--------------------------------------------------//
this.x = x;
this.y = y;
this.h = h;
this.w = w;
}
static load(xStart, yStart, xSize, ySize, direction, frames) {
//--------------------------------------------------//
//Makes an array of Sprites based on the position //
// of the first element //
//--------------------------------------------------//
//xStart, yStart(ingeger): the starting coordinates //
// for the top left corner of the first sprite //
//xSize, ySize(integer): the size in pixels of the //
// sprites to be loaded //
//direction(string): the orientation of the sprites //
// on the sprite sheet //
//frames(integer): the number of sprites to be //
// loaded into the array //
//--------------------------------------------------//
//return(array): Sprite objects //
//--------------------------------------------------//
let array = [];
for (let i = 0; i < frames; i++) {
array.push(new Sprite(xStart, yStart, xSize, ySize));
switch(direction) {
case "right":
xStart += xSize;
break;
case "down":
yStart += ySize;
break;
}
}
return array;
}
}
class Shot {
//----------------------------------------------------//
//A data structure for holding information about //
// the shots fired by the player //
//----------------------------------------------------//
constructor(x = player.x, y = player.y) {
//--------------------------------------------------//
//x, y(integer): where to first place //
// the shot sprite //
//--------------------------------------------------//
//w, h(integer): width and height of the sprite //
//count(integer): an internal counter used for //
// animation timing //
//sprite(integer): current sprite in the animation //
// cycle //
//sprites(array[Sprite]): the sprites used in the //
// animation //
//--------------------------------------------------//
this.x = x;
this.y = y - 7;
this.w = 32;
this.h = 7;
this.count = 0;
this.sprite = 0;
this.sprites = [new Sprite(128, 0, 32, 7),
new Sprite(128, 7, 32, 7),
new Sprite(128, 14, 32, 7),
new Sprite(128, 14, 32, 7)];
}
currentSprite(num) {
//--------------------------------------------------//
//Returns the current sprite in the animation cycle //
//--------------------------------------------------//
//num(integer): number of sprite in the animation //
// array //
//--------------------------------------------------//
//return(Sprite): current sprite in the animation //
// cycle //
//--------------------------------------------------//
return this.sprites[num % this.sprites.length];
}
draw(x, y) {
//--------------------------------------------------//
//Draws the sprite on the screen //
//--------------------------------------------------//
//x, y(integer): top left corner of where to draw //
// the sprite //
//--------------------------------------------------//
let img = this.currentSprite(this.sprite);
if (this.count < 3) {
this.sprite++;
this.x = x;
this.y = y - this.h;
ctx.drawImage(spriteSheet, img.x, img.y, img.w, img.h, this.x, this.y, this.w, this.h);
} else {
this.y -= 12;
ctx.drawImage(spriteSheet, img.x, img.y, img.w, img.h, this.x, this.y, this.w, this.h);
}
this.count++;
}
}
class BomberShot extends Shot {
//----------------------------------------------------//
//A data structure for holding information about //
// the shots fired by the Bomber enemy //
//----------------------------------------------------//
constructor(x, y) {
//--------------------------------------------------//
//x, y(integer): where to first place //
// the shot sprite //
//--------------------------------------------------//
//w, h(integer): width and height of the sprite //
//sprites(array[Sprite]): the array of sprites to //
// animate the shot //
//--------------------------------------------------//
super(x, y);
this.w = 10;
this.h = 11;
this.sprites = Sprite.load(128, 21, 10, 11, "right", 2);
}
draw(x, y) {
//--------------------------------------------------//
//Draws the sprite, advancing it 6 pixels for //
// each frame //
//--------------------------------------------------//
//x, y(integer): where to draw the shot //
//--------------------------------------------------//
if (this.count % 2 === 0) {
this.sprite++;
}
let img = this.currentSprite(this.sprite);
this.y += 6;
ctx.drawImage(spriteSheet, img.x, img.y, img.w, img.h, this.x, this.y, this.w, this.h);
this.count++;
}
}
class CrawlerShot extends Shot {
//----------------------------------------------------//
//A data structure for holding information about //
// the shots fired by the Crawler enemy //
//----------------------------------------------------//
constructor(x, y, xChange) {
//--------------------------------------------------//
//x, y(integer): where to first place //
// the shot sprite //
//--------------------------------------------------//
//w, h(integer): width and height of the sprite //
//xChange(integer): how much to change the x //
// position of the shot each frame //
//sprites(array[Sprite]): the array of sprites to //
// animate the shot //
//--------------------------------------------------//
super(x);
this.x += 16;
this.y = y - 16;
this.w = 8;
this.h = 8;
this.xChange = xChange;
this.sprites = Sprite.load(128, 32, 8, 8, "right", 2);
}
draw(x, y) {
//--------------------------------------------------//
//Draws the sprite, advancing it 6 pixesls for //
// each frame //
//--------------------------------------------------//
//x, y(integer): where to draw the shot //
//--------------------------------------------------//
if (this.count % 2 === 0) {
this.sprite++;
}
let img = this.currentSprite(this.sprite);
this.x += this.xChange;
this.y += 6;
ctx.drawImage(spriteSheet, img.x, img.y, img.w, img.h, this.x, this.y, this.w, this.h);
this.count++;
}
}
class Ship {
//----------------------------------------------------//
//A class for the player and any enemies //
//----------------------------------------------------//
constructor(x, y) {
//--------------------------------------------------//
//x, y(integer): where to initially draw the //
// object on the coordinate plane //
//--------------------------------------------------//
//h, w(integer): height and width of the object //
// in pixels //
//count(integer): used internally when ships need //
// to count frames //
//dead(boolean): whether or not the ship is dead //
//sprites(array[Sprite]): sprites for the default //
// animation of the ship //
//death(array[Sprite]): sprites for the death //
// animation of the ship //
//sprite(integer): index of the current sprite in //
// the animation //
//colliders(array[array]): objects to test for //
// collisions //
//queuedAction(function): action to take at the end //
// of a non-default animation cycle (eg, death) //
//--------------------------------------------------//
this.x = x;
this.y = y;
this.w = 32;
this.h = 32;
this.count = 0;
this.dead = false;
this.sprites;
this.death;
this.sprite = 0;
this.colliders = [];
this.currentAnimation = this.sprites;
this.queuedAction = null;
}
currentSprite(num) {
//--------------------------------------------------//
//num(integer): index of the current sprite //
//--------------------------------------------------//
//return(Sprite): the sprite object of the current //
// sprite //
//--------------------------------------------------//
return this.currentAnimation[num % this.currentAnimation.length];
}
get lastSprite() {
//--------------------------------------------------//
//return(boolean): whether or not the current //
// sprite is the last one it its animation cycle //
//--------------------------------------------------//
return (this.sprite % this.currentAnimation.length === this.currentAnimation.length - 1);
}
collide() {
//--------------------------------------------------//
//Checks the [this.colliders] array to see if a //
// collision has occurred //
//--------------------------------------------------//
//return(boolean): true for collision //
//--------------------------------------------------//
//console.log(this.x, this.y, this.w, this.h);
for (let i = 0; i < this.colliders.length; i++) {
for (let j = 0; j < this.colliders[i].length; j++) {
if (this.x < this.colliders[i][j].x + this.colliders[i][j].w &&
this.x + this.w > this.colliders[i][j].x &&
this.y < this.colliders[i][j].y + this.colliders[i][j].h &&
this.y + this.h > this.colliders[i][j].y) {
//console.log(this.x, this.w, this.y, this.h);
//console.log(this.colliders[i][j]);
this.colliders[i].splice(j, 1);
playSfx(deathSound);
return true;
}
}
}
return false;
}
update(c) {
//--------------------------------------------------//
//The actions that need to be taken during each //
// loop of the game loop //
//--------------------------------------------------//
//x, y(integer): the position at which to draw the //
// object //
//--------------------------------------------------//
if (c % 2 === 0) {
if (this.currentAnimation !== this.sprites &&
this.sprite === this.currentAnimation.length - 1) {
this.currentAnimation = this.sprites;
}
this.sprite++;
}
this.draw(this.x, this.y);
}
draw(x, y) {
//--------------------------------------------------//
//Draws the object on the screen //
//--------------------------------------------------//
//x, y(integer): Where to draw the ship //
//--------------------------------------------------//
let img = this.currentSprite(this.sprite);
ctx.drawImage(spriteSheet, img.x, img.y, img.w, img.h, x, y, img.w, img.h);
}
die() {
//--------------------------------------------------//
//The basic actions to take when a ship is killed //
//--------------------------------------------------//
this.dead = true;
this.currentAnimation = this.death;
this.sprite = 0;
}
}
class Player extends Ship {
//----------------------------------------------------//
//A sub-class for the player's ship //
//----------------------------------------------------//
constructor(x, y) {
//--------------------------------------------------//
//x, y(integer): where to initially draw the //
// object on the coordinate plane //
//--------------------------------------------------//
//h, w(integer): height and width of the object //
// in pixels //
//lives(integer): how many lives the player has //
//ammo(integer): starting ammo of the player //
//sprites(array[Sprite]): sprites for the default //
// animation of the ship //
//fire(array[Sprite]): sprites for the firing //
// animation //
//death(array[Sprite]): sprites for the death //
// animation of the ship //
//colliders(array[array]): objects to test for //
// collisions //
//currentAnimation(array[Sprite]): the animation //
// sequence that is currently being animated //
//shot(boolean): whether or not the player's ship //
// has fired //
//--------------------------------------------------//
super(x, y);
this.w = 32;
this.h = 32;
this.lives = 3;
this.ammo = 100;
this.sprites = Sprite.load(0, 0, 32, 32, "right", 4);
this.fire = Sprite.load(0, 32, 32, 32, "right", 4);
this.death = Sprite.load(0, 192, 32, 32, "right", 5);
this.colliders = [enemies, enemyShots];
this.currentAnimation = this.sprites;
this.shot = false;
}
get lastSprite() {
//--------------------------------------------------//
//return-> boolean: whether or not the current //
// sprite is the last one it its animation cycle //
//--------------------------------------------------//
return (this.sprite % this.currentAnimation.length === this.currentAnimation.length - 1);
}
collide() {
//--------------------------------------------------//
//Checks the [this.colliders] array to see if a //
// collision has occurred //
//--------------------------------------------------//
//return-> boolean: true for collision //
//--------------------------------------------------//
//console.log(this.x, this.y, this.w, this.h);
for (let i = 0; i < this.colliders.length; i++) {
for (let j = 0; j < this.colliders[i].length; j++) {
if (this.x < this.colliders[i][j].x + this.colliders[i][j].w &&
this.x + this.w > this.colliders[i][j].x &&
this.y < this.colliders[i][j].y + this.colliders[i][j].h &&
this.y + this.h > this.colliders[i][j].y &&
!this.colliders[i][j].dead) {
//console.log(this.x, this.w, this.y, this.h);
//console.log(this.colliders[i][j]);
if (this.colliders[i] === enemies) {
showScore(++score);
this.colliders[i][j].die();
} else {
this.colliders[i].splice(j, 1);
}
//this.colliders[i].splice(j, 1);
playSfx(deathSound);
return true;
}
}
}
return false;
}
update(c) {
//--------------------------------------------------//
//The actions that need to be taken during each //
// loop of the game loop //
//--------------------------------------------------//
//c(integer): current frame count of the main loop //
//--------------------------------------------------//
//
//Update the sprite every other frame
if (c % 2 === 0) {
//
//If the special animation cycle is over, revert
// to the default animation
if (this.currentAnimation !== this.sprites && this.lastSprite) {
this.currentAnimation = this.sprites;
//
//If there are any queued actions to take, execute them
if (this.queuedAction !== null) {
this.queuedAction();
this.queuedAction = null;
}
}
this.sprite++;
}
//
//If the player is not dead, check for collisions
if (this.collide() && !this.dead) {
this.die();
}
//If the player is not dead, draw the sprite
if (!this.dead) {
this.draw(this.x, this.y);
}
}
move(x, y) {
//--------------------------------------------------//
//Updates the ship's position and prevents it from //
// leaving the bounds of the <canvas> //
//--------------------------------------------------//
//x, y(integer): How much to change the position //
// of the ship //
//--------------------------------------------------//
if (this.x + x < canvasWidth - 32 &&
this.x + x > 0) {
this.x = this.x + x;
}
if (this.y + y < canvasHeight - 64 &&
this.y + y > 0) {
this.y = this.y + y;
}
}
shoot() {
//------------------------------------------------//
//What to do when the player shoots //
//------------------------------------------------//
if (this.ammo > 0 && !this.shot) {
this.currentAnimation = this.fire;
this.queuedAction = function() {
playSfx(shotSound);
let newShot = new Shot();
shots.push(newShot);
showAmmo(--this.ammo);
}
} else {}//play empty ammo sound
}
die() {
//--------------------------------------------------//
//What to do when the player dies //
//--------------------------------------------------//
//
//Change the animation
this.currentAnimation = this.death;
this.sprite = 0;
this.shot = true;
//
//Run after the death animation
this.queuedAction = function() {
this.dead = true;
endLoop = true;
showLives(--this.lives);
//
//Clears the screen of any shots or enemies
for (let i = enemies.length - 1; i >= 0; i--) {
enemies.pop();
}
for (let i = shots.length - 1; i >= 0; i--) {
shots.pop();
}
for (let i = enemyShots.length - 1; i >= 0; i--) {
enemyShots.pop();
}
//
//If there are lives remaining, reposition the player
// and restart the gameLoop
// Otherwise, stop the bgm and go to the gameOverLoop
if (this.lives >= 0) {
this.x = 346;
this.y = 416;
setTimeout(function() {
gameInit();
}, 500);
} else {