diff options
Diffstat (limited to 'html/tower/js/mechanics.js')
-rw-r--r-- | html/tower/js/mechanics.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js index 4babe2a..5d3eacb 100644 --- a/html/tower/js/mechanics.js +++ b/html/tower/js/mechanics.js @@ -140,4 +140,55 @@ 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 |