about summary refs log tree commit diff stats
path: root/html/tower/js/mechanics.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/tower/js/mechanics.js')
-rw-r--r--html/tower/js/mechanics.js49
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