chore: refactor the actor system

This commit is contained in:
Kazhnuz 2021-05-06 16:08:08 +02:00
parent 6c546e1e0e
commit 33427077a0
12 changed files with 363 additions and 178 deletions

View file

@ -124,10 +124,15 @@ end
-- CALLBACK FUNCTIONS -- CALLBACK FUNCTIONS
-- Handle getting a calback from the animation system -- Handle getting a calback from the animation system
function Animator:setCallback(actor) function Animator:setCallbackTarget(actor)
self.actor = actor self.actor = actor
end end
function Animator:setCallback(actor)
core.debug:warning("Animator", "Animator:setCallback is deprecated, prefer Animator:setCallbackTarget instead")
self:setCallbackTarget(actor)
end
function Animator:sendCallback() function Animator:sendCallback()
if (self.actor ~= nil) then if (self.actor ~= nil) then
self.actor:animationEnded(self.currentAnimation) self.actor:animationEnded(self.currentAnimation)

View file

@ -22,18 +22,28 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
local cwd = (...):gsub('%.actor2D$', '') .. "." local BaseActor = require "birb.modules.world.actors.mixins.base"
local BaseActor = require(cwd .. "baseactor") local SpritedActor = require("birb.modules.world.actors.mixins.sprites")
local Actor2D = BaseActor:extend() local TimedActor = require("birb.modules.world.actors.mixins.timers")
local InputActor = require("birb.modules.world.actors.mixins.inputs")
local Hitbox = require(cwd .. "utils.hitbox2D") 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"
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
function Actor2D:new(world, type, x, y, w, h, isSolid) function Actor2D:new(world, type, x, y, w, h, isSolid)
Actor2D.super.new(self, world, type, x, y, 0, w, h, 0, isSolid) self:init(world, type, x, y, 0, w, h, 0, isSolid)
self:initHitboxes() self:initHitboxes()
self:initTimers()
self:initSprite()
self:initKeys()
end end
function Actor2D:destroy() function Actor2D:destroy()

View file

@ -22,19 +22,30 @@
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]] ]]
local cwd = (...):gsub('%.actor3D$', '') .. "." local Hitbox = require("birb.modules.world.actors.utils.hitbox3D")
local BaseActor = require(cwd .. "baseactor") local Boxes = require("birb.modules.world.actors.utils.boxes")
local Actor3D = BaseActor:extend()
local Hitbox = require(cwd .. "utils.hitbox3D") local BaseActor = require("birb.modules.world.actors.mixins.base")
local Boxes = require(cwd .. "utils.boxes") 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)
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
function Actor3D:new(world, type, x, y, z, w, h, d, isSolid) function Actor3D:new(world, type, x, y, z, w, h, d, isSolid)
Actor3D.super.new(self, world, type, x, y, z, w, h, d, isSolid) self:init(world, type, x, y, z, w, h, d, isSolid)
self:initHitboxes() self:initHitboxes()
self:initTimers()
self:initSprite()
self:initKeys()
self.world:registerShape(self) self.world:registerShape(self)
self.boxes = Boxes self.boxes = Boxes
self.doCastShadows = true self.doCastShadows = true

View file

@ -29,8 +29,7 @@ function GFX:new(world, x, y, spritename)
local width, height = world.scene.assets.sprites[spritename]:getDimensions() local width, height = world.scene.assets.sprites[spritename]:getDimensions()
GFX.super.new(self, world, "gfx", x - (width/2), y - (height/2), width, height) GFX.super.new(self, world, "gfx", x - (width/2), y - (height/2), width, height)
self:setSprite(spritename) self:setSprite(spritename, true)
self:cloneSprite()
end end
function GFX:animationEnded(animation) function GFX:animationEnded(animation)

View file

@ -29,8 +29,7 @@ function GFX:new(world, x, y, z, spritename)
local width, height = world.scene.assets.sprites[spritename]:getDimensions() local width, height = world.scene.assets.sprites[spritename]:getDimensions()
GFX.super.new(self, world, "gfx", x - (width/2), y - (width/2), z - (height/2), width, width, height) GFX.super.new(self, world, "gfx", x - (width/2), y - (width/2), z - (height/2), width, width, height)
self:setSprite(spritename) self:setSprite(spritename, true)
self:cloneSprite()
end end
function GFX:animationEnded(animation) function GFX:animationEnded(animation)

View file

@ -24,20 +24,18 @@
local BaseActor = Object:extend() local BaseActor = Object:extend()
local Timer = require("birb.classes.time.timer")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- Initialise the actor and its base functions -- Initialise the actor and its base functions
function BaseActor:new(world, type, x, y, z, w, h, d, isSolid) function BaseActor:init(world, type, x, y, z, w, h, d, isSolid)
self.type = type or "" self.type = type or ""
self.isSolid = isSolid or false self.isSolid = isSolid or false
self.depth = 0 self.depth = 0
self.updateFunctions = {}
self:setManagers(world) self:setManagers(world)
self:initKeys() self:initKeys()
self:initTimers()
self:setSprite()
self:initPhysics(x, y, z, w, h, d) self:initPhysics(x, y, z, w, h, d)
self:setDebugColor(1, 1, 1) self:setDebugColor(1, 1, 1)
@ -49,6 +47,16 @@ function BaseActor:setProperties(properties)
-- Do something here -- Do something here
end 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:setManagers(world) function BaseActor:setManagers(world)
self.world = world self.world = world
self.scene = world.scene self.scene = world.scene
@ -202,9 +210,8 @@ end
function BaseActor:update(dt) function BaseActor:update(dt)
self:updateStart(dt) self:updateStart(dt)
self:updateTimers(dt)
self:autoMove(dt) self:autoMove(dt)
self:updateSprite(dt) self:applyUpdateFunctions(dt)
self:updateEnd(dt) self:updateEnd(dt)
end end
@ -218,38 +225,6 @@ function BaseActor:autoMove(dt)
-- 2D and 3D childrens -- 2D and 3D childrens
end 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 = {}
end
function BaseActor:addTimer(name, t)
self.timers[name] = Timer(self, name, t)
end
function BaseActor:updateTimers(dt)
for k,v in pairs(self.timers) do
v:update(dt)
end
end
function BaseActor:timerResponse(name)
-- here come the timer responses
end
-- HITBOX FUNCTIONS -- HITBOX FUNCTIONS
-- All functions to handle hitboxes -- All functions to handle hitboxes
@ -365,120 +340,4 @@ function BaseActor:drawHUD(id, height, width)
end end
-- SPRITES FUNCTIONS
-- 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
end
function BaseActor:cloneSprite()
if self.sprite.name ~= nil then
self.sprite.clone = self.assets.sprites[self.sprite.name]:clone()
self.sprite.clone:setCallback(self)
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)
end
end
function BaseActor:animationEnded(animation)
-- Empty placeholder function
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)
end
end
function BaseActor:updateSprite(dt)
if (self.sprite.clone ~= nil) then
self.sprite.clone:update(dt)
end
end
function BaseActor:setSpriteScallingX(sx)
local sx = sx or 1
self.sprite.sx = sx
end
function BaseActor:setSpriteScallingY(sy)
local sy = sy or 1
self.sprite.sy = 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()
end
end
function BaseActor:getSpriteScalling()
return self.sprite.sx, self.sprite.sy
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
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
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
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
end
end
return BaseActor return BaseActor

View file

@ -0,0 +1,34 @@
-- InputActor.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:warning("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:warning("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:warning("actor", "the function SpritedActor:getCurrentAnimation is deprecated, prefer SpritedActor.sprite:getCurrentAnimation()")
return self.sprite:getCurrentAnimation()
end
end
function SpritedActor:getSpriteScalling()
core.debug:warning("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:warning("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:warning("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:warning("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,49 @@
-- 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()
local TweenManager = require "birb.classes.time"
-- TIMER FUNCTIONS
-- Control the integrated timers of the actor
function TimedActor:initTimers()
self.timers = TweenManager(self)
self:addUpdateFunction(self.updateTimers)
end
function TimedActor:addTimer(name, t)
core.debug:warning("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

View 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

View file

@ -8,7 +8,7 @@ function Player:new(world, x, y, z, id)
Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true) Player.super.new(self, world, "player", x, y, 0, 16, 12, 24, true)
self:setGravity(480*2) self:setGravity(480*2)
self:init() self:initPlayer()
self.action = "normal" self.action = "normal"
@ -16,11 +16,10 @@ function Player:new(world, x, y, z, id)
self.score = 0 self.score = 0
end end
function Player:init() function Player:initPlayer()
self.charName = game.characters:getActiveCharacter() self.charName = game.characters:getActiveCharacter()
self.assets:addSprite(self.charName, "datas/gamedata/characters/" .. self.charName .. "/sprites") self.assets:addSprite(self.charName, "datas/gamedata/characters/" .. self.charName .. "/sprites")
self:setSprite(self.charName, 8, 10) self:setSprite(self.charName, true, 8, 10)
self:cloneSprite()
self.emblem = Emblem(game.characters:getActiveCharacterData(), self.scene) self.emblem = Emblem(game.characters:getActiveCharacterData(), self.scene)
self.guiborder = game.gui.newBorder(424, 20, 6) self.guiborder = game.gui.newBorder(424, 20, 6)
@ -103,7 +102,7 @@ function Player:setDirection(direction)
if direction ~= 0 then if direction ~= 0 then
direction = utils.math.sign(direction) direction = utils.math.sign(direction)
self.direction = direction self.direction = direction
self:setSpriteScallingX(direction) self.sprite:setScallingX(direction)
end end
end end

View file

@ -6,8 +6,7 @@ function GFX:new(world, x, y, spritename)
local width, height = world.scene.assets.sprites[spritename]:getDimensions() local width, height = world.scene.assets.sprites[spritename]:getDimensions()
GFX.super.new(self, world, "gfx", x - (width/2), y - (height/2), width, height) GFX.super.new(self, world, "gfx", x - (width/2), y - (height/2), width, height)
self:setSprite(spritename) self:setSprite(spritename, true)
self:cloneSprite()
end end
function GFX:animationEnded(animation) function GFX:animationEnded(animation)