Mothback/data/scripts/animation.lua

76 lines
1.3 KiB
Lua
Raw Normal View History

Animation = {}
2021-10-29 00:17:18 +00:00
function Animation:New(anim_data)
local o = {}
o.path = anim_data.path
o.frames = anim_data.frames
o.speed = anim_data.speed
o.imgs = anim_data.imgs
o.subframe = 0
o.frame = 1
setmetatable(o, self)
self.__index = self
return o
end
2021-10-29 00:17:18 +00:00
function Animation:ChangeTo(anim_data)
if anim_data.path == self.path
then
return self
else
return Animation:New(anim_data)
end
2021-10-29 00:17:18 +00:00
end
-- to manually handle what frame
function Animation:DrawFrame(frame, x, y, rotate, sx, sy)
if frame > self.frames then
frame = self.frames
end
2021-10-29 00:17:18 +00:00
local x = x or 0
local y = y or 0
local sx = sx or 1
local sy = sy or 1
love.graphics.draw(
self.imgs[frame],
math.floor(x - Camera.pos.x),
math.floor(y - Camera.pos.y),
2021-10-29 00:17:18 +00:00
rotate,
sx,
sy
)
end
-- to linearly animate
function Animation:Animate()
2021-10-29 00:17:18 +00:00
-- try to animate
self.subframe = self.subframe + current_dt
2021-10-29 00:17:18 +00:00
if self.subframe > self.speed then
self.frame = self.frame + 1
self.subframe = self.subframe - self.speed
2021-10-29 00:17:18 +00:00
end
-- cycle
if self.frame >= self.frames+1 then
self.frame = self.frame - self.frames
2021-10-29 00:17:18 +00:00
end
end
-- to draw the current frame
function Animation:Draw(x, y, rotate, sx, sy)
local x = x or 0
local y = y or 0
local sx = sx or 1
local sy = sy or 1
love.graphics.draw(
self.imgs[self.frame],
math.floor(x),
math.floor(y),
rotate,
sx,
sy
)
2021-10-29 00:17:18 +00:00
end