diff options
Diffstat (limited to 'html')
-rw-r--r-- | html/tower/js/mechanics.js | 46 | ||||
-rw-r--r-- | html/tower/js/renderer.js | 14 |
2 files changed, 45 insertions, 15 deletions
diff --git a/html/tower/js/mechanics.js b/html/tower/js/mechanics.js index 2430ca0..d172025 100644 --- a/html/tower/js/mechanics.js +++ b/html/tower/js/mechanics.js @@ -38,17 +38,32 @@ function updateEnemies() { return false; } - // Projectile collision detection + // Projectile collision detection and damage application const hitByProjectile = gameState.projectiles.some(projectile => { + // Calculate current projectile position based on lifetime + const age = performance.now() - projectile.createdAt; + const progress = Math.min(age / projectile.lifetime, 1); + + const currentX = projectile.startPos.x + (projectile.targetPos.x - projectile.startPos.x) * progress; + const currentY = projectile.startPos.y + (projectile.targetPos.y - projectile.startPos.y) * progress; + const distance = Math.hypot( - enemy.position.x - projectile.startPos.x, - enemy.position.y - projectile.startPos.y + enemy.position.x - currentX, + enemy.position.y - currentY ); - return distance < 0.5; + + if (distance < 0.5) { + // Apply damage when projectile hits + enemy.currentHealth -= projectile.damage; + return true; + } + return false; }); - if (hitByProjectile) { + if (enemy.currentHealth <= 0) { gameState.awardEnemyDestroyed(); + // Create death particles + gameState.particles.push(...createDeathParticles(enemy, cellSize)); return false; } @@ -75,13 +90,21 @@ function updateEnemies() { // Remove projectiles that hit enemies gameState.projectiles = gameState.projectiles.filter(projectile => { + const age = performance.now() - projectile.createdAt; + if (age >= projectile.lifetime) return false; + + const progress = age / projectile.lifetime; + const currentX = projectile.startPos.x + (projectile.targetPos.x - projectile.startPos.x) * progress; + const currentY = projectile.startPos.y + (projectile.targetPos.y - projectile.startPos.y) * progress; + const hitEnemy = gameState.enemies.some(enemy => { const distance = Math.hypot( - enemy.position.x - projectile.startPos.x, - enemy.position.y - projectile.startPos.y + enemy.position.x - currentX, + enemy.position.y - currentY ); return distance < 0.5; }); + return !hitEnemy; }); } @@ -218,18 +241,19 @@ function handleTowerAttack(tower, target, projectiles, particles, timestamp, cel // Create projectile projectiles.push({ - startPos: tower.position, - targetPos: target.position, + startPos: { ...tower.position }, + targetPos: { ...target.position }, createdAt: timestamp, lifetime: 300, - towerType: tower.type + towerType: tower.type, + damage: tower.damage }); // Process special abilities if (tower.special === 'slow') { handleSlowEffect(target, tower, timestamp, particles, cellSize); } else if (tower.special === 'aoe') { - handleAOEEffect(target, tower, enemies, particles, cellSize); + handleAOEEffect(target, tower, gameState.enemies, particles, cellSize); } tower.lastAttackTime = timestamp; diff --git a/html/tower/js/renderer.js b/html/tower/js/renderer.js index f37b5f3..29dd35f 100644 --- a/html/tower/js/renderer.js +++ b/html/tower/js/renderer.js @@ -92,11 +92,10 @@ function renderEnemies(ctx, enemies) { const opacity = 0.3 + (healthPercent * 0.7); // Dynamic color based on enemy state - ctx.fillStyle = `${EnemyTypes[enemy.type].color}${Math.floor(opacity * 255).toString(16).padStart(2, '0')}`; - ctx.strokeStyle = 'rgba(0, 0, 0, 0.8)'; - ctx.lineWidth = 2; + const color = EnemyTypes[enemy.type].color; + const hexOpacity = Math.floor(opacity * 255).toString(16).padStart(2, '0'); - // Enemy body + // Draw enemy body with solid black border ctx.beginPath(); ctx.arc( (enemy.position.x + 0.5) * cellSize, @@ -105,7 +104,14 @@ function renderEnemies(ctx, enemies) { 0, Math.PI * 2 ); + + // Fill with dynamic opacity + ctx.fillStyle = `${color}${hexOpacity}`; ctx.fill(); + + // Add solid black border + ctx.strokeStyle = 'black'; + ctx.lineWidth = 2; ctx.stroke(); // Range indicator for special enemy types |