experiments-pico8/games/sprites.lua

49 lines
1.1 KiB
Lua
Raw Normal View History

2023-04-25 04:21:58 +00:00
function make_sprite(x, y, anim, speed)
s = {
x=x, y=y, anim=animations[anim], speed=speed,
2023-04-25 05:22:24 +00:00
vx=0, vy=0,
2023-04-25 04:21:58 +00:00
frame=0, last_update=t(),
flip_x=false, flip_y=false, loop=false,
2023-04-30 09:34:36 +00:00
is_done=false,
2023-04-25 04:21:58 +00:00
}
return s
end
2023-04-30 09:34:36 +00:00
function reset_sprite(s)
s.speed = 0
s.frame = 0
s.last_update = t()
s.is_done = false
end
2023-04-25 04:21:58 +00:00
function update_sprite(s)
local now = t()
if s.last_update == 0 then
s.last_update = now
return
end
2023-04-25 05:22:24 +00:00
s.x += s.vx
s.y += s.vy
2023-04-25 04:21:58 +00:00
if s.speed != 0 then
if now - s.last_update > s.speed then
s.frame += 1
s.last_update = now
2023-04-25 07:24:54 +00:00
if s.frame + 1 > #s.anim.frames then
2023-04-25 04:21:58 +00:00
if s.loop then
s.frame = 0
else
s.speed = 0
s.frame = #s.anim.frames
2023-04-30 09:34:36 +00:00
s.is_done = true
2023-04-25 04:21:58 +00:00
end
end
2023-04-25 07:24:54 +00:00
2023-04-25 04:21:58 +00:00
end
end
end
function draw_sprite(s)
2023-04-25 07:24:54 +00:00
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
2023-04-25 04:21:58 +00:00
end