sonic-radiance/sonic-radiance.love/game/modules/tweenmanager/init.lua

70 lines
1.8 KiB
Lua
Raw Normal View History

local TweenManager = Object:extend()
local tween = require "game.modules.tweenmanager.libs.tween"
function TweenManager:new(subject)
self.subject = subject
self.timer = 0
self.tweens = {}
self.switches = {}
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
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
for i, tweenWrapper in ipairs(self.tweens) do
if (self.timer > tweenWrapper.start) then
tweenWrapper.tween: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
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