Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Chrome = require(script.Parent)
- local Utility = require(Chrome.Utility)
- local RBXTweenService = game:GetService("TweenService")
- local RunService = game:GetService("RunService")
- local Heartbeat = RunService.Heartbeat
- local TweenHandlerObjectLimit = 126
- local HandlersLength = 0
- local function LerpNumber(a,b,c)
- return a + (b-a) * c
- end
- local function RemoveFromList(List, Value, Index)
- table.remove(List, Index or table.find(List, Value))
- Value = nil
- end
- local function SortFunction(a, b)
- return a.Length < b.Length
- end
- local AvaibleHandlers = {}
- local function SortAvaibleHandlers()
- table.sort(AvaibleHandlers, SortFunction)
- end
- local function RunTweening(TweenHandler)
- local TweenList = TweenHandler.TweenList
- coroutine.wrap(function()
- while TweenHandler.Length > 0 do
- local Step = Heartbeat:Wait()
- for i = TweenHandler.Length, 1, -1 do
- local TweenObject = TweenList[i]
- local Object = TweenObject[1]
- local Info = TweenObject[2]
- local GoalData = TweenObject[3]
- local Progress = TweenObject[6] + Step * TweenObject[5]
- Progress = Progress > 1 and 1 or Progress
- for i = 1, TweenObject[4] do
- local CurrentGoalData = GoalData[i]
- Object[CurrentGoalData[1]] = CurrentGoalData.Type == "number" and LerpNumber(CurrentGoalData[2], CurrentGoalData[3], Progress) or CurrentGoalData[2]:Lerp(CurrentGoalData[3], RBXTweenService:GetValue(Progress, Info[2], Info[3]))
- end
- if Progress == 1 then
- RemoveFromList(TweenList, TweenObject, i)
- TweenHandler.Length -= 1
- TweenObject.Completed:Fire()
- SortAvaibleHandlers()
- continue
- end
- TweenObject[6] = Progress
- end
- end
- --// After TweenList is empty ( completed tweening all scheduled objects) removes the list
- HandlersLength -= 1
- RemoveFromList(AvaibleHandlers, TweenHandler)
- end)()
- end
- local function CreateTweenHandler()
- local TweenHandler = {
- Length = 0,
- TweenList = {},
- Running = false
- }
- --// Cache into avaible Handlers
- HandlersLength += 1
- AvaibleHandlers[HandlersLength] = TweenHandler
- SortAvaibleHandlers()
- return TweenHandler
- end
- local function PlayMethod(TweenObject)
- local TweenHandler = AvaibleHandlers[1] or CreateTweenHandler()
- local Length = TweenHandler.Length + 1
- if Length > TweenHandlerObjectLimit then
- --// Remove from avaible
- table.remove(AvaibleHandlers, table.find(AvaibleHandlers, TweenHandler))
- HandlersLength -= 1
- ----// Add to avaible
- TweenHandler = CreateTweenHandler()
- Length = 1
- end
- TweenHandler.Length = Length
- TweenHandler.TweenList[Length] = TweenObject
- TweenObject.Handler = TweenHandler
- TweenObject.ListIndex = Length
- if not TweenHandler.Running then TweenHandler.Running = true RunTweening(TweenHandler) end
- end
- local function StopMethod(TweenObject)
- local Handler = TweenObject.Handler
- Handler.Length -= 1
- table.remove(Handler.TweenList, TweenObject.ListIndex)
- end
- local Module = {}
- function Module.new(Object, Info, Goal)
- Info = type(Info) == "number" and {Info} or Info
- Info[2] = Info[2] or "Linear"
- Info[3] = Info[3] or "In"
- local GoalData = {}
- local GoalLength = 0
- for Property, EndValue in pairs(Goal) do
- GoalLength += 1
- GoalData[GoalLength] = {Property, Object[Property], EndValue, Type = type(EndValue)}
- end
- local TweenObject = {
- Object,
- Info,
- GoalData,
- GoalLength,
- 1 / Info[1], --// 1 / Time, Time represents how much time it takes to Tween the object
- 0, --// Progress
- Play = PlayMethod,
- Stop = StopMethod,
- Completed = Utility.NewEvent()
- }
- return TweenObject
- end
- return Module
Add Comment
Please, Sign In to add comment