bvg/systems/animation.lua

42 lines
1.1 KiB
Lua

local world = require("world")
local function getCurrentTimeMilliseconds()
return love.timer.getTime() * 1000
end
local Animation = {
animation = {
initialMs = T.number,
msPerFrame = T.number,
frames = Arr({ drawable = T.Drawable }),
},
position = T.XyPair,
drawAsSprite = T.Drawable
}
---@return { initialMs: number, frames: Entity[], msPerFrame: number }
function BuildAnimation(frames, msPerFrame)
return {
initialMs = getCurrentTimeMilliseconds(),
frames = frames,
msPerFrame = msPerFrame,
}
end
local currentMs = getCurrentTimeMilliseconds()
local animation = world:filteredSystem("animation", Animation, function(e, _, system)
local nextFrameIndex = 1 + math.floor((currentMs - e.animation.initialMs) / e.animation.msPerFrame)
if nextFrameIndex > #e.animation.frames then
e.animation = nil
system.world:addEntity(e)
else
e.drawAsSprite = e.animation.frames[nextFrameIndex]
end
end)
function animation:preProcess()
currentMs = getCurrentTimeMilliseconds()
end