Add missile trails

This commit is contained in:
Juhani Krekelä 2023-06-02 16:00:16 +03:00
parent 950097fbbe
commit 50f7b00f57
1 changed files with 43 additions and 2 deletions

View File

@ -7,6 +7,7 @@ local wall_thickness = 0.01
local missiles = {}
local missile_radius = 0.005
local missile_trail_length = 7
local window_width = nil
local window_height = nil
@ -57,28 +58,36 @@ function spawnMissile(x, y, dx, dy)
dx = dx,
dy = dy,
reflected = false,
history = {
{x = x, y = y}
}
})
end
function updateMissiles(dt)
for i, missile in ipairs(missiles) do
for _, missile in ipairs(missiles) do
missile.x = missile.x + missile.dx * dt
missile.y = missile.y + missile.dy * dt
local collided = false
if missile.y < wall_thickness + missile_radius then
-- Collision with top wall
missile.y = wall_thickness + missile_radius
missile.dy = -missile.dy
collided = true
end
if missile.x < wall_thickness + missile_radius then
-- Collision with left wall
missile.x = wall_thickness + missile_radius
missile.dx = -missile.dx
collided = true
elseif missile.x > 1 - (wall_thickness + missile_radius) then
-- Collision with right wall
missile.x = 1 - (wall_thickness + missile_radius)
missile.dx = -missile.dx
collided = true
end
local paddle_left = paddle_x - paddle_width/2
@ -89,6 +98,18 @@ function updateMissiles(dt)
missile.y = paddle_top
missile.dy = -missile.dy
missile.reflected = true
collided = true
end
if collided then
table.insert(missile.history, 1, {
x = missile.x,
y = missile.y
})
-- Remove the oldest segments
if #missile.history > missile_trail_length then
table.remove(missile.history)
end
end
end
@ -132,12 +153,32 @@ function drawWalls()
end
function drawMissiles()
love.graphics.setLineWidth(0.001 * scale)
-- Trails
for _, missile in ipairs(missiles) do
local from_x, from_y = toScreenCoordinates(missile.x, missile.y)
local visibility = 1
for _, point in ipairs(missile.history) do
local x, y = toScreenCoordinates(point.x, point.y)
if missile.reflected then
love.graphics.setColor(1 * visibility, 1 * visibility, 0.5 * visibility)
else
love.graphics.setColor(1 * visibility, 0.4 * visibility, 0 * visibility)
end
visibility = visibility * 0.8
love.graphics.line(from_x, from_y, x, y)
from_x = x
from_y = y
end
end
-- Missiles themselves. Drawn separately so that they're always over the trails
for _, missile in ipairs(missiles) do
local style = 'fill'
if missile.reflected then
love.graphics.setColor(1, 0.5, 0)
style = 'line'
love.graphics.setLineWidth(0.001 * scale)
else
love.graphics.setColor(1, 0.5, 0.5)
end