2019-01-28 14:00:00 +01:00
|
|
|
-- assets/animator :: the animator object. The animator object handle what
|
|
|
|
-- frame a sprite should draw.
|
|
|
|
|
|
|
|
--[[
|
|
|
|
Copyright © 2019 Kazhnuz
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
this software and associated documentation files (the "Software"), to deal in
|
|
|
|
the Software without restriction, including without limitation the rights to
|
|
|
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
]]
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
local Animator = Object:extend()
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initilizing and configuring option
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
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
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- UPDATE FUNCTIONS
|
|
|
|
-- Update the animation of the animator
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Animator:update(dt)
|
|
|
|
if (self.currentAnimation == "") then
|
2019-07-26 16:46:43 +02:00
|
|
|
core.debug:warning("animator", "no current animation data")
|
2019-01-28 09:58:12 +01:00
|
|
|
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
|
2019-07-29 08:57:04 +02:00
|
|
|
if not (self.animationData.pauseAtEnd) then
|
|
|
|
self.frame = self.animationData.loop
|
|
|
|
end
|
2019-01-28 09:58:12 +01:00
|
|
|
else
|
|
|
|
self.frame = self.frame + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- ANIMATION HANDLING FUNCTIONS
|
|
|
|
-- Change the animation of the animator
|
2019-03-23 12:02:01 +01:00
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
function Animator:changeAnimation(name, restart)
|
2019-03-10 12:55:16 +01:00
|
|
|
-- Force restart if animation name is different
|
|
|
|
if (self.currentAnimation ~= name) then
|
|
|
|
restart = true
|
|
|
|
else
|
|
|
|
restart = restart or false
|
|
|
|
end
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
self.currentAnimation = name
|
2019-01-28 10:00:36 +01:00
|
|
|
self.animationData = self.sprite.data.animations[self.currentAnimation]
|
2019-01-28 09:58:12 +01:00
|
|
|
|
2019-03-10 12:55:16 +01:00
|
|
|
if (restart == true) then
|
2019-01-28 10:00:36 +01:00
|
|
|
self.frame = self.animationData.startAt
|
2019-01-28 09:58:12 +01:00
|
|
|
self.frameTimer = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:changeToDefaultAnimation(restart)
|
|
|
|
self:changeAnimation(self.sprite.data.metadata.defaultAnim, restart)
|
|
|
|
end
|
|
|
|
|
2019-07-26 16:46:43 +02:00
|
|
|
-- INFO FUNCTIONS
|
|
|
|
-- get information with these functions
|
|
|
|
|
|
|
|
function Animator:getCurrentAnimation()
|
|
|
|
return self.currentAnimation
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:getAnimationDuration(animation)
|
|
|
|
return (self.animationData.endAt - self.animationData.startAt) / self.animationData.speed
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:getFrame()
|
|
|
|
return self.frame
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:getRelativeFrame()
|
|
|
|
return self.frame - (self.animationData.startAt) + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:animationExist(name)
|
|
|
|
return (self.sprite.data.animations[self.currentAnimation] ~= nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animator:getDimensions()
|
|
|
|
return self.sprite:getDimensions()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- DRAW FUNCTIONS
|
|
|
|
-- Draw animations using these functions
|
|
|
|
|
|
|
|
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:drawMask(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
self.sprite:drawFrameMask(self.frame, x, y, r, sx, sy, ox, oy, kx, ky)
|
|
|
|
end
|
|
|
|
|
2019-01-28 09:58:12 +01:00
|
|
|
return Animator
|