-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1597 lines (1339 loc) · 53.6 KB
/
script.js
File metadata and controls
1597 lines (1339 loc) · 53.6 KB
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
//Creates variables used to create the canvas size and cell size used for ingame squares
let cellSize;
let colDet;
//-------------------------------------------------------
//Counters for in-game variables to be shown
let coinCounter = 0;
let levelCounter = 0;
let lives = 3;
let deathCounter = 0;
let score = 0;
let timer = 400;
let scoreStr = "";
let coinStr = "";
let scoreLength;
let coinLength;
//-------------------------------------------------------
//Used for loading in images/fonts/sounds and sets the game mode to the start screen when first called
let loading = true;
let mode = "start";
//-------------------------------------------------------
//Variables for images
let brickImg;
let questionBlockImg;
let floorImg;
let blockImg;
let blueBlockImg;
let pipeImgs = [];
let flagPoleImg;
let flagImg;
let smallPlayerImg;
let bigPlayerImg;
let deathImg;
let enemyImg;
let mushroomImg;
let bossBrickImg;
let fireBallImg;
let fireBlockImg;
let bossFloorImg;
let axeImg;
let bossImg;
//-------------------------------------------------------
//Arrays for objects
let player;
let platforms1 = [];
let platforms2 = [];
let floors = [];
let bricks = [];
let items = [];
let enemies = [];
let tempX = [];
let blocks = [];
let coins = [];
let pipes = [];
let fireballs = [];
let bossFloors = [];
let boss;
let axe;
let scores = [];
//Variable used for the font used in game
let font;
//-------------------------------------------------------
//Creating variables for horizontal movement
let playerSpeed = 0;
let maxSpeed = 3 / 50 * cellSize;
let jumpSpeed = 0;
//-------------------------------------------------------
//Other variables used for animations and set timers
let breaking = false;
let temp = [];
let wallSide;
let tpAnimation = false;
let underground = false;
let underTemp = false;
let outOfPipe = false;
let endFlag = false;
let endWalk = false;
let givenFlagScore = false;
let tempPlace1;
let tempPlace2;
let jumped = false;
//-------------------------------------------------------
function preload() { //Loads some fonts, images and sounds that are required at the start but not al since some other can be loaded afterwards, allowing
font = loadFont("images/font.ttf");
//-------------------------------------------------------
brickImg = loadImage("images/bricks.png");
underBrickImg = loadImage("images/underBrick.png");
questionBlockImg = loadImage("images/questionBlocks.png");
floorImg = loadImage("images/floor.png");
underFloorImg = loadImage("images/underFloor.png");
platformImg = loadImage("images/platform.png")
blockImg = loadImage("images/block.png");
blueBlockImg = loadImage("images/blueBlock.png");
coinImg = loadImage("images/coins.png");
pipeImgs.push(loadImage("images/pipe.png"));
pipeImgs.push(loadImage("images/pipe2.png"));
flagPoleImg = loadImage("images/flagpole.png");
flagImg = loadImage("images/flag.jpg");
smallPlayerImg = loadImage("images/smallMario.png");
bigPlayerImg = loadImage("images/bigMario.png");
deathImg = loadImage("images/deathImg.png");
enemyImg = loadImage("images/enemy.png");
uEnemyImg = loadImage("images/enemyD.png");
mushroomImg = loadImage("images/mushrooms.png");
//-------------------------------------------------------
bgMusic = loadSound("sounds/bgMusic.mp3");
breakBlock = loadSound("sounds/breakBlock.wav");
coinCol = loadSound("sounds/coin.wav");
deathSound = loadSound("sounds/death.wav");
eatShroom = loadSound("sounds/eatShroom.wav");
jumpSound = loadSound("sounds/jump.wav");
}
//-------------------------------------------------------
function setup() {
//Starts loading the rest of the files after the game loads up
fireBallImg = loadImage("images/fireBalls.png");
fireBlockImg = loadImage("images/brokenBlock.png");
bossBrickImg = loadImage("images/bossBrick.png");
bossFloorImg = loadImage("images/bossFloor.png");
axeImg = loadImage("images/finishAxe.png");
bossImg = loadImage("images/boss.png");
princessImg = loadImage("images/princess.png");
killEnemy = loadSound("sounds/killEnemy.wav");
spawnMushroom = loadSound("sounds/mushroomSpawn.wav");
enterPipe = loadSound("sounds/pipe.wav");
endLevelSound = loadSound("sounds/endLevel.wav");
flagDown = loadSound("sounds/flag.wav");
killEnemy = loadSound("sounds/killEnemy.wav");
spawnMushroom = loadSound("sounds/mushroomSpawn.wav");
enterPipe = loadSound("sounds/pipe.wav");
loading = false;
angleMode(DEGREES);
//Calculating Canvas Height
let canvasHeight = ceil(windowHeight / 16) * 14;
//Calculating Cell Size
cellSize = canvasHeight / 14;
//Calulation for future collision detection
colDet = cellSize / 4;
//Using cellSize to calulate width
let canvasWidth = cellSize * 16;
//Creates the canvas with the correct width and height
let cnv = createCanvas(canvasWidth, canvasHeight);
//Calcultes the position to move the canvas to
let cnvX = (windowWidth - width) / 2;
let cnvY = (windowHeight - height) / 2;
//Moves the Canvas
cnv.position(cnvX, cnvY);
//Adds the library to the game so that it can be used for collision detection of rotating objects
addScreenPositionFunction();
//-------------------------------------------------------
//Creating Objects
player = new Player();
for(let i = 0; i < 200; i++){
player.maxSpeed += player.gravity;
player.maxSpeed *= 0.9;
}
//Sets the frame rate to 60 to try keep a constant pace of my game
frameRate(60);
}
//-------------------------------------------------------
function draw() {
switch (mode) {
case "start":
//Draws the start menu
background(0);
fill(255);
textAlign(CENTER, CENTER);
textFont(font)
textSize(cellSize / 2)
if (frameCount % 60 < 30) { //Makes it so this bit of code inside only runs half to time, making the text inside flash
text("CLICK TO START", width / 2, height / 2);
}
break;
case "game": //Game is played when this is true
//-------------------------------------------------------
if (frameCount % 24 == 0) {//Creates the countdown for when the timer should decrement
if (!(endFlag) && !(endWalk)) { //Ensures the timer doesn't decrease when it shouldn't
timer--;//Decrements the timer
if (timer <= 0) {//Kills player once timer reaches 0
this.mini = true;
this.h = cellSize * .9
deathCounter = 0;
mode = "deathScreen";
deathSound.setVolume(0.3);
deathSound.play()
lives--;
}
}
}
//Draws Backround
noStroke();
if (underground || levelCounter == 3) { //Bg for underground parts
background(0);
} else {
background(146, 144, 255)//Bg for over ground parts
}
//-------------------------------------------------------
//Draws the information at the top of the screen each frame
topText();
//-------------------------------------------------------
//Sets gravity to normal at the start of every frame
player.gravity = 0.016 * cellSize;
for (b of bricks) { //Loops through bricks array
if (player.yVelocity > 0) {//Checks is player is moving downward
if (b.nextTo(player)) {//Checks if brick is next to the player
if (player.y != player.floor) {//Checks if player is off the ground
player.gravity = 0.004 * cellSize;//Changes gravity, making the player slide down an object
if (player.x == b.x + b.w) {//Checks what side of object player is on
wallSide = "right";
} else {
wallSide = "left";
}
}
}
}
}
for (b of blocks) { //Loops through blocks array
if (player.yVelocity > 0) { //Checks is player is moving downward
if (b.nextTo(player)) { //Checks if block is next to the player
if (player.y != player.floor) { //Checks if player is off the ground
player.gravity = 0.004 * cellSize; //Changes gravity, making the player slide down an object
if (player.x == b.x + b.w) { //Checks what side of object player is on
wallSide = "right";
} else {
wallSide = "left";
}
}
}
}
}
for (p of pipes) { //Loops through blocks array
if (player.yVelocity > 0) { //Checks is player is moving downward
if (p.nextTo(player)) { //Checks if pipe is next to the player
if (player.y != player.floor) { //Checks if player is off the ground
player.gravity = 0.004 * cellSize; //Changes gravity, making the player slide down an object
if (player.x == p.x + p.w) { //Checks what side of object player is on
wallSide = "right";
} else {
wallSide = "left";
}
}
}
}
}
//-------------------------------------------------------
//Calculating player speed
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Overground player movement
if (keyIsDown(65) && keyIsDown(68)) {
playerSpeed = 0; //If 'A' and 'D' are being held down, the speed is set to 0
} else {
if (keyIsDown(65)) { // 'A' key is being held
playerSpeed = 3 / 50 * cellSize; //Moves player right
} else if (keyIsDown(68)) {// 'D' key is being held
playerSpeed = -3 / 50 * cellSize; //Moves player left
} else {
playerSpeed = 0; //If neither 'A' or 'D' is being held, the speed is set to 0
}
}
if (!(outOfPipe || endFlag || endWalk || player.deathAnimation)) { //Checks to see if player isn't in an animation
if (keyIsDown(83)) { //Check if player is crouching
player.crouching = true;
player.h = cellSize * 0.9;
if (player.y == player.floor) { //Lets player still move through the air whilst crouching
playerSpeed = 0;
}
} else { //Sets player into a non-crouching position, since 'S' is no longer being held
player.crouching = false;
if (player.mini) { //Changes height based on if player is "mini" or not
player.h = cellSize * 0.9;
} else {
player.h = cellSize * 1.8;
}
}
}
//Check for sprinting
if (keyIsDown(16)) {
playerSpeed *= 9 / 5 //Incrase speed if player is sprinting
}
if (levelCounter == 3) { //Must only do on 3rd level since axe doesn't exist until then
if (axe.finished) { //Checks if player has touched the axe
playerSpeed = -3 / 50 * cellSize; //Forcibly moves the player to the right
player.speed = 0;
}
}
if (endWalk) { //Checks if player is walking off of the screen after going on a flag
playerSpeed = 0; //ensures camera doesn't move whilst player is walking to next level
}
if (levelCounter != 3) { //Doesn't happen on level 3 bc it doesn't have floors along the bottom
if (floors[0].x > 0) { //Checks if left-most floor is off the screen at all
playerSpeed = -(floors[0].x); //Changes player speed so that floor won't go off of the screen to the left
} else if (floors[0].x == 0) { //If left-most floors is directly on the esge of canvas
if (playerSpeed > 0) {//If player tries to move to the left
playerSpeed = 0;//Stops player moving to the left, so that the floor won't go off screen
}
}
} else { //Only if on third level
if (axe.finished) { //Checks if player has touched the final axe, finishing the game
if (bricks[683].x < width) { //Cheks if right-most brick on final level has reached the right of the canvas
playerSpeed = width - bricks[683].x; //Changes speed to make brick in line with right side of canvas
} else if (bricks[683].x == width) {
playerSpeed = 0 //Makes speed 0 so that it can no longer move, ending the game
//Draws text to indicate the game has ended
textAlign(CENTER, CENTER)
textSize(cellSize / 2)
text("THANK YOU MARIO!", width / 2, height / 4);
text("YOU SAVED THE PRINCESS!", width / 2, height / 2)
noLoop(); //Finishes the game, stopping the draw from looping
}
}
}
//Allows the player to walljump by adding the jump speed to the player speed
playerSpeed += jumpSpeed;
} else {
playerSpeed = 0; //If player is in underground movement zone, set playerSpeed to 0 so that camera doesn't move
}
if (outOfPipe || endFlag || player.deathAnimation || tpAnimation) { //Ensures player can't move if in an animation
playerSpeed = 0;
player.speed = 0;
}
//Drawing and moving the player vertically
player.move();
//-------------------------------------------------------
//Horizontal collision detection for bricks
for (b of bricks) { //Loops through bricks array
if (b.invis != true) { //Checks to see if brick is an invisible brick or not
if (b.aboutToHit(player)) { //Checks to see if player is about to hit brick from the side
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Checks what movement type to be used
if (playerSpeed < 0) { //Checks direction player is moving
playerSpeed = player.x + player.w - b.x; //Moves player to be directly next to brick
} else if (playerSpeed > 0) {
playerSpeed = player.x - (b.x + b.w); //Moves player to be directly next to brick
}
} else { //For other movement system
if (player.speed > 0) { //Checks direction player is moving
player.x = b.x - player.w; //Moves player to be directly next to brick
} else if (player.speed < 0) {
player.x = b.x + b.w; //Moves player to be directly next to brick
}
}
}
for (i of items) { //Loops through items array
if (b.aboutToHit(i)) { //Checks if item is about to hit a brick from the side
i.speed = -i.speed //Changes item speed to make it move in opposite direction
}
}
for (e of enemies) { //Loops through enemies array
if (b.aboutToHit(e)) { //Checks if enemy is about to hit brick from the side
e.speed = -e.speed; //Changes enemy speed to make it move in opposite direction
}
}
} else if (b.broken) { //If brick is invisible but has been broken
//---
//Same code as above ^^^
if (b.aboutToHit(player)) {
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) {
if (playerSpeed < 0) {
playerSpeed = player.x + player.w - b.x;
} else if (playerSpeed > 0) {
playerSpeed = player.x - (b.x + b.w);
}
} else {
if (player.speed > 0) {
player.x = b.x - player.w;
} else if (player.speed < 0) {
player.x = b.x + b.w;
}
}
}
for (i of items) {
if (b.aboutToHit(i)) {
i.speed = -i.speed
}
}
for (e of enemies) {
if (b.aboutToHit(e)) {
e.speed = -e.speed
}
}
}
//---
}
//-----------------------
//Horizontal collision detection for blocks
for (b of blocks) { //Loops through blocks array
if (b.aboutToHit(player)) { //Checks to see if player is about to hit block from the side
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Checks what movement type to be used
if (playerSpeed < 0) { //Checks direction player is moving
playerSpeed = player.x + player.w - b.x; //Moves player to be directly next to block
} else if (playerSpeed > 0) {
playerSpeed = player.x - (b.x + b.w); //Moves player to be directly next to block
}
} else { //For other movement system
if (player.speed > 0) { //Checks direction player is moving
player.x = b.x - player.w; //Moves player to be directly next to block
} else if (player.speed < 0) {
player.x = b.x + b.w; //Moves player to be directly next to block
}
}
}
for (i of items) { //loops through items array
if (b.aboutToHit(i)) { //Checks if item is about to hit block
i.speed = -i.speed; //Changes item speed to make it move in opposite direction
}
}
for (e of enemies) { //Loops through enemies array
if (b.aboutToHit(e)) { //Checks if enemy is about to hit block from the side
e.speed = -e.speed; //Changes enemy speed to make it move in opposite direction
}
}
}
//-----------------------
//Horizontal collision detection for pipes
for (p of pipes) { //Loops thorugh pipes array
if (p.aboutToHit(player)) { //Checks to see if player is about to hit pipe from the side
if (p.type == "vertical") { //Checks what type of pipe it is testing collision for
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Checks what movement type to be used
if (playerSpeed < 0) { //Checks direction player is moving
playerSpeed = player.x + player.w - p.x; //Moves player to be directly next to pipe
} else if (playerSpeed > 0) {
playerSpeed = player.x - (p.x + p.w); //Moves player to be directly next to pipe
}
} else {
if (player.speed > 0) { //Checks direction player is moving
player.x = p.x - player.w; //Moves player to be directly next to pipe
} else if (player.speed < 0) {
player.x = p.x + p.w; //Moves player to be directly next to pipe
}
}
} else { //If pipe is horizontal
if ((underground && levelCounter != 2) || (levelCounter == 2 && !underground)) {
player.x = p.x + cellSize * 2 + cellSize / 8 - player.w - 2; //Moves player to be directly next to pipe
//Other bits aren't needed due to nature of horizontal pipes
}
}
}
for (i of items) { //Loops through items array
if (p.aboutToHit(i)) { //Checks if item is about to hit a pipe from the side
i.speed = -i.speed; //Changes item speed to make it move in opposite direction
}
}
for (e of enemies) { //Loops through enemies array
if (p.aboutToHit(e)) { //Checks if enemy is about to hit pipe from the side
e.speed = -e.speed; //Changes enemy speed to make it move in opposite direction
}
}
}
//-----------------------
//Horizontal collision detection for floors
for (f of floors) { //Loops through floors array
if (f.aboutToHit(player)) { //Checks to see if player is about to hit floor from the side
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Checks what movement type to be used
if (playerSpeed < 0) { //Checks direction player is moving
playerSpeed = player.x + player.w - f.x; //Moves player to be directly next to floor
} else if (playerSpeed > 0) {
playerSpeed = player.x - (f.x + f.w); //Moves player to be directly next to floor
}
} else { //For other movement system
if (player.speed > 0) { //Checks direction player is moving
player.x = f.x - player.w; //Moves player to be directly next to floor
} else if (player.speed < 0) {
player.x = f.x + f.w; //Moves player to be directly next to floor
}
}
}
for (i of items) { //Loops thorugh items array
if (f.aboutToHit(i)) { //Checks if item is about to hit a floor from the side
i.speed = -i.speed; //Changes item speed to make it move in opposite direction
}
}
for (e of enemies) { //Loops through enemies array
if (f.aboutToHit(e)) { //Checks if enemy is about to hit a floor from the side
e.speed = -e.speed; //Changes enemy speed to make it move in opposite direction
}
}
}
//-----------------------
//Horizontal collision detection for fireball blocks
for (f of fireballs) { //Loops thorugh fireballs array
if (f.aboutToHit(player)) { //Checks to see if player is about to hit fireball block from the side
if ((!underground && levelCounter != 2) || (underground && levelCounter == 2)) { //Checks what movement type to be used
if (playerSpeed < 0) { //Checks direction player is moving
playerSpeed = player.x + player.w - f.x; //Moves player to be directly next to fireball block
} else if (playerSpeed > 0) {
playerSpeed = player.x - (f.x + f.w); //Moves player to be directly next to fireball block
}
} else { //For other movement system
if (player.speed > 0) { //Checks direction player is moving
player.x = f.x - player.w; //Moves player to be directly next to fireball block
} else if (player.speed < 0) {
player.x = f.x + f.w; //Moves player to be directly next to fireball block
}
}
}
for (i of items) { //Loops through items array
if (f.aboutToHit(i)) { //Checks if item is about to hit a fireball block from the side
i.speed = -i.speed; //Changes item speed to make it move in opposite direction
}
}
for (e of enemies) {
if (f.aboutToHit(e)) { //Checks if enemy is about to hit a fireball block from the side
e.speed = -e.speed; //Changes enemy speed to make it move in opposite direction
}
}
}
//-----------------------
//Collision detection between 2 enemies, making them bounce off of each other
for (let i = 0; i < enemies.length; i++) {
for (let j = 0; j < enemies.length; j++) {
//Nested for loop to loop through the array twice for each case
if (i != j) { //Ensures it isnt trying to collide an enemy with itself
if (enemies[i].aboutToHit(enemies[j])) { //Checks if 1 enemy is about to hit another
enemies[i].speed = -enemies[i].speed; //Changes enemy speed to make it move in opposite direction
enemies[j].speed = -enemies[j].speed; //Changes enemy speed to make it move in opposite direction
}
}
}
for (j of items) { //Loops through items array | cant use 'i' as a variable since already in use for intial for loop
if (enemies[i].aboutToHit(j)) { //Checks if enemy is about to hit an item
j.speed = -j.speed; //Changes item speed to make it move in opposite direction
enemies[i].speed = -enemies[i].speed; //Changes enemy speed to make it move in opposite direction
}
}
}
//-------------------------------------------------------
//Drawing objects and verical collision detection
//Moving Objects Horizontally around player
//Put here so player is drawn on top of flag, rest is at bottom
flag.show(); //Draws flag
if (!player.deathAnimation) {
player.showDetection(); //Checks if player should be drawn, and then draws it if it can
}
player.floor = height + player.h; //sets player's floor value each frame so it resets and recalulates it
if (flag.x < width && flag.x > 0) { //Checks if flag is on screen
player.ceil = -player.h; //Allows player to jump into flag normally
} else {
player.ceil = player.h //Means player can't go ono top of map
}
//-------------------------------------------------------
for (p of pipes) {//Loops through pipes array
if (p.below(player)) { //Checks if pipe is below the player
if (p.type == "vertical") { //Checks type of pipe to ensure correct floor value is given
player.floor = p.y - p.fullHeight; //Allows player to stand on top of pipe
} else { //pipe type == horizontal
player.floor = p.y - cellSize * 2; //Allows player to stand on top of pipe
}
if (p.tp) { //Checks if pipe the player is standing on can be used to teleport
if (p.type == "vertical") { //Ensure u cant crouch on top of a horizontal pipe and teleport
if (player.crouching) { //Checks if player is crouching
//Starts teleportation animation and moves player to middle of pipe
playerSpeed = 1.5 * cellSize - p.x;
player.floor = height + cellSize * 2;
tpAnimation = true;
}
}
}
}
if (p.type == "horizontal") {//Checks if any pipe is horizontal, since theyre all telportable
if (p.goingInHorizontalPipe(player)) { //Checks if player is about to enter a horizontal pipe
if (keyIsDown(68)) { //Checks if player is holding 'D' key
player.horizontalEntry = true; //Starts teleportation animation
tpAnimation = true;
}
}
}
if (tpAnimation) { //If player is in middle of teleporting animation
if (p.inside(player)) { //Checks if player has gone far enough inside pipe
//Teleports the player to desired location
enterPipe.setVolume(0.3);
enterPipe.play();
player.speed = 0;
player.x = cellSize * 2 + cellSize / 8;
if (!underground) {
playerSpeed = p.tpDistance * cellSize;
player.y = 0;
tpAnimation = false;
player.horizontalEntry = false;
underTemp = true;
} else {
player.y = height - cellSize * 2
playerSpeed = p.tpDistance * cellSize;
tpAnimation = false;
player.horizontalEntry = false;
underground = false;
outOfPipe = true;
}
}
}
for (i of items) {
if (p.below(i)) {
i.floor = p.y - p.h; //Ensures items can be on top of vertical pipes, horizontal not needed
}
}
for (e of enemies) {
if (p.below(e)) {
e.floor = p.y - p.h; //Ensures enemies can be on top of vertical pipes, horizontal not needed
}
}
if (outOfPipe) { //Checks if player is exiting a pipe
player.y -= 1 / 150 * cellSize; //Pushes player out of pipe
if (p.below(player)) { //If player has risen up enough
//End animation and move player to ensure it's at the right position
outOfPipe = false;
player.y = p.y - p.h * cellSize
player.speed = 0;
}
}
}
for (p of pipes) {
if (p.x > -cellSize * 4 && p.x < width + cellSize) { //Checks if pipe is on screen
p.show(); //Draws pipe
}
p.move(); //Moves pipes every frame
}
//-------------------------------------------------------
for (i of items) {
if (i.x > -cellSize && i.x < width + cellSize) { //Checks if item is on screen
i.show(); //Draws item
}
i.move(); //Moves item every frame
i.floor = height + i.h; //Sets item floor value
}
for (let i = items.length - 1; i >= 0; i--) {
if (items[i].y >= height + items[i].h) { //Checks if item is off map
items.splice(i, 1); //Removes item from items array
break;
}
if (items[i].colliding(player)) { //Checks if player has collected item
switch (items[i].type) { //Runs code based on what item has been picked up
case "coin":
//Player collects coin
coinCounter++;
items.splice(i, 1);
score += 200;
scores.push(new Score(200, false));
coinCol.setVolume(0.05);
coinCol.play();
break;
case "mushroom":
//PLayer collects mushroom
player.h = cellSize * 1.8;
player.mini = false;
player.frame = 17;
score += 1000;
scores.push(new Score(1000, false));
items.splice(i, 1);
eatShroom.setVolume(0.3);
eatShroom.play();
break;
case "1UP":
//Player collects 1UP
lives++;
score += 1000;
scores.push(new Score(1000, false));
items.splice(i, 1);
eatShroom.setVolume(0.3);
eatShroom.play();
default:
break;
}
}
}
//-------------------------------------------------------
if (levelCounter == 3) { //Check if on third level, since axe is not made until then
//Move and show axe every frame
axe.move();
axe.show();
if (axe.colliding(player)) { //Checks if player is colliding with the axe
//Starts animations to finish game
axe.finished = true;
player.x = axe.x + axe.w;
bgMusic.pause();
endLevelSound.setVolume(0.5);
endLevelSound.play()
}
if (!axe.finished) { //If player hasn't collided with axe
if (axe.x < cellSize) { //Playe rhas gone past axe and axe has gone off of screen
axe.finished = true;
player.x = axe.x + axe.w;
bgMusic.pause();
endLevelSound.setVolume(0.3);
endLevelSound.play()
}
}
}
//-------------------------------------------------------
for (b of bricks) {
//Move bricks every frame
b.move();
if (b.x > -cellSize && b.x < width + cellSize) { //Checks if brick is on screen
b.show(); //If brick is on screen, it is drawn
}
if (b.invis != true) { //If brick is not invis
if (b.below(player)) {
player.floor = b.y - b.h; //Allows player to stand on bricks
}
for (i of items) {
if (b.below(i)) {
i.floor = b.y - b.h; //Allows items to be on top of bricks
}
}
for (e of enemies) {
if (b.below(e)) {
e.floor = b.y - b.h; //Allows enemies to stand on bricks
}
}
} else if (b.broken) { //If brick is invis but is also broken
if (b.below(player)) {
player.floor = b.y - b.h; //Allows player to stand on bricks
}
for (i of items) {
if (b.below(i)) {
i.floor = b.y - b.h; //Allows items to be on top of bricks
}
}
for (e of enemies) {
if (b.below(e)) {
e.floor = b.y - b.h; //Allows enemies to stand on bricks
}
}
}
if (b.above(player)) { //Checks if player is directly below brick
player.ceil = b.y + b.gap; //Changed players max y value so it can't go above the brick
if (player.top == player.ceil) { //If top of player is touching its max value whilst b.above(player) is true, the player is direwctly touching the brick from below
player.yVelocity = 0;//Stops player floating below a brick and immediately sends them down
}
}
}
//Adds location of blocks that have been touched into a temp array
for (let i = 0; i < bricks.length; i++) {
if (bricks[i].above(player)) {
if (player.yVelocity <= 0) {
if (player.top == player.ceil) {
temp.push(i);
breaking = true;
player.y++ //Fixes some weird issue with player staying on block for 2 frames
}
}
if (player.top == player.ceil) {
if (player.yVelocity < 0) {
player.yVelocity = 0;//Sets player velocity back to 0 again, just in case :)
}
}
}
}
//Breaks the blocks if necessary
if (breaking) { //Checks if a block needs to be broken
if (temp.length == 1) { //Only under 1 block
if (bricks[temp[0]].qBlock) { //If block is a question block
if (!(bricks[temp[0]].broken)) {
//Block is unbroken qBlock
bricks[temp[0]].destroy(); //Destroys Q block if it wasn't alreasy
}
} else { //Block is normal brick
bricks[temp[0]].destroy();
}
} else {
if (dist(bricks[temp[0]].x, bricks[temp[0]].y, player.x, player.top) <= dist(bricks[temp[1]].x, bricks[temp[1]].y, player.x, player.top)) {
//Left one closer
if (bricks[temp[0]].qBlock) {
if (!(bricks[temp[0]].broken)) {
//Left is qBlock and not broken
bricks[temp[0]].destroy();
} else { //Left is broken qBlock
bricks[temp[1]].destroy();
}
} else { // Left is normal brick
bricks[temp[0]].destroy();
}
} else { //Right one closer
if (bricks[temp[1]].qBlock) {
if (!(bricks[temp[1]].broken)) {
//Right is qBlock and not broken
bricks[temp[1]].destroy();
} else { //Right is broken qBlock
bricks[temp[0]].destroy();
}
} else { //Right is normal brick
bricks[temp[1]].destroy();
}
}
}
temp.splice(0, temp.length); //Empties temporary array for next frame
breaking = false;
}
//Deletes all normal bricks after their destruction animation is done
for (let i = bricks.length - 1; i >= 0; i--) {
if (bricks[i].broken && !(bricks[i].qBlock)) {
bricks.splice(i, 1);
score += 50;
scores.push(new Score(50, false));
breakBlock.setVolume(0.3);
breakBlock.play();
}
}
//-------------------------------------------------------
for (f of floors) {
f.move(); //moves floors every frame
if (f.x > -cellSize && f.x < width + cellSize) {
f.show(); //Draws floors if on screen
}
if (f.below(player)) {
player.floor = f.y - f.h;//Allows player to stand on floors
}
for (i of items) {
if (f.below(i)) {
i.floor = f.y - f.h; //Allows items to be on top of floors
}
}
for (e of enemies) {
if (f.below(e)) {
e.floor = f.y - f.h; //Allows normal enemies to stand on floors
}
}
if (levelCounter == 3) { //Checks if level counter is 3 since boss object isn;t created until then
if (f.below(boss)) {
boss.floor = f.y - f.h; //Allows boss to stand on floors, floor class used for final bridge on boss level but w/ different texture
}
}
}
for (let i = floors.length - 1; i >= 0; i--) {
if (levelCounter == 3) {
if (axe.finished) {
floors.splice(0, floors.length) //If player hits axe, it removes all floors from the floor array, destroying bridge and making boss fall to death
}
}
}
//-------------------------------------------------------
for (f of fireballs) {
//Shows and moves fireballs every frame
f.move();
f.show();
if (f.below(player)) {
player.floor = f.y - cellSize; //Allows player to stand on a fireball block
}
if (f.above(player)) {
player.ceil = f.y; //Stops player from going through a fireball block from below
if (player.top == player.ceil) {
player.yVelocity = 0; //Forces player to start going down immediately after hitting the block
}
}
if (f.colliding(player)) { //If fireballs hit the player