2013/Libraries/Fusion/Animation/getTweenRatio.luau

47 lines
1.1 KiB
Plaintext

--!strict
--[[
Given a `tweenInfo` and `currentTime`, returns a ratio which can be used to
tween between two values over time.
]]
-- local TweenService = game:GetService "TweenService"
local easing = require "../Polyfill/easing"
local function getTweenRatio(tweenInfo: TweenInfo, currentTime: number): number
local delay = tweenInfo.DelayTime
local duration = tweenInfo.Time
local reverses = tweenInfo.Reverses
local numCycles = 1 + tweenInfo.RepeatCount
local easeStyle = tweenInfo.EasingStyle
local easeDirection = tweenInfo.EasingDirection
local cycleDuration = delay + duration
if reverses then
cycleDuration += duration
end
if
currentTime >= cycleDuration * numCycles
and tweenInfo.RepeatCount > -1
then
return 1
end
local cycleTime = currentTime % cycleDuration
if cycleTime <= delay then
return 0
end
local tweenProgress = (cycleTime - delay) / duration
if tweenProgress > 1 then
tweenProgress = 2 - tweenProgress
end
-- return TweenService:GetValue(tweenProgress, easeStyle, easeDirection)
return easing[easeStyle][easeDirection](tweenProgress, 0, 1)
end
return getTweenRatio