-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
1126 lines (999 loc) · 36.7 KB
/
index.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
import { Wave, Standard as StandardEnemy, Rapid, Tank, Spawner, Stunner, Boss } from "./enemy.js";
import { Tower, Standard, Freezer, Poisoner, Bullet, towerCosts } from "./tower.js";
import { UIHandler } from "./ui-handler.js";
import { WINDOW_WIDTH, WINDOW_HEIGHT,
TOWER_LIMIT, DEFAULT_CURRENCY, DEFAULT_WAVE_INIT_TIME,
DEFAULT_HEALTH, LEVELS, MAPS, ENEMIES } from './config.js';
// ---------------------------------------------------------------------
// GLOBAL VARIABLES
// ---------------------------------------------------------------------
const canvasWidth = window.innerWidth;
const canvasHeight = window.innerHeight;
// STATE VARIABLES -----------
// 0 - main menu
// 1 - start game
var gameMode = 0;
let beginGame = false;
let gameOver = false;
var levelComplete = false;
let playSound = false;
let currentWave = 0;
// ENTITIES -----------
let enemies = [];
let towers = [];
let bullets = [];
// PLAYER RESOURCES -----------
let totalCurrency = DEFAULT_CURRENCY;
let totalHealth = DEFAULT_HEALTH;
// MISC VARIABLES -----------
// Tutorial States
let tutorialTask = 0;
let task3Currency = 1000000;
let waitBeforeNextTask = 0;
// let dragTower = null;
let towerToPlace = null;
const towerLimit = TOWER_LIMIT;
// checks if wave is over
// can cause error if new ways that enemies disapear arise so keep in mind
let initNextWave = DEFAULT_WAVE_INIT_TIME + 5;
let nextWaveCheck = {
amount: 0
}
// checks for stunned towers
let stunCooldown = {
amount: 0,
trigger: 400
}
const uiHandler = new UIHandler(WINDOW_WIDTH, WINDOW_HEIGHT);
let debug
let game;
export var mapID = 0;
// needs to be generalized for all levels
var waveAmount = LEVELS[mapID].LEVEL_DATA.length;
// ---------------------------------------------------------------------
// HELPER FUNCTIONS
// ---------------------------------------------------------------------
function getSelectedTower() {
for (let t of towers) {
if (t.selected) {
return t;
}
}
}
function fireBullets() {
// Generate bullets for each tower
for (let t of towers) {
// Skip if tower can't fire
if (!t.canFire()) {
continue;
}
let shortestDistance = Infinity;
let closestEnemy = null;
for (let e of enemies) {
let xDist = e.x - t.x;
let yDist = e.y - t.y;
let distance = sqrt(xDist * xDist + yDist * yDist);
if (distance < t.range && distance < shortestDistance) {
shortestDistance = distance;
closestEnemy = e;
}
}
if (closestEnemy !== null) {
bullets.push(t.fire(closestEnemy));
}
}
}
function dealDamage() {
for (let i = 0; i < enemies.length; i++) {
enemies[i].damageTowers(towers);
}
for (let i = 0; i < towers.length; i++) {
if (towers[i].health <= 0) {
towers.splice(i, 1);
}
}
}
// Select Map Function
function selectMap(mapID) {
switch (mapID) {
case 0:
mapImg = loadImage('Maps/Space Map 1.png');
currentLevelMusic = level1Music;
currentLevelMusic.setVolume(0.1);
break;
case 1:
mapImg = loadImage('Maps/Space Ship Map.png');
currentLevelMusic = level2Music;
currentLevelMusic.setVolume(0.1);
break;
case 2:
mapImg = loadImage('Maps/Boss Map no Path v3.png');
currentLevelMusic = level3Music;
currentLevelMusic.setVolume(0.1);
break;
case 3:
// Tutorial Map
mapImg = loadImage('Maps/Space Map 1.png');
currentLevelMusic = level1Music;
currentLevelMusic.setVolume(0.1);
break;
default:
break;
}
return;
}
// Reset the Map variables
function switchMap() {
if(mapID == 3) {
mapID = 0;
totalCurrency = DEFAULT_CURRENCY;
totalHealth = DEFAULT_HEALTH;
beginGame = false;
gameOver = false;
levelComplete = false;
} else {
++mapID;
}
totalCurrency = DEFAULT_CURRENCY;
initNextWave = 10;
currentWave = 0;
waveAmount = LEVELS[mapID].LEVEL_DATA.length;
levelComplete = false;
currentLevelMusic.stop();
enemies = []; // Reset Enemies
towers = []; // resets towers
selectMap(mapID);
uiHandler.nextLevelButton.hide();
currentLevelMusic.loop();
redraw();
}
// Spawns the next wave.
function spawnNextWave() {
try {
if (currentWave < LEVELS[mapID].LEVEL_DATA.length) {
currentWave = currentWave + 1;
let newWave = spawnWave(LEVELS[mapID].LEVEL_DATA, LEVELS[mapID].PRIORITY_DATA, currentWave);
for (let i = 0; i < ENEMIES.length; ++i)
for (let j = 0; j < LEVELS[mapID].LEVEL_DATA[currentWave - 1][i].length; ++j)
nextWaveCheck.amount += LEVELS[mapID].LEVEL_DATA[currentWave - 1][i][j];
newWave.debugPrintWave();
newWave.spawn();
enemies = newWave.getEnemies();
}
}
catch (e) {
alert(e);
}
}
/** Spawn a Wave
* @param {array} waveData - how many of each enemy type to spawn where array index = enemy type id
* @param {array} PRIORITY_DATA - order to spawn enemy types in
* @param {number} currentLevel - the wave that the game is currently in. From 1 to waveAmount
*/
function spawnWave(waveData, PRIORITY_DATA, currentLevel) {
const currentWave = new Wave(waveData[currentLevel - 1], PRIORITY_DATA[currentLevel - 1], MAPS[mapID].middlePath, 4);
return currentWave;
}
// ---------------------------------------------------------------------
// EVENT LISTENERS
// ---------------------------------------------------------------------
// Assets
let mapImg;
let towerSprite;
let freezerTowerSprite;
let poisonerTowerSprite;
let currentLevelMusic;
let titleScreenMusic;
let level1Music;
let level2Music;
let level3Music;
let deathSound;
let basicEnemy;
let summonerEnemy;
let summoneeEnemy;
let tankEnemy;
let bossEnemy;
let stunEnmeny;
let healthSprite;
let coinSprite;
let enemyDeathSound_default;
let enemyDeathSound_squid;
let enemyDeathSound_summoner;
let enemyDeathSound_zombie;
let f_Andale;
let playTitleScreenMusic = false;
window.preload = function () {
// Loads the Level Music
level1Music = loadSound('./assets/potassium.mp3');
level2Music = loadSound('./assets/Project_Beta_Song2.mp3');
level3Music = loadSound('./assets/Project_Beta_Boat_Song.mp3');
deathSound = loadSound('./assets/gta-v-wasted-death-sound.mp3');
titleScreenMusic = loadSound('./assets/Galactic_Guardians_Title_Song.mp3');
// Enemy sounds
enemyDeathSound_default = loadSound('./assets/enemyDeathSound_default.mp3');
enemyDeathSound_squid = loadSound('./assets/enemyDeathSound_squid.mp3');
enemyDeathSound_summoner = loadSound('./assets/enemyDeathSound_summoner.mp3');
enemyDeathSound_zombie = loadSound('./assets/enemyDeathSound_zombie.mp3');
f_Andale = loadFont('./assets/Andale-Mono.ttf');
towerSprite = loadImage('./assets/RedMoonTower.png');
freezerTowerSprite = loadImage('./assets/FreezerTower.png');
poisonerTowerSprite = loadImage('./assets/PoisonTower.png');
selectMap(mapID); // Loads the Map
uiHandler.preloadAssets();
basicEnemy = loadImage('./assets/Basic_Enemy.gif');
summonerEnemy = loadImage('./assets/Summoner_Animated.gif');
summoneeEnemy = loadImage('./assets/Summonee_Animated.gif');
tankEnemy = loadImage('./assets/Tank_Enemy.gif');
bossEnemy = loadImage('./assets/Boss_Boat.gif');
healthSprite = loadImage('./assets/heart.png');
coinSprite = loadImage('./assets/coin.png');
}
window.mousePressed = function (event) {
if (gameMode == 1 && uiHandler.ignoreNextClick == false && !uiHandler.encyclopediaOpen) {
// Check if mouse is inside a tower
for (let t = 0; t < towers.length; t++) {
if (towers[t].mouseInside()) {
towers[t].selected = true;
if (towers[t].isStunned()) towers[t].reduceStun(stunCooldown);
// dragTower = towers.splice(t, 1)[0];
// dragTower.hover = true;
// towers.push(dragTower);
} else {
towers[t].selected = false;
}
}
//Ignore touch events, only handle left mouse button
// Check if mouse is inside canvas
if (((event.button === 0 /* && !dragTower*/) && !(mouseX < 0 || mouseX > WINDOW_WIDTH - 50 || mouseY < 0 || mouseY + 50 > WINDOW_HEIGHT))) {
try {
if (towerToPlace) {
// if (towers.length > towerLimit) {
// throw new Error("No more towers allowed!");
// }
if (MAPS[mapID].isColliding(mouseX, 30)) {
// throw new Error("Cannot place a tower on the path!");
return;
}
if (mouseX >= WINDOW_WIDTH - 15 && mouseY > 30 || mouseY < 70) {
// throw new Error("NO");
} else {
if (totalCurrency < towerToPlace.placeTowerCost) {
throw new Error("Not enough money!");
}
else {
towerToPlace.x = mouseX;
towerToPlace.y = mouseY;
towers.push(towerToPlace);
totalCurrency -= towerToPlace.placeTowerCost;
}
}
towerToPlace = null;
}
} catch (e) {
alert(e);
}
}
} else {
uiHandler.ignoreNextClick = false;
}
}
// window.mouseDragged = function () {
// // Move tower if it's being dragged
// if (dragTower != null) {
// dragTower.x = mouseX;
// dragTower.y = mouseY;
// }
// }
// window.mouseReleased = function () {
// // Stop dragging tower
// if (dragTower != null) {
// dragTower.x = mouseX;
// dragTower.y = mouseY;
// dragTower.hover = false;
// dragTower = null;
// }
// }
window.mouseMoved = function () {
// Change cursor if mouse is inside a tower
for (let t of towers) {
if (t.mouseInside()) {
t.hover = true;
// if(uiHandler.towerTool == 0) {
cursor('grab');
// }
// if (uiHandler.towerTool == 1 || uiHandler.towerTool == 2 || uiHandler.towerTool == 3) {
// cursor('crosshair');
// }
return;
}
}
for (let t of towers) {
t.hover = false;
}
cursor();
}
window.keyPressed = function (e) {
if (keyCode === ESCAPE) { // use escape to open/close settings
towerToPlace = null;
}
if (e.key === 'd') { // use d to open/close debug console
let gameData = {
gameMode: gameMode,
totalCurrency: totalCurrency,
totalHealth: totalHealth,
waveAmount: waveAmount,
currentWave: currentWave,
towers: towers,
enemies: enemies,
}
console.log(gameData);
uiHandler.showDebugConsole(JSON.stringify(gameData));
}
if (e.altKey) {
if (e.key == 'z') {
console.log("Adding 10 currency");
totalCurrency += 10;
}
if (e.key == 'x') {
console.log("Adding 100 currency");
totalCurrency += 100;
}
if (e.key == 'c') {
console.log("Adding 1000 currency");
totalCurrency += 1000;
}
}
if (e.ctrlKey) {
if (e.key == 'z') {
console.log("Adding 10 health ");
totalHealth += 10;
}
if (e.key == 'x') {
console.log("Adding 100 health");
totalHealth += 100;
}
if (e.key == 'c') {
console.log("Adding 1000 health");
totalHealth += 1000;
}
}
}
window.setup = function () {
console.log("Wave amount",LEVELS[mapID]);
game = createCanvas(WINDOW_WIDTH, WINDOW_HEIGHT);
uiHandler.initializeUI();
uiHandler.saveButton.mousePressed(function () {
saveGame();
});
uiHandler.tutorialNextButton.mousePressed(function() {
if(tutorialTask == 6) {
switchMap();
}
tutorialTask++;
uiHandler.tutorialNextButton.hide();
});
uiHandler.muteButton.mousePressed(function() {
if (playSound) {
currentLevelMusic.pause();
playSound = false;
uiHandler.muteButton.html('volume_off');
localStorage.setItem("mute", true);
} else {
currentLevelMusic.loop();
playSound = true;
uiHandler.muteButton.html('volume_up');
localStorage.setItem("mute", false);
}
});
uiHandler.startButton.mousePressed(function() {
selectMap(mapID);
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
beginGame = true;
mapID = 0;
});
uiHandler.settingsButton.mousePressed(function() {
uiHandler.toggleSettings();
})
uiHandler.gameExit.mousePressed(function() {
saveGame();
window.location.reload();
});
uiHandler.toggleCoinsIncrease.mousePressed(function () {
totalCurrency += 100;
});
uiHandler.toggleCoinsDecrease.mousePressed(function () {
totalCurrency -= 100;
});
uiHandler.toggleHealthIncrease.mousePressed(function () {
totalHealth += 5;
});
uiHandler.toggleHealthDecrease.mousePressed(function () {
totalHealth -= 5;
});
uiHandler.loadButton.mousePressed(function () {
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
beginGame = true;
currentWave = 0;
loadGame();
});
uiHandler.launchTutorialButton.mousePressed(function() {
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
mapID = 3;
selectMap(mapID);
beginGame = true;
});
uiHandler.level1Button.mousePressed(function() {
mapID = 0;
selectMap(mapID);
currentWave = 0;
waveAmount = LEVELS[mapID].LEVEL_DATA.length;
levelComplete = false;
currentLevelMusic.stop();
enemies = []; // Reset Enemies
towers = []; // resets towers
uiHandler.nextLevelButton.hide();
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
beginGame = true;
});
uiHandler.level2Button.mousePressed(function() {
mapID = 1;
selectMap(mapID);
currentWave = 0;
waveAmount = LEVELS[mapID].LEVEL_DATA.length;
levelComplete = false;
currentLevelMusic.stop();
enemies = []; // Reset Enemies
towers = []; // resets towers
uiHandler.nextLevelButton.hide();
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
beginGame = true;
});
uiHandler.level3Button.mousePressed(function() {
mapID = 2;
selectMap(mapID);
currentWave = 0;
waveAmount = LEVELS[mapID].LEVEL_DATA.length;
levelComplete = false;
currentLevelMusic.stop();
enemies = []; // Reset Enemies
towers = []; // resets towers
uiHandler.nextLevelButton.hide();
if (!playSound) {
currentLevelMusic.setVolume(0.1);
currentLevelMusic.loop();
playSound = true;
}
beginGame = true;
});
// uiHandler.nextWaveButton.mousePressed(function() {
// spawnNextWave();
// });
uiHandler.placeStandardButton.mousePressed(function (e) {
console.log(!towerToPlace);
if (!towerToPlace && totalCurrency >= towerCosts["standard"].placeTowerCost) {
e.stopPropagation();
towerToPlace = new Standard();
}
});
uiHandler.placeFreezerButton.mousePressed(function (e) {
if (!towerToPlace && totalCurrency >= towerCosts["freezer"].placeTowerCost) {
e.stopPropagation();
towerToPlace = new Freezer();
}
});
uiHandler.placePoisonerButton.mousePressed(function (e) {
if (!towerToPlace && totalCurrency >= towerCosts["poisoner"].placeTowerCost) {
e.stopPropagation();
towerToPlace = new Poisoner();
}
});
uiHandler.upgradeRangeButton.mousePressed(function () {
let selectedUpgradeTower = getSelectedTower();
switch (selectedUpgradeTower.constructor.name) {
case "Standard":
if (totalCurrency >= towerCosts["standard"].rangeCost) {
selectedUpgradeTower.upgradeRange();
totalCurrency -= towerCosts["standard"].rangeCost;
}
break;
case "Poisoner":
if (totalCurrency >= towerCosts["poisoner"].rangeCost) {
selectedUpgradeTower.upgradeRange();
totalCurrency -= towerCosts["poisoner"].rangeCost;
}
break;
case "Freezer":
if (totalCurrency >= towerCosts["freezer"].rangeCost) {
selectedUpgradeTower.upgradeRange();
totalCurrency -= towerCosts["freezer"].rangeCost;
}
break;
}
});
uiHandler.upgradeFireRateButton.mousePressed(function () {
let selectedUpgradeTower = getSelectedTower();
switch (selectedUpgradeTower.constructor.name) {
case "Standard":
if (totalCurrency >= towerCosts["standard"].fireRateCost) {
selectedUpgradeTower.upgradeFireRate();
totalCurrency -= towerCosts["standard"].fireRateCost;
}
break;
case "Poisoner":
if (totalCurrency >= towerCosts["poisoner"].fireRateCost) {
selectedUpgradeTower.upgradeFireRate();
totalCurrency -= towerCosts["poisoner"].fireRateCost;
}
break;
case "Freezer":
if (totalCurrency >= towerCosts["freezer"].fireRateCost) {
selectedUpgradeTower.upgradeFireRate();
totalCurrency -= towerCosts["freezer"].fireRateCost;
}
break;
}
});
uiHandler.upgradeFireSpeedButton.mousePressed(function () {
let selectedUpgradeTower = getSelectedTower();
switch (selectedUpgradeTower.constructor.name) {
case "Standard":
if (totalCurrency >= towerCosts["standard"].fireSpeedCost) {
selectedUpgradeTower.upgradeFireSpeed();
totalCurrency -= towerCosts["standard"].fireSpeedCost;
}
break;
case "Poisoner":
if (totalCurrency >= towerCosts["poisoner"].fireSpeedCost) {
selectedUpgradeTower.upgradeFireSpeed();
totalCurrency -= towerCosts["poisoner"].fireSpeedCost;
}
break;
case "Freezer":
if (totalCurrency >= towerCosts["freezer"].fireSpeedCost) {
selectedUpgradeTower.upgradeFireSpeed();
totalCurrency -= towerCosts["freezer"].fireSpeedCost;
}
break;
}
});
uiHandler.nextLevelButton.mousePressed(function () {
switchMap();
saveGame();
});
uiHandler.returnToMenuButton.mousePressed(function() {
location.reload();
});
//Poll for bullets every 100ms
setInterval(fireBullets, 100);
setInterval(dealDamage, 100);
}
// ---------------------------------------------------------------------
// GAME LOOP
// ---------------------------------------------------------------------
window.draw = function () {
fill(0);
currentLevelMusic.setVolume(uiHandler.audioSlider.value());
if (gameMode == 0) {
uiHandler.updateUIForGameMode(gameMode);
uiHandler.nextLevelButton.hide();
if (!playTitleScreenMusic) {
titleScreenMusic.loop();
playTitleScreenMusic = true;
}
if (!localStorage.getItem("saveState")) {
uiHandler.loadButton.hide();
}
// Switch to game mode
if (beginGame) {
gameMode = 1;
titleScreenMusic.pause();
if (localStorage.getItem("mute")) {
if (localStorage.getItem("mute") == "true") {
currentLevelMusic.pause();
playSound = false;
uiHandler.muteButton.html('volume_off');
}
}
}
}
if (gameMode == 1) {
uiHandler.updateUIForGameMode(gameMode);
uiHandler.updateToolbarState(totalCurrency, getSelectedTower(), towerCosts);
// TODO
// if (nextWaveCheck.amount < 1) uiHandler.nextWaveButton.show();
// else uiHandler.nextWaveButton.hide();
background(200);
image(mapImg, WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, WINDOW_WIDTH, WINDOW_HEIGHT);
// Displays Level Complete Text and button when all waves are done
uiHandler.nextLevelButton.hide();
if (!uiHandler.settingsOpen && !uiHandler.encyclopediaOpen) {
if (levelComplete) {
push();
textAlign(CENTER);
textSize(40)
fill('red');
var lev = mapID + 1
stroke(0);
strokeWeight(4);
if (lev == 3) {
text('Capt. DeLozier Defeated!', 600, 100);
}
else {
text('Level ' + lev + ' Complete', 600, 100);
}
pop();
if (lev < 3) {
saveGame();
uiHandler.nextLevelButton.show();
}
else {
// Return to main Menu Button
uiHandler.returnToMenuButton.show();
}
}
if (towerToPlace) {
push();
switch(towerToPlace.constructor.name) {
case "Standard":
if (MAPS[0].isColliding(mouseX, 30) || totalCurrency < 400) {
tint(255, 0, 0, 200);
} else {
tint(255, 200);
}
image(towerSprite, mouseX, mouseY, Tower.TOWER_SIZE, Tower.TOWER_SIZE);
break;
case "Freezer":
if (MAPS[0].isColliding(mouseX, 30) || totalCurrency < 50) {
tint(255, 0, 0, 200);
} else {
tint(255, 200);
}
image(freezerTowerSprite, mouseX, mouseY, Tower.TOWER_SIZE, Tower.TOWER_SIZE);
break;
case "Poisoner":
if (MAPS[0].isColliding(mouseX, 30) || totalCurrency < 10) {
tint(255, 0, 0, 200);
} else {
tint(255, 200);
}
image(poisonerTowerSprite, mouseX, mouseY, Tower.TOWER_SIZE, Tower.TOWER_SIZE);
break;
}
pop();
}
// Draw bullets first, so they appear behind towers
for (const i in bullets) {
if (bullets[i].isOutOfRange()) {
bullets.splice(i, 1);
} else {
bullets[i].draw();
}
}
// Draw towers
for (const t of towers) {
switch(t.type) {
case "standard":
t.draw(towerSprite);
break;
case "freezer":
t.draw(freezerTowerSprite);
break;
case "poisoner":
t.draw(poisonerTowerSprite);
break;
default:
t.draw(towerSprite);
break;
}
if (t.isStunned()) t.drawStunned();
}
// Handle waves automatically, not needed for tutorial
if (nextWaveCheck.amount < 1 && mapID != 3) {
if (currentWave == 0) {
push();
textSize(20);
fill('white');
stroke(0);
strokeWeight(4);
text("Game starts in: " + initNextWave, WINDOW_WIDTH - 200, WINDOW_HEIGHT - 50);
pop();
}
else if (currentWave < waveAmount) {
push();
textSize(20);
fill('white');
stroke(0);
strokeWeight(4);
text("Next wave in: " + initNextWave, WINDOW_WIDTH - 185, WINDOW_HEIGHT - 50);
pop();
}
else {
levelComplete = true;
}
if (frameCount % 60 == 0 && initNextWave > 0) initNextWave--;
if (initNextWave == 0) {
if (currentWave < waveAmount) spawnNextWave();
initNextWave = 5;
}
}
//console.log(initNextWave);
// uiHandler.nextWaveButton.show();
// else uiHandler.nextWaveButton.hide();
// draw currency holder
push();
image(coinSprite, 90, 35, 20, 20);
textSize(20);
fill('white');
stroke(0);
strokeWeight(4);
text(totalCurrency, 105, 40);
pop();
// draw current health
push();
image(healthSprite, 25, 35, 20, 20);
textSize(20);
fill('white');
stroke(0);
strokeWeight(4);
text(totalHealth, 40, 40);
pop();
if (totalHealth <= 0) {
gameMode = -1;
gameOver = true;
}
// draw Wave information
if(mapID != 3) {
push();
textSize(20);
fill('white');
stroke(0);
strokeWeight(4);
text('Wave: ' + currentWave + '/' + LEVELS[mapID].LEVEL_DATA.length, WINDOW_WIDTH - 120, WINDOW_HEIGHT - 25);
pop();
}
// inject the tutorial
if (mapID == 3) {
tutorialHandler();
} else {
uiHandler.tutorialContainer.hide();
}
// draw or remove enemies
// iterate backwards to prevent flickering
for (let i = enemies.length - 1; i >= 0; i--) {
enemies[i].checkStatus();
if(mapID == 3){
if(enemies[i].health > 1){ enemies[i].health = 1; }
}
if (enemies[i].hasReachedEnd()) {
totalHealth -= enemies[i].damage;
nextWaveCheck.amount -= 1;
// Implement game over screen if needed
enemies.splice(i, 1);
} else if (enemies[i].health <= 0) {
totalCurrency += enemies[i].currency;
// handle spawner type enemies
// Mark an enemy as dead if its health is below 0
if (enemies[i].isDead() == false) {
enemies[i].kill();
switch (enemies[i].appearance) {
case "standard":
if(playSound) { enemyDeathSound_zombie.play(); }
break;
case "spawner":
if(playSound) { enemyDeathSound_summoner.play(); }
break;
case "rapid":
if(playSound) { enemyDeathSound_squid.play(); }
break;
default:
if(playSound) { enemyDeathSound_default.play(); }
break;
}
}
if (enemies[i].spawn) {
enemies[i].spawn(enemies, nextWaveCheck);
}
nextWaveCheck.amount -= 1;
enemies.splice(i, 1);
} else {
switch (enemies[i].appearance) {
case "standard":
enemies[i].draw(basicEnemy);
break;
case "spawner":
enemies[i].draw(summonerEnemy);
break;
case "rapid":
enemies[i].draw(summoneeEnemy);
break;
case "tank":
enemies[i].draw(tankEnemy);
break;
case "boss":
enemies[i].draw(bossEnemy);
break;
default:
enemies[i].drawBasic();
break;
}
// handle stunner type enemies
if (enemies[i].stunTower) {
if (stunCooldown.amount < stunCooldown.trigger) stunCooldown.amount++;
else {
let n = towers.length;
let stunIndex = enemies[i].stunTower(n);
if (stunIndex != -1) {
while (stunIndex < n && towers[stunIndex].isStunned()) {
stunIndex++
}
if (stunIndex < n) towers[stunIndex].stun();
}
stunCooldown.amount = 0;
}
}
// handle spawner type enemies
if (enemies[i].spawn && !enemies[i].onCooldown) {
enemies[i].spawn(enemies);
}
}
}
// Draw bullets before towers
for (const i in bullets) {
if (bullets[i].isOutOfRange()) {
bullets.splice(i, 1);
continue;
}
if (bullets[i].hasHitTarget()) {
bullets[i].target.health -= bullets[i].damage;
if (bullets[i].freeze) {
if (bullets[i].target.unFreeze == -1) {
bullets[i].target.speed /= 2;
}
bullets[i].target.unFreeze = bullets[i].target.x + 300 * bullets[i].target.speed;
}
if (bullets[i].poison) {
bullets[i].target.unPoison = bullets[i].target.x + 300 * bullets[i].target.speed;
}
bullets.splice(i, 1);
} else {
bullets[i].draw();
}
}
}
}
if (gameMode == -1 && gameOver == true) {
uiHandler.updateUIForGameMode(gameMode);