Prexxo

Custom Roblox TweenService

Mar 21st, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. local Chrome = require(script.Parent)
  2. local Utility = require(Chrome.Utility)
  3.  
  4. local RBXTweenService = game:GetService("TweenService")
  5. local RunService = game:GetService("RunService")
  6.  
  7. local Heartbeat = RunService.Heartbeat
  8.  
  9. local TweenHandlerObjectLimit = 126
  10.  
  11. local HandlersLength = 0
  12.  
  13. local function LerpNumber(a,b,c)
  14.     return a + (b-a) * c
  15. end
  16.  
  17. local function RemoveFromList(List, Value, Index)
  18.     table.remove(List, Index or table.find(List, Value))
  19.     Value = nil
  20. end
  21.  
  22. local function SortFunction(a, b)  
  23.     return a.Length < b.Length
  24. end
  25.  
  26. local AvaibleHandlers = {}
  27.  
  28. local function SortAvaibleHandlers()
  29.     table.sort(AvaibleHandlers, SortFunction)
  30. end
  31.  
  32. local function RunTweening(TweenHandler)
  33.     local TweenList = TweenHandler.TweenList
  34.  
  35.     coroutine.wrap(function()
  36.         while TweenHandler.Length > 0 do
  37.             local Step = Heartbeat:Wait()
  38.             for i = TweenHandler.Length, 1, -1 do
  39.                 local TweenObject = TweenList[i]
  40.                 local Object = TweenObject[1]
  41.                 local Info = TweenObject[2]
  42.                 local GoalData = TweenObject[3]
  43.  
  44.                 local Progress = TweenObject[6] + Step * TweenObject[5]
  45.                 Progress = Progress > 1 and 1 or Progress
  46.  
  47.                 for i = 1, TweenObject[4] do
  48.                     local CurrentGoalData = GoalData[i]
  49.                     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]))
  50.                 end
  51.  
  52.                 if Progress == 1 then
  53.                     RemoveFromList(TweenList, TweenObject, i)
  54.                     TweenHandler.Length -= 1                   
  55.                     TweenObject.Completed:Fire()
  56.                    
  57.                     SortAvaibleHandlers()
  58.                     continue
  59.                 end
  60.  
  61.                 TweenObject[6] = Progress
  62.             end
  63.         end
  64.        
  65.         --// After TweenList is empty ( completed tweening all scheduled objects) removes the list
  66.         HandlersLength -= 1
  67.         RemoveFromList(AvaibleHandlers, TweenHandler)
  68.     end)()
  69. end
  70.  
  71. local function CreateTweenHandler()
  72.     local TweenHandler = {
  73.         Length = 0,
  74.         TweenList = {},
  75.         Running = false
  76.     }
  77.  
  78.     --// Cache into avaible Handlers
  79.     HandlersLength += 1
  80.     AvaibleHandlers[HandlersLength] = TweenHandler
  81.    
  82.     SortAvaibleHandlers()
  83.    
  84.     return TweenHandler
  85. end
  86.  
  87. local function PlayMethod(TweenObject)
  88.     local TweenHandler = AvaibleHandlers[1] or CreateTweenHandler()
  89.     local Length = TweenHandler.Length + 1
  90.    
  91.     if Length > TweenHandlerObjectLimit then
  92.         --// Remove from avaible
  93.         table.remove(AvaibleHandlers, table.find(AvaibleHandlers, TweenHandler))
  94.         HandlersLength -= 1
  95.        
  96.         ----// Add to avaible
  97.         TweenHandler = CreateTweenHandler()
  98.         Length = 1
  99.     end
  100.  
  101.     TweenHandler.Length = Length
  102.    
  103.     TweenHandler.TweenList[Length] = TweenObject
  104.    
  105.     TweenObject.Handler = TweenHandler
  106.     TweenObject.ListIndex = Length
  107.    
  108.     if not TweenHandler.Running then TweenHandler.Running = true RunTweening(TweenHandler) end 
  109. end
  110.  
  111. local function StopMethod(TweenObject)
  112.     local Handler = TweenObject.Handler
  113.    
  114.     Handler.Length -= 1
  115.     table.remove(Handler.TweenList, TweenObject.ListIndex)
  116. end
  117.  
  118. local Module = {}
  119.  
  120. function Module.new(Object, Info, Goal)
  121.     Info = type(Info) == "number" and {Info} or Info
  122.     Info[2] = Info[2] or "Linear"
  123.     Info[3] = Info[3] or "In"
  124.  
  125.     local GoalData = {}
  126.     local GoalLength = 0
  127.  
  128.     for Property, EndValue in pairs(Goal) do
  129.         GoalLength += 1
  130.         GoalData[GoalLength] = {Property, Object[Property], EndValue, Type = type(EndValue)}
  131.     end
  132.  
  133.     local TweenObject = {
  134.         Object,
  135.         Info,
  136.         GoalData,
  137.         GoalLength,
  138.        
  139.         1 / Info[1], --// 1 / Time, Time represents how much time it takes to Tween the object
  140.         0, --// Progress
  141.  
  142.         Play = PlayMethod,
  143.         Stop = StopMethod,
  144.        
  145.         Completed = Utility.NewEvent()
  146.     }
  147.    
  148.     return TweenObject
  149. end
  150.  
  151. return Module
Add Comment
Please, Sign In to add comment