From 5393d16007f7832cfb0758eac243b754967713be Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 26 Nov 2020 18:40:25 +0100 Subject: [PATCH] chore: divide simple target into mixins --- birb/modules/world/actors/actor2D.lua | 10 ++ birb/modules/world/actors/actor3D.lua | 9 ++ birb/modules/world/actors/mixins/base.lua | 122 ++----------------- birb/modules/world/actors/mixins/inputs.lua | 34 ++++++ birb/modules/world/actors/mixins/sprites.lua | 101 +++++++++++++++ birb/modules/world/actors/mixins/timers.lua | 47 +++++++ 6 files changed, 214 insertions(+), 109 deletions(-) create mode 100644 birb/modules/world/actors/mixins/inputs.lua create mode 100644 birb/modules/world/actors/mixins/sprites.lua create mode 100644 birb/modules/world/actors/mixins/timers.lua diff --git a/birb/modules/world/actors/actor2D.lua b/birb/modules/world/actors/actor2D.lua index d9aae51..4f1ccd6 100644 --- a/birb/modules/world/actors/actor2D.lua +++ b/birb/modules/world/actors/actor2D.lua @@ -23,8 +23,15 @@ ]] local BaseActor = require("birb.modules.world.actors.mixins.base") +local SpritedActor = require("birb.modules.world.actors.mixins.sprites") +local TimedActor = require("birb.modules.world.actors.mixins.timers") +local InputActor = require("birb.modules.world.actors.mixins.inputs") + local Actor2D = Object:extend() Actor2D:implement(BaseActor) +Actor2D:implement(SpritedActor) +Actor2D:implement(TimedActor) +Actor2D:implement(InputActor) local Hitbox = require("birb.modules.world.actors.utils.hitbox2D") @@ -34,6 +41,9 @@ local Hitbox = require("birb.modules.world.actors.utils.hitbox2D") function Actor2D:new(world, type, x, y, w, h, isSolid) self:init(world, type, x, y, 0, w, h, 0, isSolid) self:initHitboxes() + self:initTimers() + self:initSprite() + self:initKeys() end function Actor2D:destroy() diff --git a/birb/modules/world/actors/actor3D.lua b/birb/modules/world/actors/actor3D.lua index 8d13250..1787179 100644 --- a/birb/modules/world/actors/actor3D.lua +++ b/birb/modules/world/actors/actor3D.lua @@ -24,8 +24,15 @@ local cwd = (...):gsub('%.actor3D$', '') .. "." local BaseActor = require("birb.modules.world.actors.mixins.base") +local SpritedActor = require("birb.modules.world.actors.mixins.sprites") +local TimedActor = require("birb.modules.world.actors.mixins.timers") +local InputActor = require("birb.modules.world.actors.mixins.inputs") + local Actor3D = Object:extend() Actor3D:implement(BaseActor) +Actor3D:implement(SpritedActor) +Actor3D:implement(TimedActor) +Actor3D:implement(InputActor) local Hitbox = require(cwd .. "utils.hitbox3D") local Boxes = require(cwd .. "utils.boxes") @@ -36,6 +43,8 @@ local Boxes = require(cwd .. "utils.boxes") function Actor3D:new(world, type, x, y, z, w, h, d, isSolid) self:init(world, type, x, y, z, w, h, d, isSolid) self:initHitboxes() + self:initTimers() + self:initSprite() self.world:registerShape(self) self.boxes = Boxes self.doCastShadows = true diff --git a/birb/modules/world/actors/mixins/base.lua b/birb/modules/world/actors/mixins/base.lua index 2313340..1ac7abe 100644 --- a/birb/modules/world/actors/mixins/base.lua +++ b/birb/modules/world/actors/mixins/base.lua @@ -23,7 +23,6 @@ ]] local BaseActor = Object:extend() -local Sprite = require("birb.modules.world.actors.utils.sprites") -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -35,8 +34,6 @@ function BaseActor:init(world, type, x, y, z, w, h, d, isSolid) self:setManagers(world) self:initKeys() - self:initTimers() - self:setSprite() self:initPhysics(x, y, z, w, h, d) self:setDebugColor(1, 1, 1) @@ -89,6 +86,8 @@ function BaseActor:initPhysics(x, y, z, w, h, d) self:setBounceFactor() self:setFilter() + + self.updateFunctions = {} end function BaseActor:setCoordinate(x, y, z, w, h, d) @@ -196,12 +195,21 @@ end function BaseActor:update(dt) self:updateStart(dt) - self:updateTimers(dt) self:autoMove(dt) - self:updateSprite(dt) + self:applyUpdateFunctions(dt) self:updateEnd(dt) end +function BaseActor:addUpdateFunction(func) + table.insert(self.updateFunctions, func) +end + +function BaseActor:applyUpdateFunctions(dt) + for key, func in ipairs(self.updateFunctions) do + func(self, dt) + end +end + function BaseActor:updateEnd(dt) end @@ -212,37 +220,6 @@ function BaseActor:autoMove(dt) -- 2D and 3D childrens end --- INPUT FUNCTIONS --- get input from the world object - -function BaseActor:initKeys() - self.keys = core.input.fakekeys -end - -function BaseActor:getInput(keys) - self.keys = keys or core.input.fakekeys -end - --- TIMER FUNCTIONS --- Control the integrated timers of the actor - -function BaseActor:initTimers() - self.timers = core.modules.Timers(self) -end - -function BaseActor:addTimer(name, t) - core.debug:logWarn("actor", "function actor:addTimer is deprecated, prefer actor.timers:newTimer") - self.timers:newTimer(t, name) -end - -function BaseActor:updateTimers(dt) - self.timers:update(dt) -end - -function BaseActor:timerResponse(name) - -- here come the timer responses -end - -- HITBOX FUNCTIONS -- All functions to handle hitboxes @@ -358,77 +335,4 @@ function BaseActor:drawHUD(id, height, width) end --- SPRITES FUNCTIONS --- Handle the sprite of the actor - -function BaseActor:setSprite(spritename, isClone, ox, oy) - self.sprite = Sprite(self, spritename, ox, oy) - if (isClone) then - self.sprite:clone() - end -end - -function BaseActor:changeAnimation(animation, restart) - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:changeAnimation is deprecated, prefer BaseActor.sprite:changeAnimation()") - self.sprite:changeAnimation(animation, restart) - end -end - -function BaseActor:animationEnded(animation) - -- Empty placeholder function -end - -function BaseActor:setCustomSpeed(customSpeed) - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:setCustomSpeed is deprecated, prefer BaseActor.sprite:setCustomSpeed()") - self.sprite:setCustomSpeed(customSpeed) - end -end - -function BaseActor:updateSprite(dt) - if (self.sprite ~= nil) then - self.sprite:update(dt) - end -end - -function BaseActor:getCurrentAnimation() - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:getCurrentAnimation is deprecated, prefer BaseActor.sprite:getCurrentAnimation()") - return self.sprite:getCurrentAnimation() - end -end - -function BaseActor:getSpriteScalling() - core.debug:logWarn("actor", "the function BaseActor:getSpriteScalling is deprecated, prefer BaseActor.sprite:getScalling()") - return self.sprite:getScalling() -end - -function BaseActor:getFrame() - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:getFrame is deprecated, prefer BaseActor.sprite:getFrame()") - return self.sprite:getFrame() - end -end - -function BaseActor:getRelativeFrame() - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:getRelativeFrame is deprecated, prefer BaseActor.sprite:getRelativeFrame()") - return self.sprite:getRelativeFrame() - end -end - -function BaseActor:getAnimationDuration() - if (self.sprite ~= nil) then - core.debug:logWarn("actor", "the function BaseActor:getAnimationDuration is deprecated, prefer BaseActor.sprite:getAnimationDuration()") - return self.sprite:getAnimationDuration() - end -end - -function BaseActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) - if (self.sprite ~= nil) then - self.sprite:draw(x, y, r, sx, sy, ox, oy, kx, ky) - end -end - return BaseActor diff --git a/birb/modules/world/actors/mixins/inputs.lua b/birb/modules/world/actors/mixins/inputs.lua new file mode 100644 index 0000000..cc4a1b3 --- /dev/null +++ b/birb/modules/world/actors/mixins/inputs.lua @@ -0,0 +1,34 @@ +-- SpritedActor.lua :: Get input from the world object. + +--[[ + 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. +]] + +local InputActor = Object:extend() + +function InputActor:initKeys() + self.keys = core.input.fakekeys +end + +function InputActor:getInput(keys) + self.keys = keys or core.input.fakekeys +end + +return InputActor \ No newline at end of file diff --git a/birb/modules/world/actors/mixins/sprites.lua b/birb/modules/world/actors/mixins/sprites.lua new file mode 100644 index 0000000..51bf449 --- /dev/null +++ b/birb/modules/world/actors/mixins/sprites.lua @@ -0,0 +1,101 @@ +-- SpritedActor.lua :: Handle the sprite of the actor. + +--[[ + 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. +]] + +local SpritedActor = Object:extend() +local Sprite = require("birb.modules.world.actors.utils.sprites") + +function SpritedActor:initSprite() + self:addUpdateFunction(self.updateSprite) +end + +function SpritedActor:setSprite(spritename, isClone, ox, oy) + self.sprite = Sprite(self, spritename, ox, oy) + if (isClone) then + self.sprite:clone() + end +end + +function SpritedActor:changeAnimation(animation, restart) + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:changeAnimation is deprecated, prefer SpritedActor.sprite:changeAnimation()") + self.sprite:changeAnimation(animation, restart) + end +end + +function SpritedActor:animationEnded(animation) + -- Empty placeholder function +end + +function SpritedActor:setCustomSpeed(customSpeed) + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:setCustomSpeed is deprecated, prefer SpritedActor.sprite:setCustomSpeed()") + self.sprite:setCustomSpeed(customSpeed) + end +end + +function SpritedActor:updateSprite(dt) + if (self.sprite ~= nil) then + self.sprite:update(dt) + end +end + +function SpritedActor:getCurrentAnimation() + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:getCurrentAnimation is deprecated, prefer SpritedActor.sprite:getCurrentAnimation()") + return self.sprite:getCurrentAnimation() + end +end + +function SpritedActor:getSpriteScalling() + core.debug:logWarn("actor", "the function SpritedActor:getSpriteScalling is deprecated, prefer SpritedActor.sprite:getScalling()") + return self.sprite:getScalling() +end + +function SpritedActor:getFrame() + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:getFrame is deprecated, prefer SpritedActor.sprite:getFrame()") + return self.sprite:getFrame() + end +end + +function SpritedActor:getRelativeFrame() + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:getRelativeFrame is deprecated, prefer SpritedActor.sprite:getRelativeFrame()") + return self.sprite:getRelativeFrame() + end +end + +function SpritedActor:getAnimationDuration() + if (self.sprite ~= nil) then + core.debug:logWarn("actor", "the function SpritedActor:getAnimationDuration is deprecated, prefer SpritedActor.sprite:getAnimationDuration()") + return self.sprite:getAnimationDuration() + end +end + +function SpritedActor:drawSprite(x, y, r, sx, sy, ox, oy, kx, ky) + if (self.sprite ~= nil) then + self.sprite:draw(x, y, r, sx, sy, ox, oy, kx, ky) + end +end + +return SpritedActor \ No newline at end of file diff --git a/birb/modules/world/actors/mixins/timers.lua b/birb/modules/world/actors/mixins/timers.lua new file mode 100644 index 0000000..7e045b0 --- /dev/null +++ b/birb/modules/world/actors/mixins/timers.lua @@ -0,0 +1,47 @@ +-- TimedActor.lua :: Handle the sprite of the actor. + +--[[ + 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. +]] + +local TimedActor = Object:extend() + +-- TIMER FUNCTIONS +-- Control the integrated timers of the actor + +function TimedActor:initTimers() + self.timers = core.modules.Timers(self) + self:addUpdateFunction(self.updateTimers) +end + +function TimedActor:addTimer(name, t) + core.debug:logWarn("actor", "function actor:addTimer is deprecated, prefer actor.timers:newTimer") + self.timers:newTimer(t, name) +end + +function TimedActor:updateTimers(dt) + self.timers:update(dt) +end + +function TimedActor:timerResponse(name) + -- here come the timer responses +end + +return TimedActor \ No newline at end of file