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 = {}
|
|
|
|
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
|
|
|
|
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()
|
2021-10-29 00:17:18 +00:00
|
|
|
-- try to animate
|
2021-10-29 02:15:53 +00:00
|
|
|
self.subframe = self.subframe + current_dt
|
2021-10-29 00:17:18 +00:00
|
|
|
|
2021-10-29 02:15:53 +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
|
|
|
|
|
2021-10-29 02:15:53 +00:00
|
|
|
-- cycle
|
|
|
|
if self.frame >= self.frames+1 then
|
|
|
|
self.frame = self.frame - self.frames
|
2021-10-29 00:17:18 +00:00
|
|
|
end
|
|
|
|
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
|