From 38ecbee541053edde6f9161038f2abe0be55cbf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 2 Jun 2023 17:21:52 +0300 Subject: [PATCH] Have explosions destroy things --- bundle/main.lua | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/bundle/main.lua b/bundle/main.lua index fbe2216..99333b6 100644 --- a/bundle/main.lua +++ b/bundle/main.lua @@ -98,6 +98,7 @@ function spawnExplosion(x, y) table.insert(explosions, { x = x, y = y, + radius = 0, remaining = explosion_duration, }) end @@ -145,10 +146,9 @@ function updateMissiles(dt) local distance = math.sqrt(dx * dx + dy * dy) if city.alive and distance < city_radius then spawnExplosion(missile.x, missile.y) - -- Destroy city - city.alive = false - -- Destroy missile - missile.alive = false + -- Freeze the missile in-place + missile.dx = 0 + missile.dy = 0 end end @@ -178,8 +178,36 @@ end function updateExplosions(dt) local i = 1 while i <= #explosions do - explosions[i].remaining = explosions[i].remaining - dt - if explosions[i].remaining < 0 then + local explosion = explosions[i] + + local completeness = (explosion_duration - explosion.remaining) / explosion_duration + explosion.radius = completeness ^ 1.5 * explosion_radius + + -- Destroy missiles within range + for _, missile in ipairs(missiles) do + local dx = missile.x - explosion.x + local dy = missile.x - explosion.x + local distance = math.sqrt(dx * dx + dy * dy) + if distance < explosion.radius then + missile.alive = false + end + end + + explosion.remaining = explosion.remaining - dt + + if explosion.remaining < explosion_duration * 0.2 then + -- Destroy cities within range + for _, city in ipairs(cities) do + local dx = city.x - explosion.x + local dy = city.x - explosion.x + local distance = math.sqrt(dx * dx + dy * dy) + if distance < explosion.radius + city_radius then + city.alive = false + end + end + end + + if explosion.remaining < 0 then table.remove(explosions, i) else i = i + 1 @@ -273,10 +301,9 @@ end function drawExplosions() for _, explosion in ipairs(explosions) do - local completeness = (explosion_duration - explosion.remaining) / explosion_duration love.graphics.setColor(1, 0, 0) local x, y = toScreenCoordinates(explosion.x, explosion.y) - local radius = toScreenSize(completeness ^ 1.5 * explosion_radius) + local radius = toScreenSize(explosion.radius) love.graphics.circle('fill', x, y, radius) end end