experiments-pico8/games/sprites.lua
2023-04-30 04:34:36 -05:00

49 lines
1.1 KiB
Lua

function make_sprite(x, y, anim, speed)
s = {
x=x, y=y, anim=animations[anim], speed=speed,
vx=0, vy=0,
frame=0, last_update=t(),
flip_x=false, flip_y=false, loop=false,
is_done=false,
}
return s
end
function reset_sprite(s)
s.speed = 0
s.frame = 0
s.last_update = t()
s.is_done = false
end
function update_sprite(s)
local now = t()
if s.last_update == 0 then
s.last_update = now
return
end
s.x += s.vx
s.y += s.vy
if s.speed != 0 then
if now - s.last_update > s.speed then
s.frame += 1
s.last_update = now
if s.frame + 1 > #s.anim.frames then
if s.loop then
s.frame = 0
else
s.speed = 0
s.frame = #s.anim.frames
s.is_done = true
end
end
end
end
end
function draw_sprite(s)
if s.anim then
spr(s.anim.frames[s.frame], s.x, s.y, s.anim.w, s.anim.h, s.flip_x, s.flip_y)
end
end