feat(timer): add a switch system

This commit is contained in:
Kazhnuz 2019-09-08 14:21:21 +02:00
parent 8290fbb8d2
commit 02bc52a49b

View file

@ -32,15 +32,21 @@ function TimersManager:new(subject)
self.time = 0 self.time = 0
self.timers = {} self.timers = {}
self.switches = {}
end end
-- UPDATE -- UPDATE
-- Update every timers -- Update every timers
function TimersManager:update(dt) function TimersManager:update(dt)
self.time = self.time + dt
for k, timer in pairs(self.timers) do for k, timer in pairs(self.timers) do
timer:update(dt) timer:update(dt)
end end
self:updateSwitches(dt)
end end
-- TIMER FUNCTIONS -- TIMER FUNCTIONS
@ -58,4 +64,35 @@ function TimersManager:timerResponse(timername)
self.subject:timerResponse(timername) self.subject:timerResponse(timername)
end end
-- SWITCH FUNCTIONS
-- Handle switches
function TimersManager:newSwitch(start, bools)
local newSwitch = {}
-- we add the data into a switch wrapper
newSwitch.bools = bools
newSwitch.start = self.time + start -- /!\ START IS RELATIVE TO CURRENT TIME
newSwitch.clear = newSwitch.start + 1
table.insert(self.switches, newSwitch)
end
function TimersManager:updateSwitches(dt)
for i, switch in ipairs(self.switches) do
if (self.time > switch.start) then
-- We test each boolean in the switch
for i, bool in ipairs(switch.bools) do
-- if it's nil, we set it to true
if self.subject[bool] == nil then
self.subject[bool] = true
else
-- if it's not nil, we reverse the boolean
self.subject[bool] = (self.subject[bool] == false)
end
end
table.remove(self.switches, i)
end
end
end
return TimersManager return TimersManager