about summary refs log tree commit diff stats
path: root/html/tower
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-02-16 21:08:31 -0500
committerelioat <elioat@tilde.institute>2025-02-16 21:08:31 -0500
commit3aeffc98035ee4762a36ffa1657f084817d4dc49 (patch)
treefbd2b8229d6ff7b02e25133de0a560ee9c0e092f /html/tower
parent44274c741392a48ec3d360df18643b060314d4ac (diff)
downloadtour-3aeffc98035ee4762a36ffa1657f084817d4dc49.tar.gz
*
Diffstat (limited to 'html/tower')
-rw-r--r--html/tower/js/game.js27
-rw-r--r--html/tower/js/gameState.js33
-rw-r--r--html/tower/js/mechanics.js113
-rw-r--r--html/tower/js/renderer.js6
-rw-r--r--html/tower/js/uiHandlers.js4
5 files changed, 85 insertions, 98 deletions
diff --git a/html/tower/js/game.js b/html/tower/js/game.js
index 7d362ae..57eaa3a 100644
--- a/html/tower/js/game.js
+++ b/html/tower/js/game.js
@@ -1,19 +1,6 @@
 const canvas = document.getElementById('gameCanvas');
 const ctx = canvas.getContext('2d');
 
-// Initialize game state
-let gameState = {
-    grid: Array(20).fill().map(() => Array(20).fill('empty')),
-    path: [],
-    towers: [],
-    enemies: [],
-    playerCurrency: 100,
-    phase: 'placement',
-    isGameOver: false,
-    particles: [],
-    projectiles: []
-};
-
 let lastTimestamp = 0;
 const ENEMY_SPAWN_INTERVAL = 1000; // 1 second
 let lastEnemySpawn = 0;
@@ -38,7 +25,7 @@ function gameLoop(timestamp) {
 
 function handleCombatPhase(timestamp, deltaTime) {
     spawnEnemies(timestamp);
-    updateEnemies(gameState.enemies, gameState.path, deltaTime);
+    updateEnemies();
     gameState.particles = updateParticles(gameState.particles, timestamp, deltaTime);
     gameState.projectiles = gameState.projectiles.filter(p => timestamp - p.createdAt < p.lifetime);
     
@@ -63,7 +50,13 @@ function handleCombatPhase(timestamp, deltaTime) {
     );
     
     // Remove dead enemies and destroyed towers
-    gameState.enemies = gameState.enemies.filter(enemy => enemy.currentHealth > 0);
+    gameState.enemies = gameState.enemies.filter(enemy => {
+        if (enemy.currentHealth <= 0) {
+            gameState.awardEnemyDestroyed();
+            return false;
+        }
+        return true;
+    });
     gameState.towers = gameState.towers.filter(tower => tower.currentHealth > 0);
 }
 
@@ -146,11 +139,11 @@ function initializeEventListeners() {
         const tower = TowerTypes[draggedTowerType];
         if (
             gameState.grid[hoverCell.y][hoverCell.x] === 'empty' &&
-            gameState.playerCurrency >= tower.cost
+            gameState.currency >= tower.cost
         ) {
             gameState.grid[hoverCell.y][hoverCell.x] = 'tower';
             gameState.towers.push(createTower(draggedTowerType, { ...hoverCell }));
-            gameState.playerCurrency -= tower.cost;
+            gameState.currency -= tower.cost;
         }
         
         draggedTowerType = null;
diff --git a/html/tower/js/gameState.js b/html/tower/js/gameState.js
index 0f379f6..63270fa 100644
--- a/html/tower/js/gameState.js
+++ b/html/tower/js/gameState.js
@@ -109,16 +109,25 @@ function createParticle(type, position, angle) {
     };
 }
 
-// Add to gameState object
-gameState.particles = [];
-gameState.projectiles = [];
-gameState.enemiesDestroyed = 0;
-gameState.enemiesEscaped = 0;
-gameState.path = []; // This will be populated when the path is generated
-
-gameState.awardEnemyDestroyed = function() {
-    this.enemiesDestroyed++;
-    // Random reward between 5 and 10
-    const reward = Math.floor(Math.random() * 6) + 5;
-    this.currency += reward;
+// Initialize game state at the bottom of the file
+const gameState = {
+    grid: Array(20).fill().map(() => Array(20).fill('empty')),
+    path: [],
+    towers: [],
+    enemies: [],
+    currency: 100,
+    phase: 'placement',
+    isGameOver: false,
+    particles: [],
+    projectiles: [],
+    enemiesDestroyed: 0,
+    enemiesEscaped: 0,
+    
+    // Define the function as part of the initial object
+    awardEnemyDestroyed() {
+        this.enemiesDestroyed++;
+        // Random reward between 1 and 3
+        const reward = Math.floor(Math.random() * 3) + 1;
+        this.currency += reward;
+    }
 }; 
\ No newline at end of file
diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js
index 5d3eacb..c9c4202 100644
--- a/html/tower/js/mechanics.js
+++ b/html/tower/js/mechanics.js
@@ -1,20 +1,52 @@
 // Combat mechanics
-function updateEnemies(enemies, path, deltaTime) {
-    enemies.forEach(enemy => {
-        if (enemy.pathIndex < path.length - 1) {
-            const targetPos = path[enemy.pathIndex + 1];
-            const dx = targetPos.x - enemy.position.x;
-            const dy = targetPos.y - enemy.position.y;
-            const distance = Math.sqrt(dx * dx + dy * dy);
-            
-            if (distance < enemy.speed * deltaTime / 1000) {
-                enemy.position = { ...targetPos };
-                enemy.pathIndex++;
-            } else {
-                enemy.position.x += (dx / distance) * enemy.speed * deltaTime / 1000;
-                enemy.position.y += (dy / distance) * enemy.speed * deltaTime / 1000;
-            }
+function updateEnemies() {
+    gameState.enemies = gameState.enemies.filter(enemy => {
+        // Add progress property if it doesn't exist
+        if (typeof enemy.progress === 'undefined') {
+            enemy.progress = 0;
+        }
+        
+        // Update progress
+        enemy.progress += enemy.speed * 0.001;
+        
+        // Check if enemy has completed the path
+        if (enemy.progress >= 1) {
+            gameState.enemiesEscaped++;
+            return false; // Remove from array
+        }
+        
+        // Check for collisions with projectiles
+        const hitByProjectile = gameState.projectiles.some(projectile => {
+            const distance = Math.hypot(
+                enemy.position.x - projectile.startPos.x,
+                enemy.position.y - projectile.startPos.y
+            );
+            return distance < 0.5;
+        });
+        
+        if (hitByProjectile) {
+            gameState.awardEnemyDestroyed();
+            return false; // Remove from array
         }
+        
+        // Update enemy position based on progress
+        const pathPosition = getPathPosition(enemy.progress, gameState.path);
+        enemy.position.x = pathPosition.x;
+        enemy.position.y = pathPosition.y;
+        
+        return true;
+    });
+    
+    // Remove projectiles that hit enemies
+    gameState.projectiles = gameState.projectiles.filter(projectile => {
+        const hitEnemy = gameState.enemies.some(enemy => {
+            const distance = Math.hypot(
+                enemy.position.x - projectile.startPos.x,
+                enemy.position.y - projectile.startPos.y
+            );
+            return distance < 0.5;
+        });
+        return !hitEnemy;
     });
 }
 
@@ -140,55 +172,4 @@ function handleEnemyAttack(enemy, tower, particles, timestamp, cellSize) {
     
     // Reduce tower's damage as it takes damage
     tower.damage = TowerTypes[tower.type].damage * (tower.currentHealth / tower.maxHealth);
-}
-
-function updateEnemies() {
-    gameState.enemies = gameState.enemies.filter(enemy => {
-        // Add progress property if it doesn't exist
-        if (typeof enemy.progress === 'undefined') {
-            enemy.progress = 0;
-        }
-        
-        // Reduce the multiplier from 0.01 to 0.001 for more reasonable speed
-        enemy.progress += enemy.speed * 0.001; // Smaller multiplier for slower movement
-        
-        // Check if enemy has completed the path
-        if (enemy.progress >= 1) {
-            gameState.enemiesEscaped++;
-            return false; // Remove from array
-        }
-        
-        // Check for collisions with projectiles
-        const hitByProjectile = gameState.projectiles.some(projectile => {
-            const distance = Math.hypot(
-                enemy.position.x - projectile.startPos.x,
-                enemy.position.y - projectile.startPos.y
-            );
-            return distance < 0.5; // Adjust collision radius as needed
-        });
-        
-        if (hitByProjectile) {
-            gameState.awardEnemyDestroyed();
-            return false; // Remove from array
-        }
-        
-        // Update enemy position based on progress
-        const pathPosition = getPathPosition(enemy.progress, gameState.path);
-        enemy.position.x = pathPosition.x;
-        enemy.position.y = pathPosition.y;
-        
-        return true;
-    });
-    
-    // Remove projectiles that hit enemies
-    gameState.projectiles = gameState.projectiles.filter(projectile => {
-        const hitEnemy = gameState.enemies.some(enemy => {
-            const distance = Math.hypot(
-                enemy.position.x - projectile.startPos.x,
-                enemy.position.y - projectile.startPos.y
-            );
-            return distance < 0.5;
-        });
-        return !hitEnemy;
-    });
 } 
\ No newline at end of file
diff --git a/html/tower/js/renderer.js b/html/tower/js/renderer.js
index b0d1da0..24a18ef 100644
--- a/html/tower/js/renderer.js
+++ b/html/tower/js/renderer.js
@@ -100,8 +100,12 @@ function renderEnemies(ctx, enemies) {
 function renderUI(ctx, gameState) {
     ctx.fillStyle = 'black';
     ctx.font = '20px Arial';
-    ctx.fillText(`Currency: ${gameState.playerCurrency}`, 10, 30);
+    ctx.fillText(`Currency: $${gameState.currency}`, 10, 30);
     ctx.fillText(`Phase: ${gameState.phase}`, 10, 60);
+    
+    // Add enemy stats
+    ctx.fillText(`Destroyed: ${gameState.enemiesDestroyed}`, 10, 90);
+    ctx.fillText(`Escaped: ${gameState.enemiesEscaped}`, 10, 120);
 }
 
 function renderTowers(ctx, towers) {
diff --git a/html/tower/js/uiHandlers.js b/html/tower/js/uiHandlers.js
index 5dc048a..6e0789e 100644
--- a/html/tower/js/uiHandlers.js
+++ b/html/tower/js/uiHandlers.js
@@ -39,10 +39,10 @@ function placeTower(gameState, towerType, position) {
     const tower = TowerTypes[towerType];
     if (
         gameState.grid[position.y][position.x] === 'empty' &&
-        gameState.playerCurrency >= tower.cost
+        gameState.currency >= tower.cost
     ) {
         gameState.grid[position.y][position.x] = 'tower';
         gameState.towers.push(createTower(towerType, { ...position }));
-        gameState.playerCurrency -= tower.cost;
+        gameState.currency -= tower.cost;
     }
 } 
\ No newline at end of file
pers from subx-common into layers' href='/akkartik/mu/commit/079emit.subx?h=main&id=fd91f7f61bfa84cbc24590d5394d75891cc1cfcc'>fd91f7f6 ^
71eb22a5 ^
fd91f7f6 ^




























7a583220 ^
fd91f7f6 ^




7a583220 ^
fd91f7f6 ^









686a52bd ^
fd91f7f6 ^
686a52bd ^
fd91f7f6 ^








71eb22a5 ^
fd91f7f6 ^



























7a583220 ^
fd91f7f6 ^




7a583220 ^
fd91f7f6 ^









686a52bd ^
fd91f7f6 ^
686a52bd ^
fd91f7f6 ^








71eb22a5 ^
fd91f7f6 ^



























7a583220 ^
fd91f7f6 ^




7a583220 ^
fd91f7f6 ^









686a52bd ^
fd91f7f6 ^
686a52bd ^
fd91f7f6 ^








71eb22a5 ^
fd91f7f6 ^





















































7a583220 ^
fd91f7f6 ^


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