From cc73f371a56695b03983a831edcae35d4d275e22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 2 Jun 2023 18:04:16 +0300 Subject: [PATCH] Make enemies killable --- bundle/main.lua | 65 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 10 deletions(-) diff --git a/bundle/main.lua b/bundle/main.lua index 536d668..a4ea5a1 100644 --- a/bundle/main.lua +++ b/bundle/main.lua @@ -60,8 +60,16 @@ function love.load() setScreenDimensions(width, height) movePaddle(0) spawnCities() - spawnEnemy(0.5, 0.1) - spawnEnemy(0.1, 0.3) + spawnEnemy(0.1, 0.1, false) + spawnEnemy(0.2, 0.1, true) + spawnEnemy(0.3, 0.1, false) + spawnEnemy(0.4, 0.1, true) + spawnEnemy(0.5, 0.1, false) + spawnEnemy(0.6, 0.1, true) + spawnEnemy(0.7, 0.1, false) + spawnEnemy(0.8, 0.1, true) + spawnEnemy(0.9, 0.1, false) + spawnEnemy(0.1, 0.3, true) end function spawnCities() @@ -107,11 +115,13 @@ function spawnExplosion(x, y) }) end -function spawnEnemy(x, y) +function spawnEnemy(x, y, active) table.insert(enemies, { x = x, y = y, - until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot) + active = active, + until_shoot = math.random() * (enemy_max_shoot - enemy_min_shoot), + alive = true }) end @@ -164,6 +174,20 @@ function updateMissiles(dt) end end + if missile.reflected then + for _, enemy in ipairs(enemies) do + local dx = enemy.x - missile.x + local dy = enemy.y - missile.y + local distance = math.sqrt(dx * dx + dy * dy) + if distance < enemy_radius then + spawnExplosion(missile.x, missile.y) + -- Freeze the missile in-place + missile.dx = 0 + missile.dy = 0 + end + end + end + if collided then table.insert(missile.history, 1, { x = missile.x, @@ -205,6 +229,16 @@ function updateExplosions(dt) end end + -- Destroy enemies within range + for _, enemy in ipairs(enemies) do + local dx = enemy.x - explosion.x + local dy = enemy.y - explosion.y + local distance = math.sqrt(dx * dx + dy * dy) + if distance < explosion.radius + enemy_radius then + enemy.alive = false + end + end + explosion.remaining = explosion.remaining - dt if explosion.remaining < explosion_duration * 0.2 then @@ -228,12 +262,23 @@ function updateExplosions(dt) end function updateEnemies(dt) - for _, enemy in ipairs(enemies) do - enemy.until_shoot = enemy.until_shoot - dt - if enemy.until_shoot < 0 then - enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot) - local target = cities[math.random(1, #cities)] - spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2) + local i = 1 + while i <= #enemies do + local enemy = enemies[i] + + if enemy.active then + enemy.until_shoot = enemy.until_shoot - dt + if enemy.until_shoot < 0 then + enemy.until_shoot = enemy_min_shoot + math.random() * (enemy_max_shoot - enemy_min_shoot) + local target = cities[math.random(1, #cities)] + spawnMissile(enemy.x, enemy.y, target.x, target.y, 0.2) + end + end + + if not enemy.alive then + table.remove(enemies, i) + else + i = i + 1 end end end