Add explosion effects

This commit is contained in:
Juhani Krekelä 2023-06-02 16:57:19 +03:00
parent 29d65a9cf2
commit 793d84e680
1 changed files with 38 additions and 1 deletions

View File

@ -13,6 +13,10 @@ local cities = {}
local city_width = 0.07
local city_height = 0.05
local explosions = {}
local explosion_radius = 0.05
local explosion_duration = 0.4
local window_width = nil
local window_height = nil
local viewport_x_offset = nil
@ -91,6 +95,14 @@ function spawnMissile(x, y, target_x, target_y, speed)
})
end
function spawnExplosion(x, y)
table.insert(explosions, {
x = x,
y = y,
remaining = explosion_duration,
})
end
function updateMissiles(dt)
for _, missile in ipairs(missiles) do
missile.x = missile.x + missile.dx * dt
@ -134,6 +146,7 @@ function updateMissiles(dt)
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
spawnExplosion(missile.x, missile.y)
-- Destroy city
city.alive = false
-- Destroy missile
@ -159,13 +172,26 @@ function updateMissiles(dt)
-- Went off the bottom of the screen or exploded, delete
table.remove(missiles, i)
else
i = i +1
i = i + 1
end
end
end
function updateExplosions(dt)
local i = 1
while i <= #explosions do
explosions[i].remaining = explosions[i].remaining - dt
if explosions[i].remaining < 0 then
table.remove(explosions, i)
else
i = i + 1
end
end
end
function love.update(dt)
updateMissiles(dt)
updateExplosions(dt)
end
function movePaddle(screen_x)
@ -248,9 +274,20 @@ function drawCities()
end
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)
love.graphics.circle('fill', x, y, radius)
end
end
function love.draw()
drawCities()
drawWalls()
drawMissiles()
drawPaddle()
drawExplosions()
end