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

44 lines
1 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.targets = {}
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: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
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