chore: divide simple target into mixins

This commit is contained in:
Kazhnuz 2020-11-26 18:40:25 +01:00
parent b97d320dc7
commit 5393d16007
6 changed files with 214 additions and 109 deletions

View file

@ -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()

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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