diff options
Diffstat (limited to 'html/tower/js/mechanics.js')
-rw-r--r-- | html/tower/js/mechanics.js | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js new file mode 100644 index 0000000..72b284a --- /dev/null +++ b/html/tower/js/mechanics.js @@ -0,0 +1,94 @@ +// 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 updateParticles(particles, timestamp, deltaTime) { + return particles.filter(particle => { + const age = timestamp - particle.createdAt; + if (age > particle.lifetime) return false; + + particle.position.x += particle.velocity.x * deltaTime; + particle.position.y += particle.velocity.y * deltaTime; + return true; + }); +} + +function createDeathParticles(target, cellSize) { + const particles = []; + const centerX = (target.position.x + 0.5) * cellSize; + const centerY = (target.position.y + 0.5) * cellSize; + + const particleCount = 8 + Math.floor(Math.random() * 8); + for (let i = 0; i < particleCount; i++) { + const baseAngle = (Math.PI * 2 * i) / particleCount; + const randomAngle = baseAngle + (Math.random() - 0.5) * 1.5; + const speedMultiplier = 0.7 + Math.random() * 0.6; + const startOffset = Math.random() * 5; + const startX = centerX + Math.cos(randomAngle) * startOffset; + const startY = centerY + Math.sin(randomAngle) * startOffset; + + particles.push(createParticle( + { + ...ParticleTypes.DEATH_PARTICLE, + speed: ParticleTypes.DEATH_PARTICLE.speed * speedMultiplier, + lifetime: ParticleTypes.DEATH_PARTICLE.lifetime * (0.8 + Math.random() * 0.4) + }, + { x: startX, y: startY }, + randomAngle + )); + } + return particles; +} + +function processTowerAttacks(towers, enemies, projectiles, particles, timestamp, cellSize) { + towers.forEach(tower => { + if (timestamp - tower.lastAttackTime > 1000 / tower.attackSpeed) { + const enemiesInRange = findEnemiesInRange(tower, enemies); + + if (enemiesInRange.length > 0) { + const target = enemiesInRange[0]; + handleTowerAttack(tower, target, projectiles, particles, timestamp, cellSize); + } + } + }); +} + +function findEnemiesInRange(tower, enemies) { + return enemies.filter(enemy => { + const dx = enemy.position.x - tower.position.x; + const dy = enemy.position.y - tower.position.y; + return Math.sqrt(dx * dx + dy * dy) <= tower.range; + }); +} + +function handleTowerAttack(tower, target, projectiles, particles, timestamp, cellSize) { + projectiles.push({ + startPos: tower.position, + targetPos: target.position, + createdAt: timestamp, + lifetime: 300 + }); + + target.currentHealth -= tower.damage; + tower.lastAttackTime = timestamp; + + if (target.currentHealth <= 0) { + particles.push(...createDeathParticles(target, cellSize)); + } +} \ No newline at end of file |