2019-08-16 22:37:27 +02:00
|
|
|
local TweenManager = Object:extend()
|
|
|
|
|
|
|
|
local tween = require "game.modules.tweenmanager.libs.tween"
|
|
|
|
|
|
|
|
function TweenManager:new(subject)
|
|
|
|
self.subject = subject
|
|
|
|
self.timer = 0
|
|
|
|
|
|
|
|
self.tweens = {}
|
2019-08-17 15:51:02 +02:00
|
|
|
self.switches = {}
|
2019-08-16 22:37:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function TweenManager:newTween(start, duration, target, easing)
|
|
|
|
local newTween = {}
|
|
|
|
-- we add the data into a tween wrapper
|
|
|
|
newTween.tween = tween.new(duration, self.subject, target, easing)
|
|
|
|
newTween.start = self.timer + start -- /!\ START IS RELATIVE TO CURRENT TIME
|
|
|
|
newTween.clear = newTween.start + duration
|
|
|
|
|
|
|
|
table.insert(self.tweens, newTween)
|
|
|
|
end
|
|
|
|
|
2019-08-17 15:51:02 +02:00
|
|
|
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
|
|
|
|
|
2019-08-16 22:37:27 +02:00
|
|
|
function TweenManager:update(dt)
|
|
|
|
self.timer = self.timer + dt
|
|
|
|
|
|
|
|
for i, tweenWrapper in ipairs(self.tweens) do
|
|
|
|
if (self.timer > tweenWrapper.start) then
|
|
|
|
tweenWrapper.tween:update(dt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-17 15:51:02 +02:00
|
|
|
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
|
|
|
|
|
2019-08-16 22:37:27 +02:00
|
|
|
self:clearEndedTweens()
|
|
|
|
end
|
|
|
|
|
|
|
|
function TweenManager:clearEndedTweens(dt)
|
|
|
|
for i, tweenWrapper in ipairs(self.tweens) do
|
|
|
|
if (self.timer > tweenWrapper.clear) then
|
|
|
|
table.remove(self.tweens, i)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return TweenManager
|