diff options
author | elioat <elioat@tilde.institute> | 2025-02-16 21:08:31 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-02-16 21:08:31 -0500 |
commit | 3aeffc98035ee4762a36ffa1657f084817d4dc49 (patch) | |
tree | fbd2b8229d6ff7b02e25133de0a560ee9c0e092f /html/tower/js/mechanics.js | |
parent | 44274c741392a48ec3d360df18643b060314d4ac (diff) | |
download | tour-3aeffc98035ee4762a36ffa1657f084817d4dc49.tar.gz |
*
Diffstat (limited to 'html/tower/js/mechanics.js')
-rw-r--r-- | html/tower/js/mechanics.js | 113 |
1 files changed, 47 insertions, 66 deletions
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 |