diff options
author | elioat <elioat@tilde.institute> | 2025-02-16 16:36:44 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-02-16 16:36:44 -0500 |
commit | b86049a413b7a63e04adeb6d2afd80801313b019 (patch) | |
tree | 6fc8fc49895c470b733dd6af49128c92f2baf6e8 /html/tower/js/mechanics.js | |
parent | 651ba65917968af90b97a77d02e28648c808b672 (diff) | |
download | tour-b86049a413b7a63e04adeb6d2afd80801313b019.tar.gz |
*
Diffstat (limited to 'html/tower/js/mechanics.js')
-rw-r--r-- | html/tower/js/mechanics.js | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js index 72b284a..4babe2a 100644 --- a/html/tower/js/mechanics.js +++ b/html/tower/js/mechanics.js @@ -91,4 +91,53 @@ function handleTowerAttack(tower, target, projectiles, particles, timestamp, cel if (target.currentHealth <= 0) { particles.push(...createDeathParticles(target, cellSize)); } +} + +function processEnemyAttacks(enemies, towers, particles, timestamp, cellSize) { + enemies.forEach(enemy => { + if (!EnemyTypes[enemy.type].isRanged) return; + + if (timestamp - enemy.lastAttackTime > 1000 / EnemyTypes[enemy.type].attackSpeed) { + const towersInRange = findTowersInRange(enemy, towers); + + if (towersInRange.length > 0) { + const target = towersInRange[0]; + handleEnemyAttack(enemy, target, particles, timestamp, cellSize); + } + } + }); +} + +function findTowersInRange(enemy, towers) { + return towers.filter(tower => { + const dx = tower.position.x - enemy.position.x; + const dy = tower.position.y - enemy.position.y; + return Math.sqrt(dx * dx + dy * dy) <= EnemyTypes[enemy.type].attackRange; + }); +} + +function handleEnemyAttack(enemy, tower, particles, timestamp, cellSize) { + // Create enemy projectile + const projectileColor = '#8e44ad80'; // Semi-transparent purple + particles.push(createParticle( + { + ...ParticleTypes.PROJECTILE, + color: projectileColor, + lifetime: 500 + }, + { + x: (enemy.position.x + 0.5) * cellSize, + y: (enemy.position.y + 0.5) * cellSize + }, + Math.atan2( + tower.position.y - enemy.position.y, + tower.position.x - enemy.position.x + ) + )); + + tower.currentHealth -= enemy.damage; + enemy.lastAttackTime = timestamp; + + // Reduce tower's damage as it takes damage + tower.damage = TowerTypes[tower.type].damage * (tower.currentHealth / tower.maxHealth); } \ No newline at end of file |