63 lines
1.5 KiB
Lua
63 lines
1.5 KiB
Lua
local Animator = Object:extend()
|
|
|
|
function Animator:new(sprite)
|
|
self.sprite = sprite
|
|
self.frame = 1
|
|
self.frameTimer = 0
|
|
self.currentAnimation = ""
|
|
self.animationData = {}
|
|
|
|
self.customSpeed = 0
|
|
|
|
self:changeToDefaultAnimation()
|
|
end
|
|
|
|
function Animator:setCustomSpeed(customSpeed)
|
|
self.customSpeed = customSpeed or 0
|
|
end
|
|
|
|
function Animator:update(dt)
|
|
if (self.currentAnimation == "") then
|
|
print("warning: no current animation data")
|
|
return 0
|
|
end
|
|
|
|
local speed = self.animationData.speed
|
|
if (self.animationData.speed) == -1 then
|
|
speed = self.customSpeed --math.abs(self.xsp / 16)
|
|
end
|
|
self.frameTimer = self.frameTimer + (speed * dt)
|
|
if self.frameTimer > 1 then
|
|
self.frameTimer = 0
|
|
if self.frame == self.animationData.endAt then
|
|
self.frame = self.animationData.loop
|
|
else
|
|
self.frame = self.frame + 1
|
|
end
|
|
end
|
|
end
|
|
|
|
function Animator:getFrame()
|
|
return self.frame
|
|
end
|
|
|
|
function Animator:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
self.sprite:drawFrame(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
end
|
|
|
|
function Animator:changeAnimation(name, restart)
|
|
self.currentAnimation = name
|
|
self.animationData = self.sprite.data.animations[self.currentAnimation]
|
|
local restart = restart or true
|
|
|
|
if (restart) then
|
|
self.frame = self.animationData.startAt
|
|
self.frameTimer = 0
|
|
end
|
|
end
|
|
|
|
function Animator:changeToDefaultAnimation(restart)
|
|
self:changeAnimation(self.sprite.data.metadata.defaultAnim, restart)
|
|
end
|
|
|
|
return Animator
|