From d1a5bb9d39c168641e809b0d9708d107df21266a Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sun, 5 May 2019 20:13:19 +0200 Subject: [PATCH] modules/world: add a timer system to the actors --- gamecore/modules/world/actors/actor2D.lua | 27 +++++++++++- gamecore/modules/world/actors/utils/timer.lua | 44 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 gamecore/modules/world/actors/utils/timer.lua diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 16101de..46a7a00 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -22,7 +22,10 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] +local cwd = (...):gsub('%.actor2D$', '') .. "." + local Actor2D = Object:extend() +local Timer = require(cwd .. "utils.timer") -- INIT FUNCTIONS -- Initialise the actor and its base functions @@ -35,6 +38,7 @@ function Actor2D:new(world, type, x, y, w, h, isSolid) self:initKeys() self:register() + self:initTimers() self:setDebugColor(1, 1, 1) end @@ -99,11 +103,32 @@ function Actor2D:getInput(keys) self.keys = keys or core.input.fakekeys end +-- TIMER FUNCTIONS +-- Control the integrated timers of the actor + +function Actor2D:initTimers() + self.timers = {} +end + +function Actor2D:addTimer(name, t) + self.timers[name] = Timer(self, name, t) +end + +function Actor2D:updateTimers(dt) + for k,v in pairs(self.timers) do + v:update(dt) + end +end + +function Actor2D:timerResponse(name) + -- here come the timer responses +end + -- UPDATE FUNCTIONS -- Theses functions are activated every steps function Actor2D:update(dt) - + self:updateTimers(dt) self:autoMove(dt) self:updateSprite(dt) end diff --git a/gamecore/modules/world/actors/utils/timer.lua b/gamecore/modules/world/actors/utils/timer.lua new file mode 100644 index 0000000..5ba7c3f --- /dev/null +++ b/gamecore/modules/world/actors/utils/timer.lua @@ -0,0 +1,44 @@ +-- timer.lua :: a basic implementation of a timer for the actor system. + +--[[ + 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 Timer = Object:extend() + +function Timer:new(actor, name, t) + self.time = t + self.actor = actor + self.name = name +end + +function Timer:update(dt) + self.time = self.time - dt + if (self.time <= 0) then + self:finish() + end +end + +function Timer:finish() + self.actor:timerResponse(self.name) + self.actor.timers[self.name] = nil +end + +return Timer