diff --git a/bundle/main.lua b/bundle/main.lua index 82b4950..6b79bde 100644 --- a/bundle/main.lua +++ b/bundle/main.lua @@ -80,7 +80,8 @@ function spawnMissile(x, y, dx, dy) reflected = false, history = { {x = x, y = y} - } + }, + alive = true }) end @@ -121,6 +122,19 @@ function updateMissiles(dt) collided = true end + for _, city in ipairs(cities) do + local city_left = city.x - city_width/2 + local city_right = city.x + city_width/2 + local city_top = city.y - city_height/2 + local city_bottom = city.y + city_height/2 + if city.alive and city_left <= missile.x and missile.x <= city_right and city_top <= missile.y and missile.y <= city_bottom then + -- Destroy city + city.alive = false + -- Destroy missile + missile.alive = false + end + end + if collided then table.insert(missile.history, 1, { x = missile.x, @@ -135,8 +149,8 @@ function updateMissiles(dt) local i = 1 while i <= #missiles do - if missiles[i].y > 1 + missile_radius then - -- Went off the bottom of the screen, delete + if missiles[i].y > 1 + missile_radius or not missiles[i].alive then + -- Went off the bottom of the screen or exploded, delete table.remove(missiles, i) else i = i +1