2021-10-29 02:15:53 +00:00
|
|
|
Animation = {}
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2021-10-29 02:15:53 +00:00
|
|
|
function Animation:New(anim_data)
|
|
|
|
local o = {}
|
2022-01-19 23:53:59 +00:00
|
|
|
|
|
|
|
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
|
2021-10-29 02:15:53 +00:00
|
|
|
|
|
|
|
setmetatable(o, self)
|
|
|
|
self.__index = self
|
|
|
|
return o
|
|
|
|
end
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2021-10-29 02:15:53 +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
|
|
|
|
|
2021-10-29 02:15:53 +00:00
|
|
|
-- 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(
|
2021-10-29 02:15:53 +00:00
|
|
|
self.imgs[frame],
|
2022-01-17 23:14:54 +00:00
|
|
|
math.floor(x - Camera.pos.x),
|
|
|
|
math.floor(y - Camera.pos.y),
|
2021-10-29 00:17:18 +00:00
|
|
|
rotate,
|
|
|
|
sx,
|
|
|
|
sy
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-10-29 02:15:53 +00:00
|
|
|
-- to linearly animate
|
|
|
|
function Animation:Animate()
|
2022-01-19 23:53:59 +00:00
|
|
|
if self.speed ~= 0 then
|
|
|
|
-- try to animate
|
|
|
|
self.subframe = self.subframe + current_dt
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2022-01-19 23:53:59 +00:00
|
|
|
if self.subframe > self.speed then
|
|
|
|
self.frame = self.frame + 1
|
|
|
|
self.subframe = self.subframe - self.speed
|
|
|
|
end
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2022-01-19 23:53:59 +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
|
|
|
|
|
2021-10-29 02:15:53 +00:00
|
|
|
-- 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],
|
2022-01-17 23:14:54 +00:00
|
|
|
math.floor(x),
|
|
|
|
math.floor(y),
|
2021-10-29 02:15:53 +00:00
|
|
|
rotate,
|
|
|
|
sx,
|
|
|
|
sy
|
|
|
|
)
|
2021-10-29 00:17:18 +00:00
|
|
|
end
|