Mothback/code/animation.lua

78 lines
1.4 KiB
Lua
Raw Permalink Normal View History

Animation = {}
2021-10-29 00:17:18 +00:00
function Animation:New(anim_data)
2022-02-26 02:56:53 +00:00
local o = {}
2022-02-26 02:56:53 +00:00
o.path = anim_data.path
o.frames = anim_data.frames
2022-02-26 02:56:53 +00:00
o.imgs = anim_data.imgs
o.subframe = 0
o.frame = 1
2022-02-26 02:56:53 +00:00
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()
2022-02-26 02:56:53 +00:00
if self.frames[self.frame] ~= 0 then
-- try to animate
self.subframe = self.subframe + current_dt
2021-10-29 00:17:18 +00:00
2022-02-26 02:56:53 +00:00
if self.subframe > self.frames[self.frame] then
self.subframe = self.subframe - self.frames[self.frame]
self.frame = self.frame + 1
end
2021-10-29 00:17:18 +00:00
2022-02-26 02:56:53 +00:00
-- cycle
if self.frame >= #self.frames+1 then
self.frame = self.frame - #self.frames
end
end
2021-10-29 00:17:18 +00:00
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