-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
1565 lines (1347 loc) · 60.6 KB
/
test.html
File metadata and controls
1565 lines (1347 loc) · 60.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced Tower Defense</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Arial', sans-serif;
}
body {
background: #0c0c16;
overflow: hidden;
color: #fff;
}
#game-container {
position: relative;
width: 100vw;
height: 100vh;
background: radial-gradient(ellipse at center, #1a1a3a 0%, #0a0a1a 100%);
overflow: hidden;
}
#game-canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1;
}
#particles-canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
opacity: 0.7;
}
#hud {
position: absolute;
top: 10px;
left: 10px;
padding: 15px;
background: rgba(0, 0, 0, 0.7);
border: 2px solid #4a4a8a;
border-radius: 10px;
box-shadow: 0 0 15px rgba(80, 80, 255, 0.7);
z-index: 3;
backdrop-filter: blur(5px);
}
#tower-menu {
position: absolute;
right: 10px;
top: 10px;
padding: 15px;
background: rgba(0, 0, 0, 0.7);
border: 2px solid #4a4a8a;
border-radius: 10px;
display: flex;
flex-direction: column;
gap: 10px;
box-shadow: 0 0 15px rgba(80, 80, 255, 0.7);
z-index: 3;
backdrop-filter: blur(5px);
}
.tower-btn {
padding: 10px;
background: linear-gradient(to bottom, #564aa0, #2c2457);
border: none;
color: white;
border-radius: 5px;
cursor: pointer;
position: relative;
overflow: hidden;
transition: all 0.3s;
box-shadow: 0 0 10px rgba(86, 74, 160, 0.5);
text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}
.tower-btn:hover {
transform: translateY(-2px);
box-shadow: 0 0 20px rgba(86, 74, 160, 0.8);
}
.tower-btn:after {
content: '';
position: absolute;
top: -50%;
left: -60%;
width: 200%;
height: 200%;
background: rgba(255, 255, 255, 0.1);
transform: rotate(30deg);
transition: all 0.3s;
}
.tower-btn:hover:after {
left: 100%;
}
#start-wave-btn {
margin-top: 20px;
padding: 10px;
background: linear-gradient(to bottom, #ac4a4a, #571e1e);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
box-shadow: 0 0 10px rgba(172, 74, 74, 0.5);
text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}
#start-wave-btn:hover {
transform: translateY(-2px);
box-shadow: 0 0 20px rgba(172, 74, 74, 0.8);
}
.upgrade-btn {
padding: 8px;
background: linear-gradient(to bottom, #4a8aac, #1e4557);
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 5px;
transition: all 0.3s;
box-shadow: 0 0 10px rgba(74, 138, 172, 0.5);
text-shadow: 0 0 5px rgba(255, 255, 255, 0.8);
}
.upgrade-btn:hover {
transform: translateY(-2px);
box-shadow: 0 0 20px rgba(74, 138, 172, 0.8);
}
#modal-overlay {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
z-index: 10;
backdrop-filter: blur(5px);
}
#game-over-modal, #level-complete-modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(20, 20, 40, 0.9);
padding: 30px;
border-radius: 15px;
text-align: center;
z-index: 11;
min-width: 300px;
box-shadow: 0 0 30px rgba(100, 100, 255, 0.8);
border: 3px solid #4a4a8a;
}
.modal-title {
font-size: 28px;
margin-bottom: 20px;
color: #fff;
text-shadow: 0 0 10px rgba(100, 100, 255, 0.8);
}
.modal-btn {
padding: 12px 20px;
margin: 10px;
background: linear-gradient(to bottom, #564aa0, #2c2457);
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: all 0.3s;
box-shadow: 0 0 15px rgba(86, 74, 160, 0.6);
}
.modal-btn:hover {
transform: scale(1.05);
box-shadow: 0 0 25px rgba(86, 74, 160, 0.9);
}
.tower-preview {
position: absolute;
pointer-events: none;
z-index: 2;
opacity: 0.7;
}
.tower-range-indicator {
position: absolute;
border-radius: 50%;
background: rgba(100, 100, 255, 0.2);
border: 2px solid rgba(100, 100, 255, 0.5);
pointer-events: none;
z-index: 2;
}
#tutorial-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.8);
z-index: 10;
display: flex;
justify-content: center;
align-items: center;
}
#tutorial-box {
background: rgba(20, 20, 40, 0.9);
padding: 30px;
border-radius: 15px;
max-width: 600px;
text-align: center;
box-shadow: 0 0 30px rgba(100, 100, 255, 0.8);
border: 3px solid #4a4a8a;
}
#tutorial-title {
font-size: 24px;
margin-bottom: 20px;
color: #fff;
text-shadow: 0 0 10px rgba(100, 100, 255, 0.8);
}
#tutorial-content {
margin-bottom: 20px;
line-height: 1.6;
}
#tutorial-btn {
padding: 12px 20px;
background: linear-gradient(to bottom, #564aa0, #2c2457);
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: all 0.3s;
}
#tutorial-btn:hover {
transform: scale(1.05);
box-shadow: 0 0 25px rgba(86, 74, 160, 0.9);
}
.wave-announcement {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
font-size: 48px;
text-shadow: 0 0 20px rgba(255, 100, 100, 0.8);
opacity: 0;
transition: opacity 0.5s;
z-index: 5;
pointer-events: none;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.1); opacity: 0.7; }
100% { transform: scale(1); opacity: 1; }
}
.pulsating {
animation: pulse 2s infinite;
}
.tower-tooltip {
position: absolute;
background: rgba(0, 0, 0, 0.8);
padding: 10px;
border-radius: 5px;
border: 1px solid #4a4a8a;
color: white;
font-size: 14px;
pointer-events: none;
z-index: 5;
max-width: 200px;
backdrop-filter: blur(5px);
box-shadow: 0 0 10px rgba(80, 80, 255, 0.5);
display: none;
}
</style>
</head>
<body>
<div id="game-container">
<canvas id="particles-canvas"></canvas>
<canvas id="game-canvas"></canvas>
<div id="hud">
<h2 style="margin-bottom: 10px; text-shadow: 0 0 5px rgba(100, 100, 255, 0.8);">NEBULA DEFENSE</h2>
<p>Money: $<span id="money">150</span></p>
<p>Lives: <span id="lives">10</span></p>
<p>Wave: <span id="wave">0</span></p>
<p>Score: <span id="score">0</span></p>
</div>
<div id="tower-menu">
<h3 style="margin-bottom: 5px; text-align: center; text-shadow: 0 0 5px rgba(100, 100, 255, 0.8);">TOWERS</h3>
<button class="tower-btn" id="basic-tower-btn" data-cost="50" data-type="basic">Basic Tower ($50)</button>
<button class="tower-btn" id="sniper-tower-btn" data-cost="100" data-type="sniper">Sniper Tower ($100)</button>
<button class="tower-btn" id="splash-tower-btn" data-cost="150" data-type="splash">Splash Tower ($150)</button>
<button id="start-wave-btn">Start Wave</button>
</div>
<div class="wave-announcement" id="wave-announcement"></div>
<div class="tower-tooltip" id="tower-tooltip"></div>
</div>
<div id="modal-overlay"></div>
<div id="game-over-modal">
<h2 class="modal-title">Game Over!</h2>
<p>Your base has been destroyed.</p>
<p style="margin: 15px 0;">Final Score: <span id="final-score">0</span></p>
<button class="modal-btn" id="restart-btn">Play Again</button>
</div>
<div id="level-complete-modal">
<h2 class="modal-title">Victory!</h2>
<p>You've successfully defended your base!</p>
<p style="margin: 15px 0;">Final Score: <span id="victory-score">0</span></p>
<button class="modal-btn" id="next-level-btn">Play Again</button>
</div>
<div id="tutorial-overlay">
<div id="tutorial-box">
<h2 id="tutorial-title">Welcome to Nebula Defense!</h2>
<div id="tutorial-content">
<p>Defend your space station against waves of alien invaders!</p>
<p>Build towers by selecting a tower type and placing it on the map. Different towers have different abilities:</p>
<p>• <strong>Basic Tower</strong>: Balanced attack and range</p>
<p>• <strong>Sniper Tower</strong>: Long range, high damage, slow fire rate</p>
<p>• <strong>Splash Tower</strong>: Area damage, affects multiple enemies</p>
<p>Enemies follow the path to your base. If too many reach it, you lose!</p>
<p>Defeat enemies to earn money for more towers. Good luck, Commander!</p>
</div>
<button id="tutorial-btn">Start Game</button>
</div>
</div>
<script>
// Game constants and variables
const GRID_SIZE = 40;
const ENEMY_TYPES = {
NORMAL: { color: '#f55', health: 100, speed: 1, reward: 10, size: 15 },
FAST: { color: '#5ff', health: 60, speed: 2, reward: 15, size: 12 },
TANK: { color: '#55f', health: 300, speed: 0.5, reward: 25, size: 20 }
};
const TOWER_TYPES = {
basic: {
color: '#5f5',
range: 120,
damage: 20,
fireRate: 1,
cost: 50,
size: 20,
upgrades: [
{ level: 2, cost: 40, damage: 30, fireRate: 1.2, range: 130 },
{ level: 3, cost: 80, damage: 45, fireRate: 1.5, range: 140 }
],
projectileColor: '#5f5',
projectileSize: 5,
description: "Balanced tower with moderate damage and range."
},
sniper: {
color: '#ff5',
range: 200,
damage: 80,
fireRate: 0.5,
cost: 100,
size: 25,
upgrades: [
{ level: 2, cost: 80, damage: 120, fireRate: 0.6, range: 220 },
{ level: 3, cost: 160, damage: 200, fireRate: 0.7, range: 250 }
],
projectileColor: '#ff5',
projectileSize: 4,
description: "High damage and long range, but slow firing rate."
},
splash: {
color: '#f5f',
range: 100,
damage: 15,
fireRate: 0.8,
cost: 150,
size: 22,
splashRadius: 40,
upgrades: [
{ level: 2, cost: 120, damage: 25, fireRate: 0.9, range: 110, splashRadius: 50 },
{ level: 3, cost: 200, damage: 40, fireRate: 1.0, range: 120, splashRadius: 60 }
],
projectileColor: '#f5f',
projectileSize: 6,
description: "Deals area damage to multiple enemies."
}
};
// Game state
let game = {
money: 150,
lives: 10,
wave: 0,
score: 0,
gameOver: false,
victory: false,
waveInProgress: false,
selectedTower: null,
towers: [],
enemies: [],
projectiles: [],
particles: [],
explosions: [],
path: [],
grid: [],
canvas: null,
ctx: null,
particlesCanvas: null,
particlesCtx: null,
lastFrameTime: 0,
selectedPlacedTower: null,
mapWidth: 0,
mapHeight: 0
};
// Tutorial handling
document.getElementById('tutorial-btn').addEventListener('click', function() {
document.getElementById('tutorial-overlay').style.display = 'none';
initializeGame();
});
// Initialize game when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Game canvas setup
game.canvas = document.getElementById('game-canvas');
game.ctx = game.canvas.getContext('2d');
// Particles canvas setup
game.particlesCanvas = document.getElementById('particles-canvas');
game.particlesCtx = game.particlesCanvas.getContext('2d');
// Resize canvas to fill window
resizeCanvas();
window.addEventListener('resize', resizeCanvas);
// Game will start after tutorial is closed
});
function resizeCanvas() {
game.canvas.width = window.innerWidth;
game.canvas.height = window.innerHeight;
game.particlesCanvas.width = window.innerWidth;
game.particlesCanvas.height = window.innerHeight;
game.mapWidth = Math.floor(game.canvas.width / GRID_SIZE);
game.mapHeight = Math.floor(game.canvas.height / GRID_SIZE);
// If game is already initialized, regenerate the grid and path
if (game.grid.length > 0) {
generateMap();
gameLoop(0);
}
}
function initializeGame() {
// Reset game state
game.money = 150;
game.lives = 10;
game.wave = 0;
game.score = 0;
game.gameOver = false;
game.victory = false;
game.waveInProgress = false;
game.selectedTower = null;
game.towers = [];
game.enemies = [];
game.projectiles = [];
game.particles = [];
game.explosions = [];
// Generate the map
generateMap();
// Set up event listeners
setupEventListeners();
// Start background particles
createBackgroundParticles(100);
// Start the game loop
requestAnimationFrame(gameLoop);
// Update HUD
updateHUD();
}
function generateMap() {
// Initialize grid
game.grid = Array(game.mapHeight).fill().map(() => Array(game.mapWidth).fill(0));
// Create path
let startRow = Math.floor(game.mapHeight / 2);
let startCol = 0;
let endCol = game.mapWidth - 1;
game.path = [];
let currentRow = startRow;
let currentCol = startCol;
// Add starting point
game.path.push({ row: currentRow, col: currentCol });
// Generate a random path to the end
while (currentCol < endCol) {
let direction = Math.random();
let nextRow = currentRow;
let nextCol = currentCol + 1;
// 30% chance to move up or down (if possible)
if (direction < 0.3 && currentRow > 1) {
nextRow--;
} else if (direction < 0.6 && currentRow < game.mapHeight - 2) {
nextRow++;
}
// Ensure the path doesn't double back or cross itself
const pathCellExists = game.path.some(point =>
point.row === nextRow && point.col === nextCol
);
if (!pathCellExists) {
currentRow = nextRow;
currentCol = nextCol;
game.path.push({ row: currentRow, col: currentCol });
}
}
// Mark path cells on the grid
game.path.forEach(point => {
game.grid[point.row][point.col] = 1; // 1 indicates a path cell
});
}
function createBackgroundParticles(count) {
for (let i = 0; i < count; i++) {
game.particles.push({
x: Math.random() * game.canvas.width,
y: Math.random() * game.canvas.height,
size: Math.random() * 2 + 1,
speedX: (Math.random() - 0.5) * 0.5,
speedY: (Math.random() - 0.5) * 0.5,
color: `rgba(${100 + Math.random() * 155}, ${100 + Math.random() * 155}, ${200 + Math.random() * 55}, ${0.3 + Math.random() * 0.5})`
});
}
}
function setupEventListeners() {
// Tower selection buttons
document.querySelectorAll('.tower-btn').forEach(button => {
button.addEventListener('click', function() {
const towerType = this.getAttribute('data-type');
const towerCost = parseInt(this.getAttribute('data-cost'));
if (game.money >= towerCost) {
game.selectedTower = towerType;
document.body.style.cursor = 'pointer';
} else {
showNotification("Not enough money!");
}
});
// Tooltip for tower buttons
button.addEventListener('mouseenter', function() {
const towerType = this.getAttribute('data-type');
const tooltip = document.getElementById('tower-tooltip');
const tower = TOWER_TYPES[towerType];
tooltip.innerHTML = `<strong>${capitalizeFirstLetter(towerType)} Tower</strong><br>
${tower.description}<br>
Damage: ${tower.damage}<br>
Range: ${tower.range}<br>
Fire Rate: ${tower.fireRate}/s`;
tooltip.style.display = 'block';
tooltip.style.left = `${this.getBoundingClientRect().left - 210}px`;
tooltip.style.top = `${this.getBoundingClientRect().top}px`;
});
button.addEventListener('mouseleave', function() {
document.getElementById('tower-tooltip').style.display = 'none';
});
});
// Start wave button
document.getElementById('start-wave-btn').addEventListener('click', function() {
if (!game.waveInProgress) {
startWave();
}
});
// Canvas click event for tower placement
game.canvas.addEventListener('click', function(event) {
// Get the clicked grid cell
const x = event.clientX;
const y = event.clientY;
const col = Math.floor(x / GRID_SIZE);
const row = Math.floor(y / GRID_SIZE);
// If a tower is selected and the clicked cell is valid for tower placement
if (game.selectedTower && isValidTowerPlacement(row, col)) {
const towerType = game.selectedTower;
const tower = TOWER_TYPES[towerType];
// Check if player has enough money
if (game.money >= tower.cost) {
// Place the tower
game.towers.push({
type: towerType,
row: row,
col: col,
x: col * GRID_SIZE + GRID_SIZE / 2,
y: row * GRID_SIZE + GRID_SIZE / 2,
range: tower.range,
damage: tower.damage,
fireRate: tower.fireRate,
lastFireTime: 0,
level: 1,
target: null,
size: tower.size,
splashRadius: tower.splashRadius || 0,
projectileColor: tower.projectileColor,
projectileSize: tower.projectileSize
});
// Mark the grid cell as occupied
game.grid[row][col] = 2; // 2 indicates a tower
// Deduct the cost
game.money -= tower.cost;
updateHUD();
// Reset cursor and selected tower
document.body.style.cursor = 'default';
game.selectedTower = null;
removeAllTowerIndicators();
} else {
showNotification("Not enough money!");
}
}
// Check if player clicked on an existing tower
else if (!game.selectedTower) {
game.selectedPlacedTower = null;
removeAllTowerIndicators();
for (let i = 0; i < game.towers.length; i++) {
const tower = game.towers[i];
const distance = Math.sqrt(
Math.pow(x - tower.x, 2) +
Math.pow(y - tower.y, 2)
);
if (distance <= tower.size) {
game.selectedPlacedTower = i;
showTowerUpgradeOptions(tower);
showTowerRangeIndicator(tower);
break;
}
}
}
});
// Canvas mousemove event for tower preview
game.canvas.addEventListener('mousemove', function(event) {
if (game.selectedTower) {
// Update tower preview position
const x = event.clientX;
const y = event.clientY;
const col = Math.floor(x / GRID_SIZE);
const row = Math.floor(y / GRID_SIZE);
// Show tower preview at mouse position
showTowerPreview(row, col);
}
});
// Canvas mouseout event to remove preview
game.canvas.addEventListener('mouseout', function() {
removeAllTowerIndicators();
});
// Reset button events
document.getElementById('restart-btn').addEventListener('click', function() {
hideModals();
initializeGame();
});
document.getElementById('next-level-btn').addEventListener('click', function() {
hideModals();
initializeGame();
});
// Escape key to cancel tower selection
document.addEventListener('keydown', function(event) {
if (event.key === 'Escape') {
game.selectedTower = null;
document.body.style.cursor = 'default';
removeAllTowerIndicators();
}
});
}
function showTowerPreview(row, col) {
removeAllTowerIndicators();
if (row >= 0 && row < game.mapHeight && col >= 0 && col < game.mapWidth) {
const isValid = isValidTowerPlacement(row, col);
const x = col * GRID_SIZE + GRID_SIZE / 2;
const y = row * GRID_SIZE + GRID_SIZE / 2;
// Create tower preview element
const preview = document.createElement('div');
preview.className = 'tower-preview';
preview.style.width = `${TOWER_TYPES[game.selectedTower].size * 2}px`;
preview.style.height = `${TOWER_TYPES[game.selectedTower].size * 2}px`;
preview.style.left = `${x - TOWER_TYPES[game.selectedTower].size}px`;
preview.style.top = `${y - TOWER_TYPES[game.selectedTower].size}px`;
preview.style.borderRadius = '50%';
preview.style.backgroundColor = isValid ? TOWER_TYPES[game.selectedTower].color : 'rgba(255, 0, 0, 0.5)';
document.getElementById('game-container').appendChild(preview);
// Create range indicator
const rangeIndicator = document.createElement('div');
rangeIndicator.className = 'tower-range-indicator';
rangeIndicator.style.width = `${TOWER_TYPES[game.selectedTower].range * 2}px`;
rangeIndicator.style.height = `${TOWER_TYPES[game.selectedTower].range * 2}px`;
rangeIndicator.style.left = `${x - TOWER_TYPES[game.selectedTower].range}px`;
rangeIndicator.style.top = `${y - TOWER_TYPES[game.selectedTower].range}px`;
document.getElementById('game-container').appendChild(rangeIndicator);
}
}
function showTowerRangeIndicator(tower) {
const rangeIndicator = document.createElement('div');
rangeIndicator.className = 'tower-range-indicator';
rangeIndicator.style.width = `${tower.range * 2}px`;
rangeIndicator.style.height = `${tower.range * 2}px`;
rangeIndicator.style.left = `${tower.x - tower.range}px`;
rangeIndicator.style.top = `${tower.y - tower.range}px`;
document.getElementById('game-container').appendChild(rangeIndicator);
}
function showTowerUpgradeOptions(tower) {
// Remove any existing upgrade container
removeAllTowerIndicators();
// Create upgrade container
const upgradeContainer = document.createElement('div');
upgradeContainer.className = 'tower-tooltip';
upgradeContainer.id = 'tower-upgrade';
upgradeContainer.style.display = 'block';
upgradeContainer.style.left = `${tower.x + 30}px`;
upgradeContainer.style.top = `${tower.y - 30}px`;
// Tower info
const towerType = capitalizeFirstLetter(tower.type);
upgradeContainer.innerHTML = `<strong>${towerType} Tower (Level ${tower.level})</strong><br>
Damage: ${tower.damage}<br>
Range: ${tower.range}<br>
Fire Rate: ${tower.fireRate.toFixed(1)}/s<br>`;
// Check if tower can be upgraded further
const towerData = TOWER_TYPES[tower.type];
const upgradeInfo = towerData.upgrades.find(u => u.level === tower.level + 1);
if (upgradeInfo) {
const upgradeBtn = document.createElement('button');
upgradeBtn.className = 'upgrade-btn';
upgradeBtn.innerHTML = `Upgrade ($${upgradeInfo.cost})`;
upgradeBtn.addEventListener('click', function() {
upgradeTower(game.selectedPlacedTower, upgradeInfo);
});
upgradeContainer.appendChild(upgradeBtn);
} else {
upgradeContainer.innerHTML += '<br><strong>Max Level Reached</strong>';
}
document.getElementById('game-container').appendChild(upgradeContainer);
}
function upgradeTower(towerIndex, upgradeInfo) {
const tower = game.towers[towerIndex];
if (game.money >= upgradeInfo.cost) {
// Apply upgrade
tower.level = upgradeInfo.level;
tower.damage = upgradeInfo.damage;
tower.fireRate = upgradeInfo.fireRate;
tower.range = upgradeInfo.range;
if (upgradeInfo.splashRadius) {
tower.splashRadius = upgradeInfo.splashRadius;
}
// Effect for upgrade
createExplosion(tower.x, tower.y, 30, 'rgba(100, 255, 100, 0.7)', 1);
// Deduct cost
game.money -= upgradeInfo.cost;
updateHUD();
// Update the tower info display
removeAllTowerIndicators();
showTowerUpgradeOptions(tower);
showTowerRangeIndicator(tower);
} else {
showNotification("Not enough money for upgrade!");
}
}
function removeAllTowerIndicators() {
const previews = document.querySelectorAll('.tower-preview');
previews.forEach(preview => preview.remove());
const rangeIndicators = document.querySelectorAll('.tower-range-indicator');
rangeIndicators.forEach(indicator => indicator.remove());
const upgradeContainer = document.getElementById('tower-upgrade');
if (upgradeContainer) {
upgradeContainer.remove();
}
}
function isValidTowerPlacement(row, col) {
// Check if the coordinates are within the grid
if (row < 0 || row >= game.mapHeight || col < 0 || col >= game.mapWidth) {
return false;
}
// Check if the cell is not a path and not already occupied by a tower
return game.grid[row][col] === 0;
}
function startWave() {
game.wave++;
game.waveInProgress = true;
// Announce the wave
const announcement = document.getElementById('wave-announcement');
announcement.textContent = `WAVE ${game.wave}`;
announcement.style.opacity = '1';
setTimeout(() => {
announcement.style.opacity = '0';
}, 2000);
// Update HUD
updateHUD();
// Generate enemies based on the current wave
generateEnemies();
}
function generateEnemies() {
const waveData = {
enemyCount: 10 + game.wave * 5,
types: ['NORMAL'],
spawnInterval: 1000, // milliseconds
lastSpawnTime: 0,
spawnedCount: 0
};
// Add more enemy types as waves progress
if (game.wave >= 3) {
waveData.types.push('FAST');
}
if (game.wave >= 5) {
waveData.types.push('TANK');
}
// Spawn function to be called periodically
game.waveData = waveData;
}
function spawnEnemy() {
if (!game.waveData || game.waveData.spawnedCount >= game.waveData.enemyCount) {
return;
}
const now = Date.now();
if (now - game.waveData.lastSpawnTime > game.waveData.spawnInterval) {
// Select a random enemy type from available types
const typeIndex = Math.floor(Math.random() * game.waveData.types.length);
const enemyType = game.waveData.types[typeIndex];
const enemyData = ENEMY_TYPES[enemyType];
// Create the enemy
const startPoint = game.path[0];
const enemy = {
type: enemyType,
x: startPoint.col * GRID_SIZE + GRID_SIZE / 2,
y: startPoint.row * GRID_SIZE + GRID_SIZE / 2,
health: enemyData.health + (game.wave - 1) * 20, // Increase health with wave
maxHealth: enemyData.health + (game.wave - 1) * 20,
speed: enemyData.speed,
reward: enemyData.reward,
size: enemyData.size,
pathIndex: 0,
color: enemyData.color,
dead: false
};
game.enemies.push(enemy);
game.waveData.spawnedCount++;
game.waveData.lastSpawnTime = now;
// Add a small effect when enemy spawns
createParticleEffect(enemy.x, enemy.y, 10, enemy.color, 20);
}
}
function createParticleEffect(x, y, count, color, speed = 10) {
for (let i = 0; i < count; i++) {
const angle = Math.random() * Math.PI * 2;
const dist = Math.random() * speed;
game.particles.push({
x: x,
y: y,
size: Math.random() * 3 + 2,
speedX: Math.cos(angle) * dist,
speedY: Math.sin(angle) * dist,
color: color,
alpha: 1,
life: 20 + Math.random() * 20
});
}
}
function createExplosion(x, y, radius, color, duration) {
game.explosions.push({
x: x,
y: y,
radius: 0,
maxRadius: radius,
color: color,
duration: duration,
time: 0
});
}
function updateEnemies(deltaTime) {
for (let i = game.enemies.length - 1; i >= 0; i--) {
const enemy = game.enemies[i];
if (enemy.dead) {
// Remove dead enemies
game.enemies.splice(i, 1);
continue;
}
// Move enemy along the path
const targetPoint = game.path[enemy.pathIndex];
const targetX = targetPoint.col * GRID_SIZE + GRID_SIZE / 2;
const targetY = targetPoint.row * GRID_SIZE + GRID_SIZE / 2;
// Calculate direction to target
const dx = targetX - enemy.x;
const dy = targetY - enemy.y;
const distance = Math.sqrt(dx * dx + dy * dy);
// Move if not at target
if (distance > 5) {
enemy.x += (dx / distance) * enemy.speed * deltaTime;
enemy.y += (dy / distance) * enemy.speed * deltaTime;
} else {
// Reached current target, move to next path point
enemy.pathIndex++;