modules/world: add a timer system to the actors
This commit is contained in:
parent
25a582821f
commit
d1a5bb9d39
2 changed files with 70 additions and 1 deletions
|
@ -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
|
||||
|
|
44
gamecore/modules/world/actors/utils/timer.lua
Normal file
44
gamecore/modules/world/actors/utils/timer.lua
Normal file
|
@ -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
|
Loading…
Reference in a new issue