chore: extract the sprite of the actor
This commit is contained in:
parent
b12108a260
commit
8b3a5f1f0c
2 changed files with 149 additions and 60 deletions
|
@ -26,6 +26,7 @@ local cwd = (...):gsub('%.baseactor$', '') .. "."
|
||||||
local BaseActor = Object:extend()
|
local BaseActor = Object:extend()
|
||||||
|
|
||||||
local Timer = require(cwd .. "utils.timer")
|
local Timer = require(cwd .. "utils.timer")
|
||||||
|
local Sprite = require(cwd .. "utils.sprites")
|
||||||
|
|
||||||
-- INIT FUNCTIONS
|
-- INIT FUNCTIONS
|
||||||
-- Initialise the actor and its base functions
|
-- Initialise the actor and its base functions
|
||||||
|
@ -364,28 +365,20 @@ end
|
||||||
-- Handle the sprite of the actor
|
-- Handle the sprite of the actor
|
||||||
|
|
||||||
function BaseActor:setSprite(spritename, ox, oy)
|
function BaseActor:setSprite(spritename, ox, oy)
|
||||||
self.sprite = {}
|
self.sprite = Sprite(self, spritename, ox, oy)
|
||||||
self.sprite.name = spritename or nil
|
|
||||||
self.sprite.ox = ox or 0
|
|
||||||
self.sprite.oy = oy or 0
|
|
||||||
self.sprite.sx = 1
|
|
||||||
self.sprite.sy = 1
|
|
||||||
self.sprite.exist = (spritename ~= nil)
|
|
||||||
self.sprite.clone = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:cloneSprite()
|
function BaseActor:cloneSprite()
|
||||||
if self.sprite.name ~= nil then
|
if self.sprite ~= nil then
|
||||||
self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
|
core.debug:warning("actor", "the function BaseActor:cloneSprite is deprecated, prefer BaseActor.sprite:clone()")
|
||||||
self.sprite.clone:setCallbackTarget(self)
|
self.sprite:clone()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:changeAnimation(animation, restart)
|
function BaseActor:changeAnimation(animation, restart)
|
||||||
if (self.sprite.clone == nil) then
|
if (self.sprite ~= nil) then
|
||||||
self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart)
|
core.debug:warning("actor", "the function BaseActor:changeAnimation is deprecated, prefer BaseActor.sprite:changeAnimation()")
|
||||||
else
|
self.sprite:changeAnimation(animation, restart)
|
||||||
self.sprite.clone:changeAnimation(animation, restart)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -394,85 +387,61 @@ function BaseActor:animationEnded(animation)
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:setCustomSpeed(customSpeed)
|
function BaseActor:setCustomSpeed(customSpeed)
|
||||||
if (self.sprite.clone == nil) then
|
if (self.sprite ~= nil) then
|
||||||
self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed)
|
core.debug:warning("actor", "the function BaseActor:setCustomSpeed is deprecated, prefer BaseActor.sprite:setCustomSpeed()")
|
||||||
else
|
self.sprite:setCustomSpeed(customSpeed)
|
||||||
self.sprite.clone:setCustomSpeed(customSpeed)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:updateSprite(dt)
|
function BaseActor:updateSprite(dt)
|
||||||
if (self.sprite.clone ~= nil) then
|
if (self.sprite ~= nil) then
|
||||||
self.sprite.clone:update(dt)
|
self.sprite:update(dt)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:setSpriteScallingX(sx)
|
function BaseActor:setSpriteScallingX(sx)
|
||||||
local sx = sx or 1
|
core.debug:warning("actor", "the function BaseActor:setSpriteScallingX is deprecated, prefer BaseActor.sprite:setSpriteScallingX()")
|
||||||
|
self.sprite:setScallingX(sx)
|
||||||
self.sprite.sx = sx
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:setSpriteScallingY(sy)
|
function BaseActor:setSpriteScallingY(sy)
|
||||||
local sy = sy or 1
|
core.debug:warning("actor", "the function BaseActor:setSpriteScallingY is deprecated, prefer BaseActor.sprite:setSpriteScallingY()")
|
||||||
|
self.sprite:setScallingY(sY)
|
||||||
self.sprite.sy = sy
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:getCurrentAnimation()
|
function BaseActor:getCurrentAnimation()
|
||||||
if (self.sprite.clone == nil) then
|
if (self.sprite ~= nil) then
|
||||||
return self.assets.sprites[self.sprite.name]:getCurrentAnimation()
|
core.debug:warning("actor", "the function BaseActor:getCurrentAnimation is deprecated, prefer BaseActor.sprite:getCurrentAnimation()")
|
||||||
else
|
return self.sprite:getCurrentAnimation()
|
||||||
return self.sprite.clone:getCurrentAnimation()
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function BaseActor:getSpriteScalling()
|
function BaseActor:getSpriteScalling()
|
||||||
return self.sprite.sx, self.sprite.sy
|
return self.sprite:getScalling()
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:getFrame()
|
function BaseActor:getFrame()
|
||||||
if (self.sprite.name ~= nil) then
|
if (self.sprite ~= nil) then
|
||||||
if (self.sprite.clone ~= nil) then
|
return self.sprite:getFrame()
|
||||||
return self.sprite.clone:getFrame()
|
|
||||||
else
|
|
||||||
return self.assets.sprites[self.sprite.name]:getFrame()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:getRelativeFrame()
|
function BaseActor:getRelativeFrame()
|
||||||
if (self.sprite.name ~= nil) then
|
if (self.sprite ~= nil) then
|
||||||
if (self.sprite.clone ~= nil) then
|
return self.sprite:getRelativeFrame()
|
||||||
return self.sprite.clone:getRelativeFrame()
|
|
||||||
else
|
|
||||||
return self.assets.sprites[self.sprite.name]:getRelativeFrame()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:getAnimationDuration()
|
function BaseActor:getAnimationDuration()
|
||||||
if (self.sprite.name ~= nil) then
|
if (self.sprite ~= nil) then
|
||||||
if (self.sprite.clone ~= nil) then
|
return self.sprite:getAnimationDuration()
|
||||||
return self.sprite.clone:getAnimationDuration()
|
|
||||||
else
|
|
||||||
return self.assets.sprites[self.sprite.name]:getAnimationDuration()
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky)
|
function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky)
|
||||||
if (self.sprite.name ~= nil) then
|
if (self.sprite ~= nil) then
|
||||||
local x = x + self.sprite.ox
|
self.sprite:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
||||||
local y = y + self.sprite.oy
|
|
||||||
local sx = sx or self.sprite.sx
|
|
||||||
local sy = sy or self.sprite.sy
|
|
||||||
if (self.sprite.clone ~= nil) then
|
|
||||||
self.sprite.clone:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
||||||
else
|
|
||||||
self.assets.sprites[self.sprite.name]:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
120
birb/modules/world/actors/utils/sprites.lua
Normal file
120
birb/modules/world/actors/utils/sprites.lua
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
local Sprite = Object:extend()
|
||||||
|
|
||||||
|
-- SPRITES FUNCTIONS
|
||||||
|
-- Handle the sprite of the actor
|
||||||
|
|
||||||
|
function Sprite:new(owner, spritename, ox, oy)
|
||||||
|
self.owner = owner
|
||||||
|
self.assets = self.owner.assets
|
||||||
|
self.name = spritename or nil
|
||||||
|
self.ox = ox or 0
|
||||||
|
self.oy = oy or 0
|
||||||
|
self.sx = 1
|
||||||
|
self.sy = 1
|
||||||
|
self.exist = (spritename ~= nil)
|
||||||
|
self.spriteClone = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:clone()
|
||||||
|
if self.name ~= nil then
|
||||||
|
self.spriteClone = self.assets.sprites[self.name]:clone()
|
||||||
|
self.spriteClone:setCallbackTarget(self.owner)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:isCloned()
|
||||||
|
return (self.spriteClone == nil)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:changeAnimation(animation, restart)
|
||||||
|
if (self:isCloned()) then
|
||||||
|
self.assets.sprites[self.name]:changeAnimation(animation, restart)
|
||||||
|
else
|
||||||
|
self.spriteClone:changeAnimation(animation, restart)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:setCustomSpeed(customSpeed)
|
||||||
|
if (self:isCloned()) then
|
||||||
|
self.assets.sprites[self.name]:setCustomSpeed(customSpeed)
|
||||||
|
else
|
||||||
|
self.spriteClone:setCustomSpeed(customSpeed)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:update(dt)
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
self.spriteClone:update(dt)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:setScallingX(sx)
|
||||||
|
local sx = sx or 1
|
||||||
|
|
||||||
|
self.sx = sx
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:setScallingY(sy)
|
||||||
|
local sy = sy or 1
|
||||||
|
|
||||||
|
self.sy = sy
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getCurrentAnimation()
|
||||||
|
if (self:isCloned()) then
|
||||||
|
return self.assets.sprites[self.name]:getCurrentAnimation()
|
||||||
|
else
|
||||||
|
return self.spriteClone:getCurrentAnimation()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function Sprite:getScalling()
|
||||||
|
return self.sx, self.sy
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getFrame()
|
||||||
|
if (self.name ~= nil) then
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
return self.spriteClone:getFrame()
|
||||||
|
else
|
||||||
|
return self.assets.sprites[self.name]:getFrame()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getRelativeFrame()
|
||||||
|
if (self.name ~= nil) then
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
return self.spriteClone:getRelativeFrame()
|
||||||
|
else
|
||||||
|
return self.assets.sprites[self.name]:getRelativeFrame()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:getAnimationDuration()
|
||||||
|
if (self.name ~= nil) then
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
return self.spriteClone:getAnimationDuration()
|
||||||
|
else
|
||||||
|
return self.assets.sprites[self.name]:getAnimationDuration()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function Sprite:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
||||||
|
if (self.name ~= nil) then
|
||||||
|
local x = x + self.ox
|
||||||
|
local y = y + self.oy
|
||||||
|
local sx = sx or self.sx
|
||||||
|
local sy = sy or self.sy
|
||||||
|
if (self.spriteClone ~= nil) then
|
||||||
|
self.spriteClone:draw(x, y, r, sx, sy, ox, oy, kx, ky)
|
||||||
|
else
|
||||||
|
self.assets.sprites[self.name]:drawAnimation(x, y, r, sx, sy, ox, oy, kx, ky)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return Sprite
|
Loading…
Reference in a new issue