diff options
author | elioat <elioat@tilde.institute> | 2024-12-18 15:58:25 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-12-18 15:58:25 -0500 |
commit | 735c44959dc85d9e2ac185b5b988e993b672a7d4 (patch) | |
tree | d066c7122453fa412a32a308a307ddbc1eb1885d /html/plains/enemies.js | |
parent | ee38675b78dc7f6a49af2a7004822565086df476 (diff) | |
download | tour-735c44959dc85d9e2ac185b5b988e993b672a7d4.tar.gz |
*
Diffstat (limited to 'html/plains/enemies.js')
-rw-r--r-- | html/plains/enemies.js | 95 |
1 files changed, 38 insertions, 57 deletions
diff --git a/html/plains/enemies.js b/html/plains/enemies.js index ad457b2..aa8bbe7 100644 --- a/html/plains/enemies.js +++ b/html/plains/enemies.js @@ -106,65 +106,33 @@ const handleEnemyDamage = (enemy, damage, knockbackForce = 0, angle = 0) => { return damage > 0 && enemy.hp <= 0; }; -const updateEnemies = (enemies, deltaTime) => { - const gridSize = CONFIG.display.grid.size; - const aggroRange = gridSize * CONFIG.enemies.chase.range; - - // Check for weapon collisions - enemies.forEach(enemy => { - // Check bubble collisions - state.player.bubbles.forEach((bubble, bubbleIndex) => { - const dx = enemy.x - bubble.x; - const dy = enemy.y - bubble.y; - const distance = Math.sqrt(dx * dx + dy * dy); - - if (distance < enemy.size + CONFIG.bubble.size) { - // Remove the bubble - state.player.bubbles.splice(bubbleIndex, 1); - - // Normalize the direction vector - const length = Math.sqrt(dx * dx + dy * dy); - const dirX = dx / length; - const dirY = dy / length; - - // Set up knockback animation - enemy.knockback = { - active: true, - startX: enemy.x, - startY: enemy.y, - targetX: enemy.x + dirX * CONFIG.display.grid.size, - targetY: enemy.y + dirY * CONFIG.display.grid.size, - startTime: animationTime, - duration: 300 - }; - - // Stun the enemy - enemy.stunned = true; - enemy.stunEndTime = animationTime + 500; // Stun for 500ms - } - }); - - // Check sword collision - if (state.player.isSwinging && state.player.equipment === 'sword') { - 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 - }; - - const dx = enemy.x - swordTip.x; - const dy = enemy.y - swordTip.y; +// Add this new function to find nearest lost villager +const findNearestLostVillager = (enemyX, enemyY, villagers) => { + let nearestVillager = null; + let shortestDistance = Infinity; + + villagers.forEach(villager => { + if (villager.status === 'lost') { + const dx = villager.x - enemyX; + const dy = villager.y - enemyY; const distance = Math.sqrt(dx * dx + dy * dy); - if (distance < enemy.size + 10) { // 10px sword hit tolerance - handleEnemyDamage(enemy, 1); - enemy.stunned = true; - enemy.stunEndTime = animationTime + 300; // Stun for 300ms + if (distance < shortestDistance) { + shortestDistance = distance; + nearestVillager = villager; } } }); + + return nearestVillager; +}; + +const updateEnemies = (enemies, deltaTime) => { + const gridSize = CONFIG.display.grid.size; + const aggroRange = gridSize * CONFIG.enemies.chase.range; return enemies.filter(enemy => enemy.hp > 0).map(enemy => { - const baseSpeed = CONFIG.enemies.patrol.speed.base * deltaTime / 1000; // Convert to pixels per frame + const baseSpeed = CONFIG.enemies.patrol.speed.base * deltaTime / 1000; // Handle knockback animation if (enemy.knockback.active) { @@ -234,11 +202,23 @@ const updateEnemies = (enemies, deltaTime) => { enemy.stunned = false; } + // Check if current target villager was rescued if (enemy.targetVillager.status === 'rescued') { - return { - ...enemy, - color: CONFIG.enemies.colors.defeated - }; + // Find new target villager + const newTarget = findNearestLostVillager(enemy.x, enemy.y, state.villagers); + + if (newTarget) { + // Update enemy with new target + enemy.targetVillager = newTarget; + enemy.isReturning = true; // Make enemy return to new villager + enemy.isChasing = false; + } else { + // No more villagers to guard, enemy becomes defeated + return { + ...enemy, + color: CONFIG.enemies.colors.defeated + }; + } } // Calculate distance to player @@ -449,5 +429,6 @@ const generateDiamonds = () => { window.enemySystem = { generateEnemies, updateEnemies, - renderEnemies + renderEnemies, + findNearestLostVillager // Export this so it can be used elsewhere if needed }; \ No newline at end of file |