-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathGame.js
1247 lines (1123 loc) · 35 KB
/
Game.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
/*
Description: Game.js, this script contains all the javascript required for the game to work on the JS_game html page.
New levels can be added by:
- Adding an extra array of objects in LEVEL_ENEMIES
- Adding an extra object to the array LEVEL_PLAYER_CHARACTERS
- Adding an extra object to the array LEVEL_CLOUDS
- Making a new background or copying an existing one and incrementing the number by 1
- Increasing totalLevels variable by 1
Author: Open Source - Contributor list can be seen in GitHub
*/
const userKeys = {
LEFT: 37,
UP: 38,
RIGHT: 39,
DOWN: 40,
W: 87,
S: 83,
A: 65,
D: 68,
SPACE: 32,
P: 80,
M: 77,
C: 67
};
const specialKeys = {
8:'Backspace',
9:'Tab',
13:'Enter',
16:'Shift',
17:'Ctrl',
18:'Alt',
19:'Pause/break',
20:'Caps lock',
27: 'Escape',
32:'Space',
33:'Page up',
34:'Page down',
35:'End',
36:'Home',
37:'Left',
38:'Up',
39:'Right',
40:'Down',
45:'Insert',
46:'Delete',
91:'LeftwinKey',
92:'RightwinKey',
106:'Multiply',
107:'Add',
111:'Divide',
187:'Equal',
188:'Comma',
189:'Dash',
190:'Period',
191:'Slash',
192:'GraveAccent',
219:'OpenBracket',
220:'BackSlash',
221:'CloseBraket',
222:'SingleQuote'
};
const LEVEL_ENEMIES = [ //The y2 variable dictates how high up the unit starts
[{
name: 'FloatingFish',
width: 44,
height: 36,
y2: 200
}, {
name: 'zombie',
width: 40,
height: 45,
y2: 205
}],
[{
name: 'FloatingFish',
width: 44,
height: 36,
y2: 200
}, {
name: 'BlackBlob',
width: 60,
height: 53,
y2: 197
}],
[{
name: 'FloatingFish',
width: 44,
height: 36,
y2: 200
}, {
name: 'SlidingSkull',
width: 60,
height: 50,
y2: 200
}],
[{
name: 'FloatingFish',
width: 44,
height: 36,
y2: 200
}, {
name: 'CyclopsCrab',
width: 60,
height: 37,
y2: 220
}],
[{
name: 'FloatingFish',
width: 44,
height: 36,
y2: 200
}, {
name: 'CyclopsCrab',
width: 60,
height: 37,
y2: 220
}, {
name: 'SpinningSword',
width: 80,
height: 14,
y2: 170
}, {
name: 'ScarletStabber',
width: 70,
height: 60,
y2: 185
}]
];
const LEVEL_PLAYER_CHARACTERS = [{
name: 'good_guy',
x2: 100,
y2: 120
}, {
name: 'good_girl',
x2: 100,
y2: 120
}, {
name: 'good_girl',
x2: 100,
y2: 120
}, {
name: 'good_guy',
x2: 100,
y2: 120
}, {
name: 'ninja',
x2: 390, //Ideally this would call on the canvas width to place the character in the center--however, canvas.width is only created later.
y2: 120
}];
const LEVEL_CLOUDS = [{
name: 'cloud',
width: 60,
height: 34
}, {
name: 'cloud2',
width: 65,
height: 50
}, {
name: 'cloud3',
width: 60,
height: 40
}, {
name: 'cloud3',
width: 60,
height: 40
}, {
name: 'cloud3',
width: 60,
height: 40
}];
//END CONFIG
const font = 'Share Tech Mono';
const totalLevels = 5; //This constant is very important--it tells the game how many levels it has.
const coinWidth = 40;
const LEVEL_COMPLETION_TIME = 3000;
const MAX_VARIABLES = Math.floor(LEVEL_COMPLETION_TIME / 50); //Each of our arrays should be able to contain a maximum of 2 objects/second.
const FLYING = 0; //This movement type goes up and down as it travels, going from right to left.
const WALKING = 1; //This movement type goes in a straight line from right to left--or, in some cases, doesn't move.
const ROTATING = 2; //This movement type rotates in two dimensions, traveling from right to left.
const REVERSED = 3; //This movement type travels from left to right.
//flyUp says whether FLYING enemies are flying up or down--up if true, down if false.
//flyCounter sets the interval at which flyUp changes.
var flyUp = false;
var flyCounter = 0;
var audio; //Here we're just pre-defining our audio variables
var coinpickup;
var gameover;
var gamewon;
var jump;
var enemykilled;
var currentLevel = 4;
var collectedCoins = 0;
var currentCoins = 0;
var timeLeft; //Says how much time is left in the level--will be calculated based off of LEVEL_COMPLETION_TIME later.
var score = 0;
var currentScore = 0;
var playerCharacter;
var background;
var background2;
var backgroundDx = 0;
var xPos = -5;
var coinRotationValue = 0;
var scoreBoard;
var coinScoreBoard;
var coinScoreBoardImg;
var coinScoreBoardSupImg;
var highscoreBoard;
var startArrow1;
var startArrow2;
var startArrow3;
var switchArrow = 0;
var timeBoard;
var timeBoardImg;
var levelDisplay;
var enemyCharacters = [];
var coins = [];
var clouds = [];
var keysPressed = {
LEFT: false,
UP: false,
RIGHT: false,
DOWN: false,
P: false,
M: false,
W: false,
S: false,
A: false,
D: false
};
var gamePaused = false;
var displayOptionsModal = false;
var optionId= '';
let musicMuted = false;
let musicToggled = false; //this is just for muting music when game paused
let dir; // which way character faces. 1 is right, -1 is left
var highscore = 0;
function KeyDown(event) {
var key;
key = event.which;
keysPressed[key] = true;
//avoid auto-repeated keydown event
if(event.repeat)
return;
if(!displayOptionsModal){
if ((keysPressed[userKeys.DOWN] || keysPressed[userKeys.S]) && playerCharacter.duckCooldown === false) {
duck();
}
if((keysPressed[userKeys.LEFT] || keysPressed[userKeys.A]) && playerCharacter.leftCooldown === false){
moveLeft();
playerCharacter.leftCooldown = true;
}
if ((keysPressed[userKeys.RIGHT] || keysPressed[userKeys.D]) && playerCharacter.rightCooldown === false) {
moveRight();
playerCharacter.rightCooldown = true;
}
if((keysPressed[userKeys.UP] || keysPressed[userKeys.W]) && playerCharacter.hitGround && playerCharacter.duckCooldown === false){
if(playerCharacter.jumpCooldown === false){
moveUp();
}
}
if(keysPressed[userKeys.SPACE]){
restartGame();
}
if(keysPressed[userKeys.P]){
keysPressed[userKeys.P] = false;
pauseGame();
}
if(keysPressed[userKeys.M]) {
keysPressed[userKeys.M] = false;
muteMusic();
}
if(keysPressed[userKeys.C]) {
keysPressed[userKeys.C] = false;
resumeGame();
}
}else{
changeOption(key);
}
}
// Toggle music at 'M' key press
function muteMusic() {
musicMuted = !musicMuted;
var imgButton = document.getElementById('audioButton');
if (musicMuted) {
imgButton.src = 'Pictures/audioOff.png';
if (!gamePaused && !displayOptionsModal) { //If the game is running, just turn the audio off
audio.pause();
} else { //Otherwise, we need to change our musicToggled variable, so that the audio resumes properly with the game
musicToggled = false;
}
} else {
imgButton.src = 'Pictures/audioOn.png';
if (!gamePaused && !displayOptionsModal) {
audio.load();
} else {
musicToggled = true;
}
}
updateSoundPng(); //Ensures that the mute button in the options page remains updated
}
function pauseGame() {
gamePaused = !gamePaused;
}
function updateSoundPng(){
if(musicMuted){
document.getElementById('MImg').src = 'Pictures/audioOff.png';
}else{
document.getElementById('MImg').src = 'Pictures/audioOn.png';
}
}
function gameOptions(){
displayOptionsModal = !displayOptionsModal;
}
function dispMess(id,type){
if(type === 'SOUND'){
muteMusic();
}else{
document.getElementById('mess').innerHTML = 'Press the key you want to use for '+'"'+type+'"';
optionId= id;
}
}
function changeOption(key){
var chr = String.fromCharCode(key);
if(optionId!== '' && !Object.values(userKeys).includes(key)){
if(48 <= key && key <= 90 ){
document.getElementById(optionId).value = chr;
userKeys[optionId] = key;
}else if(key in specialKeys){
document.getElementById(optionId).value = specialKeys[key];
userKeys[optionId] = key;
}
}else if(keysPressed[userKeys.M] && optionId !== 'M'){
dispMess('M','SOUND');
}else{
document.getElementById('mess').innerHTML = 'You can\'t use this key';
}
}
function updateBackgroundDx(){
if(keysPressed[userKeys.LEFT] || keysPressed[userKeys.A]) {
backgroundDx = -5;
}else if(keysPressed[userKeys.RIGHT] || keysPressed[userKeys.D]) {
backgroundDx = 5;
}else{
backgroundDx = 0;
}
}
function KeyUp(event) {
var key;
key = event.which;
keysPressed[key] = false;
switch (key) {
case userKeys.UP:
case userKeys.W:
playerCharacter.speedY += playerCharacter.gravity;
playerCharacter.jumpCooldown = false;
break;
case userKeys.LEFT:
case userKeys.A:
if (keysPressed[userKeys.RIGHT]||keysPressed[userKeys.D]) {
moveRight();
} else {
playerCharacter.speedX = 0;
}
playerCharacter.leftCooldown = false;
break;
case userKeys.RIGHT:
case userKeys.D:
if (keysPressed[userKeys.LEFT]||keysPressed[userKeys.A]) {
moveLeft();
} else {
playerCharacter.speedX = 0;
}
playerCharacter.rightCooldown = false;
break;
case userKeys.DOWN:
case userKeys.S:
if(playerCharacter.hitGround && playerCharacter.duckCooldown === true){//this if statement is used so that the playercharacter doesnt increase in size when DOWN or S is pressed while character is in the air
playerCharacter.height = playerCharacter.height * 2;
playerCharacter.duckCooldown = false;
}
}
updateBackgroundDx();
}
function showInstructions(){
gameArea.init();
//background
background = new Component();
background.init(gameArea.canvas.width, gameArea.canvas.height, 'Pictures/background_1.jpg', 0, 0, 'image', WALKING, true);
var modal = document.getElementById('instructionsModal');
modal.style.display = 'block';
}
function initialize_game() {
currentLevel = 4;
collectedCoins = 0;
currentCoins = 0;
score = 0;
currentScore = 0;
var coinMessage = document.getElementById('coinMessage');
var pointsMessage = document.getElementById('pointsMessage');
if (coinMessage) {
var levelTransitionModalContent = document.getElementById('levelTransitionModalContent');
levelTransitionModalContent.removeChild(coinMessage);
levelTransitionModalContent.removeChild(pointsMessage);
}
audio = document.getElementById('bgm');
audio.autoplay = true;
audio.loop = true;
if (!musicMuted) {
audio.load();
}
startLevel();
}
function startLevel() {
//Synchronizes the start coordinates of enemy characters
flyUp = false;
flyCounter = 0;
dir = 1; //Begin facing to the right
xPos = -5;
//player character
playerCharacter = new Component();
let char = LEVEL_PLAYER_CHARACTERS[currentLevel - 1];
playerCharacter.init(60, 70, `Pictures/${char.name}.png`, char.x2, char.y2, 'image', WALKING, undefined, char.name);
playerCharacter.jumpCooldown = false; //These cooldowns let our system know whether a certain key has recently been
playerCharacter.leftCooldown = false; //pressed--"false" means that the key is not on cooldown and should be
playerCharacter.rightCooldown = false;//acknowledged normally.
playerCharacter.duckCooldown = false;
//background
background = new Component();
background2 = new Component();
background.init(gameArea.canvas.width, gameArea.canvas.height, `Pictures/background_${currentLevel}.jpg`, -50, 0, 'image', WALKING);
background2.init(gameArea.canvas.width, gameArea.canvas.height, `Pictures/background_${currentLevel}_reverse.jpg`,850, 0, 'image', WALKING);
//score
scoreBoard = new Component();
scoreBoard.init('20px', font, 'black', 250, 40, 'text', WALKING);
//collected Coins
coinScoreBoard = new Component();
coinScoreBoard.init('20px', font, 'black', 450, 40, 'text', WALKING);
coinScoreBoardImg = new Component();
coinScoreBoardImg.init(22, 22, 'Pictures/coin.png', 420, 21, 'image', WALKING);
coinScoreBoardSupImg = new Component();
coinScoreBoardSupImg.init(40, 40, 'Pictures/stars.png', 412, 10, 'image', WALKING);
highscoreBoard = new Component();
highscoreBoard.init('20px', 'Consolas', 'black', 20, 40, 'text', WALKING);
highscoreBoard.text = 'HIGHSCORE:' + highscore;
//startArrow
startArrow1 = new Component();
startArrow2 = new Component();
startArrow3 = new Component();
startArrow1.init(90, 70, 'Pictures/blackArrow.png', 60, 125, 'image', 1);
startArrow2.init(90, 70, 'Pictures/blackArrow.png', 30, 125, 'image', 1);
startArrow3.init(90, 70, 'Pictures/blackArrow.png', 0, 125, 'image', 1);
//current time left in the given level
timeBoard = new Component ();
timeBoard.init('20px', font, 'black', 830, 40, 'text', WALKING);
timeBoardImg = new Component();
timeBoardImg.init(22, 22, 'Pictures/clock.png', 800, 21, 'image', WALKING);
//current level display
levelDisplay = new Component();
levelDisplay.init('20px', font, 'black', 670, 40, 'text', WALKING);
//Loop for creating new enemy characters setting a random x coordinate for each. Creates a maximum of 2 enemies/second.
for (let i = 0; i < MAX_VARIABLES; i++) {
enemyCharacters[i] = new Component();
var x = Math.floor((Math.random() * (i * (gameArea.canvas.width / 2))) + ((gameArea.canvas.width / 2) * i + (gameArea.canvas.width * 1.25)));
//moveType describes the type of enemy: flying (0), walking (1), rotating (2), entering from the left (3)...
//when you want to add a new type of enemy, increment the number inside the Math.random and
//insert in the correct case the enemy
var moveType = Math.floor(Math.random() * (LEVEL_ENEMIES[currentLevel - 1].length));
let enemy = LEVEL_ENEMIES[currentLevel - 1][moveType];
if (moveType === REVERSED) {
//These enemies enter offscreen from the left, and have roughly the reverse of the normal formula.
x = Math.floor(Math.random() * (-i * (gameArea.canvas.width / 2)));
}
enemyCharacters[i].init(enemy.width, enemy.height, `Pictures/${enemy.name}.png`, x, enemy.y2, 'image', moveType);
}
//Loop for creating new clouds setting a random x coordinate for each. Creates a maximum of 2 clouds/second.
for (let i = 0; i < MAX_VARIABLES; i++) {
let x = Math.floor((Math.random() * (-600 + i * 450) + 1));
clouds[i] = new Component();
let cloud = LEVEL_CLOUDS[currentLevel - 1];
clouds[i].init(cloud.width, cloud.height, `Pictures/${cloud.name}.png`, x, 40, 'image', WALKING);
}
//Generates new coins at random positions. Creates a maximum of 2/second.
for (let i = 0; i < MAX_VARIABLES; i++) {
let x = Math.floor(((Math.random() + 1) * gameArea.canvas.width) + (i * gameArea.canvas.width / 2));
var y = Math.floor(Math.random() * 150 + 30); //150 is canvas height - baseline(150) - char height - 30 (space on top)
coins[i] = new Component();
coins[i].init(coinWidth, coinWidth, 'Pictures/coin.png', x, y, 'image', WALKING);
}
//call start function
gameArea.init();
gameArea.start();
}
/**
* @type {{canvas: Element, start: gameArea.start, clear: gameArea.clear, stop: gameArea.stop}}
*/
var gameArea = {
init: function() {
this.canvas = document.getElementById('canvas');
this.canvas.width = 900;
this.canvas.height = 400;
this.context = this.canvas.getContext('2d');
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
document.body.insertBefore(document.getElementById('banner'), document.body.childNodes[0])
this.time = 0;
this.bonusActiveTime = 1500; //The actual number isn't important, we just want to make sure
this.coinScoreActiveTime = 1500; //that the flash functions won't activate immediately
this.coinScoreInterval = null;
},
start: function() {
this.frameNo = 0;
this.time = 0;
// hide modals
var modals = document.getElementsByClassName('modal');
for (let i = 0; i < modals.length; i++) {
var modal = modals[i];
modal.style.display = 'none';
}
//update interval
this.interval = setInterval(updateGameArea, 20);
},
//function used for refreshing page
clear: function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
//function used for stopping the game
stop: function() {
clearInterval(this.interval);
}
};
function Component() {
this.init = function(width, height, color, x, y, dataType, moveType, initialShow = false, charName = undefined) {
this.moveType = moveType; //This seems to mainly describe the movement type of an enemy
this.alive = true;
this.alive = true;
this.color = color;
//Assigns data type of the variable (usually an image).
this.dataType = dataType;
this.ctx = gameArea.context;
if (dataType === 'image') {
this.image = new Image();
this.image.src = this.color;
this.image.width = width;
this.image.height = height;
if (charName) {
this.imageMirror = new Image();
this.imageMirror.src = `Pictures/${charName}_left.png`;
this.imageMirror.width = width;
this.imageMirror.height = height;
}
if (initialShow) {//This is for things we want displaying before the game starts--basically the background
var imgCopy = this.image;
var ctxCopy = this.ctx;
this.image.onload = function() {
ctxCopy.drawImage(imgCopy, this.x, this.y, this.width, this.height);
};
}
}
this.width = width;
this.initHeight = height; // to get squeezed height later
this.alpha = 1; //This variable decrees how much an object is "faded"--1 is fully displayed, 0 is gone.
this.height = height;
//change Components position
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.orignX = x;
this.gravity = 1.5;
//indicates if the character is on the ground or not
this.hitGround = true;
this.doubleJumpAllowed = true;
//angle
this.angle = 0;
};
//function to decide to decide what to display on screen, text, image or fill color
this.update = function() {
if (this.dataType === 'image') {
this.ctx.globalAlpha = this.alpha;
if (this.angle !== 0) {
this.ctx.save();
this.ctx.translate(this.x + this.width / 2, this.y + this.height / 2);
this.ctx.rotate(this.angle);
this.ctx.translate(-this.x - this.width / 2, -this.y - this.height / 2);
this.ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
this.ctx.restore();
} else {
this.ctx.drawImage(this.image, this.x, this.y, this.width, this.height);
}
} else if (this.dataType === 'text') {
this.ctx.font = this.width + ' ' + this.height;
this.ctx.fillStyle = this.color;
this.ctx.fillText(this.text, this.x, this.y);
} else {
this.ctx.fillStyle = this.color;
this.ctx.fillRect(this.x, this.y, this.width, this.height);
}
};
//This function manages the scrolling backgrounds
this.moveBackgrounds = function(background2){
if(0 <= xPos){
xPos += backgroundDx;
this.x -= backgroundDx;
background2.x -= backgroundDx;
}else{
backgroundDx = 0;
}
if(this.x <= -900){this.x = 900;}
else if(background2.x <= -900){background2.x = 900;}
else if(900 <= this.x){this.x = -900;}
else if(900 <= background2.x){background2.x = -900;}
else if(900 < Math.abs(this.x)+Math.abs(background2.x)){
if(Math.abs(background2.x) < Math.abs(this.x)){
this.x += (0 < this.x)?-5:5;
}else{
background2.x += (0 < background2.x)?-5:5;
}
}
};
//enemy character collision function
this.crashWith = function(otherobj) {
var left = this.x;
var right = this.x + (this.width);
var top = this.y;
var bottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if ((bottom < othertop + 10) ||
(top > otherbottom - 20) ||
(right < otherleft + 15) ||
(left > otherright - 15)) {
crash = false;
}
return crash;
};
//This function tells us whether the player character (or any other object) has jumped on another object
this.jumpsOn = function(otherobj) {
var bottomY = this.y + (this.height);
var farX = this.x + this.width;
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var smoosh = false;
if ((bottomY > othertop - 5) &&
(bottomY < (othertop + 20)) &&
(farX > otherleft) &&
(this.x < otherright)) {
smoosh = true;
//When the player smooshes an enemy, we send them up
moveUp('hit');
}
return smoosh;
};
//gravity property
this.newPos = function() {
this.y += this.speedY; //increment y position with his speed
this.speedY += this.gravity; //increment the y speed with the gravity
this.x += this.speedX;
this.hitBottom();
};
//set floor on canvas
this.hitBottom = function() {
var rockbottom = gameArea.canvas.height - this.height - 150;
if (this.y > rockbottom) {
this.y = rockbottom;
this.hitGround = true;
this.doubleJumpAllowed = true;
}
};
this.setAlive = function(alive) {
this.alive = alive;
};
this.isAlive = function() {
return this.alive;
};
this.setX = function(x){
this.x = x;
};
this.getX = function(){
return this.x;
};
this.getOrignX = function(){
return this.orignX;
};
this.getImgSrc = function(){
return this.image.src;
};
this.setSrc = function(src){
this.image.src = src;
};
//This is a rotation function for coins only, allowing them to rotate in 3 dimensions
this.rotation = function(){
if(coinRotationValue === 0){
this.setSrc('Pictures/coin.png');
}else if(coinRotationValue === 10){
this.setSrc('Pictures/coin2.png');
}else if(coinRotationValue === 20){
this.setSrc('Pictures/coin3.png');
}else if(coinRotationValue === 30){
this.setSrc('Pictures/coin4.png');
}
};
//check if there was a change in direction character is facing
// newDir takes either -1 (left move) or 1 (right move)
this.changeDir = function(newDir) {
if (dir !== newDir) {
[playerCharacter.image, playerCharacter.imageMirror] = [playerCharacter.imageMirror, playerCharacter.image];
dir = newDir;
}
};
this.coinDisappear = function(){
this.y += -2;
this.alpha -= 0.03;
if(this.alpha < 0){
this.alpha = 0;
}
};
}
function gameOver() {
interval && clearInterval(interval);
//adding score to list of highscores
if(highscore < score){
highscore = score;
}
var modal = document.getElementById('gameOverModal');
modal.style.display = 'block';
audio = document.getElementById('bgm');
audio.pause();
if (!musicMuted) {
gameover = document.getElementById('gameover');
gameover.autoplay = true;
gameover.load();
}
}
function restartGame() {
gameArea.stop();
initialize_game();
}
function gameComplete() {
var modal = document.getElementById('gameCompleteModal');
modal.style.display = 'block';
gameArea.stop();
if(highscore < score){
highscore = score;
}
if (!musicMuted) {
audio = document.getElementById('bgm');
audio.pause();
gamewon = document.getElementById('gamewon');
gamewon.autoplay = true;
gamewon.load();
}
}
//Adjust character to a valid position if it moves out of border
function correctCharacterPos() {
if (playerCharacter.y < 0) {
playerCharacter.speedY = 0;
playerCharacter.y = 0;
}
if (playerCharacter.x < 0) {
playerCharacter.speedX = 0;
playerCharacter.x = 0;
}
if (playerCharacter.x > gameArea.canvas.width - playerCharacter.width) {
playerCharacter.speedX = 0;
playerCharacter.x = gameArea.canvas.width - playerCharacter.width;
}
if (playerCharacter.y > gameArea.canvas.height - playerCharacter.height) {
playerCharacter.speedY = 0;
playerCharacter.y = gameArea.canvas.height - playerCharacter.height;
}
}
function flashScore() {
if (scoreBoard.color === 'black') {
scoreBoard.color = 'white';
} else {
scoreBoard.color = 'black';
}
if (gameArea.bonusActiveTime > 1200) {
scoreBoard.color = 'black';
clearInterval(gameArea.bonusInterval);
}
gameArea.bonusActiveTime += 150;
}
function flashCoinScore() {
coinScoreBoardSupImg.update();
if (coinScoreBoard.color === 'black') {
coinScoreBoard.color = 'white';
coinScoreBoardSupImg.alpha = 1;
} else {
coinScoreBoard.color = 'black';
coinScoreBoardSupImg.alpha = 0;
}
if (gameArea.coinScoreActiveTime > 1200) {
coinScoreBoard.color = 'black';
coinScoreBoardSupImg.alpha = 0;
clearInterval(gameArea.coinScoreInterval);
}
gameArea.coinScoreActiveTime += 150;
}
function flashStartArrow(){
switchArrow++;
if(switchArrow < 30){
startArrow3.setSrc('Pictures/goldArrow.png');
startArrow2.setSrc('Pictures/blackArrow.png');
startArrow1.setSrc('Pictures/blackArrow.png');
}else if(switchArrow < 60){
startArrow3.setSrc('Pictures/blackArrow.png');
startArrow2.setSrc('Pictures/goldArrow.png');
}else if(switchArrow < 90){
startArrow2.setSrc('Pictures/blackArrow.png');
startArrow1.setSrc('Pictures/goldArrow.png');
}else{
switchArrow = 0;
}
startArrow1.setX(startArrow1.getOrignX()-xPos);
startArrow2.setX(startArrow2.getOrignX()-xPos);
startArrow3.setX(startArrow3.getOrignX()-xPos);
}
//Update game area for period defined in game area function, current 20th of a millisecond (50 times a second)
function updateGameArea() {
let pausemodal = document.getElementById('gamePauseModal');
let optionsModal = document.getElementById('optionsModal');
if (gamePaused) {
pausemodal.style.display = 'block';
if (!musicMuted) { //Then mute music, and keep musicToggled on so that we know it's not muted for real
audio.pause();
musicToggled = true;
}
return;
} else if (displayOptionsModal){
optionsModal.style.display = 'block';
if (!musicMuted) {
audio.pause();
musicToggled = true;
}
return;
} else {
pausemodal.style.display = 'none';
optionsModal.style.display = 'none';
if (musicToggled) { //Then unmute the music, and cancel musicToggled so that this won't re-trigger
audio.load();
musicToggled = false;
}
}
//when frame number reaches 3000 (point at which obstacles end) end level
//check current level, if more than 5 (because there are five levels currently), show game complete modal
if (gameArea.time >= LEVEL_COMPLETION_TIME) {
gameArea.stop();
if (currentLevel === totalLevels) gameComplete();
else {
currentLevel++;
var levelTransitionModal = document.getElementById('levelTransitionModal');
levelTransitionModal.style.display = 'block';
var levelTransitionModalContent = document.getElementById('levelTransitionModalContent');
levelTransitionModalContent.innerHTML += `<p id="coinMessage" class="levelTransitionMessage">Coins earned: ${currentCoins}</p>`;
levelTransitionModalContent.innerHTML += `<p id="pointsMessage" class="levelTransitionMessage">Points earned: ${currentScore}</p>`;
}
}
for (let i = 0; i < enemyCharacters.length; i++){
if(enemyCharacters[i].isAlive()) {
if(playerCharacter.jumpsOn(enemyCharacters[i])){
enemyCharacters[i].setAlive(false);
incrementScore(100*currentLevel);
gameArea.bonusActiveTime = 0;
gameArea.bonusInterval = setInterval(flashScore, 150);
if (!musicMuted) {
enemykilled = document.getElementById('enemykilled');
enemykilled.autoplay = true;
enemykilled.load();
}
} else if (playerCharacter.crashWith(enemyCharacters[i])){
backgroundDx = 0;
gameArea.stop();
gameOver();
}
}
}
coinRotationValue++; //We update the rotation value for the coins before updating the coins
if (coinRotationValue >= 40){
coinRotationValue = 0;
}
//loop for coin collision
for (let i = 0; i < coins.length; i++) {
if (coins[i].isAlive()){
if (playerCharacter.crashWith(coins[i])){
coins[i].setSrc('Pictures/stars.png');
//increase collected coins counter
collectedCoins++;
currentCoins++;
incrementScore(50 * currentLevel);
coins[i].setAlive(false);
//animate coin score board
gameArea.coinScoreActiveTime = 0;
gameArea.coinScoreInterval = setInterval(flashCoinScore, 150);
if(!musicMuted){
coinpickup = document.getElementById('coinpickup');
coinpickup.autoplay = true;
coinpickup.load();
}
}else{
coins[i].rotation();
}
}
}