about summary refs log tree commit diff stats
path: root/html/plains
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2024-12-18 16:08:15 -0500
committerelioat <elioat@tilde.institute>2024-12-18 16:08:15 -0500
commite8b1a10f45e25aef021b3b164582b609c0ed36a7 (patch)
tree9a31877cb48a83cbeaf5367a972e9b29e677c8c6 /html/plains
parent735c44959dc85d9e2ac185b5b988e993b672a7d4 (diff)
downloadtour-e8b1a10f45e25aef021b3b164582b609c0ed36a7.tar.gz
*
Diffstat (limited to 'html/plains')
-rw-r--r--html/plains/enemies.js31
-rw-r--r--html/plains/game.js24
2 files changed, 48 insertions, 7 deletions
diff --git a/html/plains/enemies.js b/html/plains/enemies.js
index aa8bbe7..77f5245 100644
--- a/html/plains/enemies.js
+++ b/html/plains/enemies.js
@@ -73,17 +73,35 @@ const handleEnemyDamage = (enemy, damage, knockbackForce = 0, angle = 0) => {
     const gridSize = CONFIG.display.grid.size;
     enemy.hp -= damage;
     
+    // Apply stun when hit
+    enemy.stunned = true;
+    enemy.stunEndTime = animationTime + 500; // 500ms stun duration
+    
+    // Apply knockback if there's a force
+    if (knockbackForce > 0) {
+        const knockbackDistance = gridSize * 0.5; // Half grid cell knockback
+        enemy.knockback = {
+            active: true,
+            startX: enemy.x,
+            startY: enemy.y,
+            targetX: enemy.x + Math.cos(angle) * knockbackDistance,
+            targetY: enemy.y + Math.sin(angle) * knockbackDistance,
+            startTime: animationTime,
+            duration: 300
+        };
+    }
+    
     if (damage > 0 && enemy.hp <= 0) {
         // Create death particles
         const numParticles = 15 + Math.floor(Math.random() * 10);
         for (let i = 0; i < numParticles; i++) {
-            const angle = (i / numParticles) * Math.PI * 2;
+            const particleAngle = (i / numParticles) * Math.PI * 2;
             const speed = 2 + Math.random() * 3;
             state.particles.push({
                 x: enemy.x,
                 y: enemy.y,
-                dx: Math.cos(angle) * speed,
-                dy: Math.sin(angle) * speed,
+                dx: Math.cos(particleAngle) * speed,
+                dy: Math.sin(particleAngle) * speed,
                 size: enemy.size * (0.1 + Math.random() * 0.2),
                 color: enemy.color,
                 lifetime: 1000,
@@ -92,12 +110,12 @@ const handleEnemyDamage = (enemy, damage, knockbackForce = 0, angle = 0) => {
         }
 
         // Generate diamonds when enemy is defeated
-        const diamondCount = Math.floor(Math.random() * 5); // 0 to 4 diamonds
+        const diamondCount = Math.floor(Math.random() * 5);
         for (let i = 0; i < diamondCount; i++) {
             state.diamonds.push({
                 x: enemy.x + (Math.random() - 0.5) * 20,
                 y: enemy.y + (Math.random() - 0.5) * 20,
-                size: 6, // Smaller size (was 10)
+                size: 6,
                 collected: false
             });
         }
@@ -430,5 +448,6 @@ window.enemySystem = {
     generateEnemies,
     updateEnemies,
     renderEnemies,
-    findNearestLostVillager  // Export this so it can be used elsewhere if needed
+    findNearestLostVillager,
+    handleEnemyDamage
 }; 
\ No newline at end of file
diff --git a/html/plains/game.js b/html/plains/game.js
index d25daaf..1f9b27e 100644
--- a/html/plains/game.js
+++ b/html/plains/game.js
@@ -777,13 +777,35 @@ const weaponSystems = {
         const newAngle = state.player.swordAngle + CONFIG.sword.swingSpeed;
         const swingComplete = newAngle > Math.atan2(state.player.direction.y, state.player.direction.x) + Math.PI / 2;
         
+        // Add sword collision detection
+        const swordTip = {
+            x: state.player.x + Math.cos(state.player.swordAngle) * CONFIG.sword.length,
+            y: state.player.y + Math.sin(state.player.swordAngle) * CONFIG.sword.length
+        };
+
+        // Check all enemies for sword collision
+        const updatedEnemies = state.enemies.map(enemy => {
+            if (!enemy.stunned) {
+                const dx = enemy.x - state.player.x;
+                const dy = enemy.y - state.player.y;
+                const distance = Math.sqrt(dx * dx + dy * dy);
+
+                if (distance < enemy.size + CONFIG.sword.length) {
+                    const knockbackAngle = Math.atan2(dy, dx);
+                    enemySystem.handleEnemyDamage(enemy, 1, 1, knockbackAngle);
+                }
+            }
+            return enemy;
+        });
+        
         return {
             ...state,
             player: {
                 ...state.player,
                 swordAngle: newAngle,
                 isSwinging: !swingComplete
-            }
+            },
+            enemies: updatedEnemies
         };
     }
 };