oma-tank/game/debris.lua
2026-04-18 11:35:06 +01:00

69 lines
2.1 KiB
Lua

-- Freefalling debris: 6 shrapnel pieces spawned when a tank is destroyed.
-- Random linear + angular velocity with gravity; auto-despawn when they fall below ground.
local Palette = require("rendering.palette")
local Projection = require("rendering.projection")
local Models = require("data.models")
local Debris = {}
local pieces = {}
local PIECES_PER_EXPLOSION = 6
local MAX_VELOCITY = 120 -- spread of initial shrapnel velocities
local MAX_ANG_VELOCITY = 10 -- rad/s on each axis
local GRAVITY = 90 -- world units/sec² downward
local function randomModel()
local pool = Models.debrisPool
return pool[math.random(1, #pool)]
end
function Debris.explode(x, y, z)
y = y or 0
for _ = 1, PIECES_PER_EXPLOSION do
table.insert(pieces, {
x = x, y = y, z = z,
vx = (math.random() * 2 - 1) * MAX_VELOCITY,
vy = math.random() * MAX_VELOCITY, -- up (positive in our world)
vz = (math.random() * 2 - 1) * MAX_VELOCITY,
rx = math.random() * math.pi * 2,
ry = math.random() * math.pi * 2,
rz = math.random() * math.pi * 2,
drx = (math.random() * 2 - 1) * MAX_ANG_VELOCITY,
dry = (math.random() * 2 - 1) * MAX_ANG_VELOCITY,
drz = (math.random() * 2 - 1) * MAX_ANG_VELOCITY,
model = randomModel(),
})
end
end
function Debris.update(dt)
for i = #pieces, 1, -1 do
local d = pieces[i]
d.x = d.x + d.vx * dt
d.y = d.y + d.vy * dt
d.z = d.z + d.vz * dt
d.rx = d.rx + d.drx * dt
d.ry = d.ry + d.dry * dt
d.rz = d.rz + d.drz * dt
d.vy = d.vy - GRAVITY * dt
if d.y < 0 then
table.remove(pieces, i)
end
end
end
function Debris.draw()
local p = Palette.get()
love.graphics.setColor(p.bright)
love.graphics.setLineWidth(1.5)
for _, d in ipairs(pieces) do
Projection.drawModelXYZ(d.model, d.x, d.y, d.z, d.rx, d.ry, d.rz)
end
end
function Debris.clear()
pieces = {}
end
return Debris