From 8b3a5f1f0ccbb89dd99ab4d11029bbd9b91b17c3 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 10 May 2020 11:14:51 +0200 Subject: [PATCH] chore: extract the sprite of the actor --- birb/modules/world/actors/baseactor.lua | 89 +++++---------- birb/modules/world/actors/utils/sprites.lua | 120 ++++++++++++++++++++ 2 files changed, 149 insertions(+), 60 deletions(-) create mode 100644 birb/modules/world/actors/utils/sprites.lua diff --git a/birb/modules/world/actors/baseactor.lua b/birb/modules/world/actors/baseactor.lua index f034895..bfcfde9 100644 --- a/birb/modules/world/actors/baseactor.lua +++ b/birb/modules/world/actors/baseactor.lua @@ -26,6 +26,7 @@ local cwd = (...):gsub('%.baseactor$', '') .. "." local BaseActor = Object:extend() local Timer = require(cwd .. "utils.timer") +local Sprite = require(cwd .. "utils.sprites") -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -364,28 +365,20 @@ end -- Handle the sprite of the actor function BaseActor:setSprite(spritename, ox, oy) - self.sprite = {} - 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 + self.sprite = Sprite(self, spritename, ox, oy) end function BaseActor:cloneSprite() - if self.sprite.name ~= nil then - self.sprite.clone = self.assets.sprites[self.sprite.name]:clone() - self.sprite.clone:setCallbackTarget(self) + if self.sprite ~= nil then + core.debug:warning("actor", "the function BaseActor:cloneSprite is deprecated, prefer BaseActor.sprite:clone()") + self.sprite:clone() end end function BaseActor:changeAnimation(animation, restart) - if (self.sprite.clone == nil) then - self.assets.sprites[self.sprite.name]:changeAnimation(animation, restart) - else - self.sprite.clone:changeAnimation(animation, restart) + if (self.sprite ~= nil) then + core.debug:warning("actor", "the function BaseActor:changeAnimation is deprecated, prefer BaseActor.sprite:changeAnimation()") + self.sprite:changeAnimation(animation, restart) end end @@ -394,85 +387,61 @@ function BaseActor:animationEnded(animation) end function BaseActor:setCustomSpeed(customSpeed) - if (self.sprite.clone == nil) then - self.assets.sprites[self.sprite.name]:setCustomSpeed(customSpeed) - else - self.sprite.clone:setCustomSpeed(customSpeed) + if (self.sprite ~= nil) then + core.debug:warning("actor", "the function BaseActor:setCustomSpeed is deprecated, prefer BaseActor.sprite:setCustomSpeed()") + self.sprite:setCustomSpeed(customSpeed) end end function BaseActor:updateSprite(dt) - if (self.sprite.clone ~= nil) then - self.sprite.clone:update(dt) + if (self.sprite ~= nil) then + self.sprite:update(dt) end end function BaseActor:setSpriteScallingX(sx) - local sx = sx or 1 - - self.sprite.sx = sx + core.debug:warning("actor", "the function BaseActor:setSpriteScallingX is deprecated, prefer BaseActor.sprite:setSpriteScallingX()") + self.sprite:setScallingX(sx) end function BaseActor:setSpriteScallingY(sy) - local sy = sy or 1 - - self.sprite.sy = sy + core.debug:warning("actor", "the function BaseActor:setSpriteScallingY is deprecated, prefer BaseActor.sprite:setSpriteScallingY()") + self.sprite:setScallingY(sY) end function BaseActor:getCurrentAnimation() - if (self.sprite.clone == nil) then - return self.assets.sprites[self.sprite.name]:getCurrentAnimation() - else - return self.sprite.clone:getCurrentAnimation() + if (self.sprite ~= nil) then + core.debug:warning("actor", "the function BaseActor:getCurrentAnimation is deprecated, prefer BaseActor.sprite:getCurrentAnimation()") + return self.sprite:getCurrentAnimation() end end function BaseActor:getSpriteScalling() - return self.sprite.sx, self.sprite.sy + return self.sprite:getScalling() end function BaseActor:getFrame() - if (self.sprite.name ~= nil) then - if (self.sprite.clone ~= nil) then - return self.sprite.clone:getFrame() - else - return self.assets.sprites[self.sprite.name]:getFrame() - end + if (self.sprite ~= nil) then + return self.sprite:getFrame() end end function BaseActor:getRelativeFrame() - if (self.sprite.name ~= nil) then - if (self.sprite.clone ~= nil) then - return self.sprite.clone:getRelativeFrame() - else - return self.assets.sprites[self.sprite.name]:getRelativeFrame() - end + if (self.sprite ~= nil) then + return self.sprite:getRelativeFrame() end end function BaseActor:getAnimationDuration() - if (self.sprite.name ~= nil) then - if (self.sprite.clone ~= nil) then - return self.sprite.clone:getAnimationDuration() - else - return self.assets.sprites[self.sprite.name]:getAnimationDuration() - end + if (self.sprite ~= nil) then + return self.sprite:getAnimationDuration() end end function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) - if (self.sprite.name ~= nil) then - local x = x + self.sprite.ox - 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 + if (self.sprite ~= nil) then + self.sprite:draw(x, y, r, sx, sy, ox, oy, kx, ky) end end diff --git a/birb/modules/world/actors/utils/sprites.lua b/birb/modules/world/actors/utils/sprites.lua new file mode 100644 index 0000000..564e499 --- /dev/null +++ b/birb/modules/world/actors/utils/sprites.lua @@ -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