feat(tweenmanager): add a switch system

This commit is contained in:
Kazhnuz 2019-08-17 15:51:02 +02:00
parent fcd1288ebf
commit 6418b77b39

View file

@ -7,7 +7,7 @@ function TweenManager:new(subject)
self.timer = 0
self.tweens = {}
self.targets = {}
self.switches = {}
end
function TweenManager:newTween(start, duration, target, easing)
@ -20,6 +20,16 @@ function TweenManager:newTween(start, duration, target, easing)
table.insert(self.tweens, newTween)
end
function TweenManager:newSwitch(start, bools)
local newSwitch = {}
-- we add the data into a tween wrapper
newSwitch.bools = bools
newSwitch.start = self.timer + start -- /!\ START IS RELATIVE TO CURRENT TIME
newSwitch.clear = newSwitch.start + 1
table.insert(self.switches, newSwitch)
end
function TweenManager:update(dt)
self.timer = self.timer + dt
@ -29,6 +39,22 @@ function TweenManager:update(dt)
end
end
for i, switch in ipairs(self.switches) do
if (self.timer > 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
self:clearEndedTweens()
end