Advertisement
Guest User

Easimation Module v1.0

a guest
Jul 11th, 2020
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.53 KB | None | 0 0
  1. --[[
  2.  
  3.  
  4. ███████╗░█████╗░░██████╗██╗███╗░░░███╗░█████╗░████████╗██╗░█████╗░███╗░░██╗
  5. ██╔════╝██╔══██╗██╔════╝██║████╗░████║██╔══██╗╚══██╔══╝██║██╔══██╗████╗░██║
  6. █████╗░░███████║╚█████╗░██║██╔████╔██║███████║░░░██║░░░██║██║░░██║██╔██╗██║
  7. ██╔══╝░░██╔══██║░╚═══██╗██║██║╚██╔╝██║██╔══██║░░░██║░░░██║██║░░██║██║╚████║
  8. ███████╗██║░░██║██████╔╝██║██║░╚═╝░██║██║░░██║░░░██║░░░██║╚█████╔╝██║░╚███║
  9. ╚══════╝╚═╝░░╚═╝╚═════╝░╚═╝╚═╝░░░░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚═╝░╚════╝░╚═╝░░╚══╝  V1.0
  10. MODULE BY REK_KIE 7/11/20
  11.  
  12.  
  13. IMPORTANT NOTE: For those of you who don't know, when using AnimationTrack weight and you want animations to
  14. "stop playing", make sure you NEVER set it to 0 if you're planning to adjust the weight to something higher again.
  15. Set it to a really small number like .000001, because once you set an AnimationTrack's weight to 0, the server
  16. won't account for the animation anymore, and the animation won't replicate. It's a weird ROBLOX thing, it's not
  17. anyone's fault.
  18.  
  19.  
  20. Anyways hello, and thanks for getting my Easimation module! Here's some documentation and how to use it:
  21.  
  22.  
  23. DOCUMENTATION:
  24.  
  25. -- ========== EVENTS ============
  26.  
  27. • easimation.Played/easimation.Stopped - fired when the tween starts/stops when using easimation:Play() and easimation:Stop() (before the tween is actually visible)
  28. • easimation.PlayCompleted/easimation.StopCompleted - fired when the tween finishes when using easimation:Play() and easimation:Stop()
  29. • easimation.WeightEaseStarted - fired when the easing starts when using easimation:EaseWeight()
  30. • easimation.WeightEaseCompleted - fired when the easing tween completes when using easimation:EaseWeight()
  31. • easimation.SpeedEaseStarted- fired when the easing starts when using easimation:EaseSpeed()
  32. • easimation.SpeedEaseCompleted - fired when the easing tween completes when using easimation:EaseSpeed()
  33. • easimation.Eased - fired during easing constantly when the value is being updated (kind of like a Changed event)
  34.  
  35.  
  36. -- ========== FUNCTIONS ============
  37.  
  38.  
  39. --  easimation.New(animation, humanoid)
  40.  
  41.     • Returns an easimation object with access to the easing functions, the easing events (listed above), and the original animation track.
  42.       You still have access to the original AnimationTrack's functions and properties, and you can access them by indexing the easimation
  43.       object like this:
  44.  
  45.       ex. easimation["animTrack"].IsPlaying --> would still be true or false
  46.  
  47.  
  48. --  easimationObject:Play(finalWeight, speed, tweenInfo)
  49.  
  50.     • After getting your easimation object by using easimation.New(), then you can use easimationObject:Play() where finalWeight is the
  51.       weight to ease to. :Play() eases from "0" to finalWeight, with tweenInfo specified by you. speed is the Speed to play
  52.       the animation at, obviously. Weights after 1 are the same visually, so if you enter a weight higher than 1 and want the same nice
  53.       tween effect, this module tweens the weight to 1 and then sets the weight to your specified weight right after.
  54.  
  55.  
  56. --  easimationObject:Stop(tweenInfo)
  57.  
  58.     • After getting your easimation object by using easimation.New(), then you can also use easimationObject:Stop()
  59.       where tweenInfo is the Tween info to ease with. It tweens from its current weight to 0.
  60.  
  61.  
  62. VIDEO THAT DEMONSTRATES ROBLOX'S NORMAL :Play() and :Stop(), which has no customization with easing and is forced linear:
  63. https://streamable.com/egkj6e
  64.  
  65. VIDEO DEMONSTRATING EASIMATION'S :Play() and :Stop() with customizable easing:
  66. https://streamable.com/7ud163
  67.  
  68.  
  69. --  easimationObject:EaseWeight(finalWeight, tweenInfo)
  70.  
  71.     • After getting your easimation object by using easimation.New(), then you can also use easimationObject:EaseWeight(),
  72.       where tweenInfo is the Tween info to ease with and finalWeight is the weight to tween to from its current weight.
  73.  
  74. VIDEO DEMONSTRATING EASIMATION'S :EaseWeight():
  75. https://streamable.com/9c79h9
  76.  
  77. --  easimationObject:EaseSpeed(finalSpeed, tweenInfo)
  78.  
  79.     • After getting your easimation object by using easimation.New(), then you can also use easimationObject:EaseSpeed(),
  80.       where tweenInfo is the Tween info to ease with and finalSpeed is the speed to tween to from its current speed.
  81.  
  82. VIDEO DEMONSTRATING EASIMATION'S :EaseSpeed():
  83. https://streamable.com/e74yl3
  84.  
  85.  
  86. -- ===============  INSTRUCTION MANUAL  ==================
  87.  
  88. To set it up, require it in the script(s) you need it for, obviously.
  89. Now, you need to make a new easimation object by using easimation.New() [documentation above]
  90.  
  91. ex. local anim = easimation.New(script:WaitForChild("Animation"), humanoid)
  92.  
  93. Now you have an easimation object with access to the functions and events, as well as the original AnimationTrack. You're only gaining,
  94. and not losing here.
  95.  
  96.  
  97. Demonstrations of each function:
  98.  
  99.     local anim = easimation.New(script:WaitForChild("Animation"), humanoid)
  100.    
  101.     Playing the animation:
  102.     anim:Play(1, 1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingStyle.Out))
  103.    
  104.     Stopping the animation:
  105.     anim:Stop(TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingStyle.Out))
  106.    
  107.     Easing the weight (let's assume its current weight is 0):
  108.     anim:EaseWeight(1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingStyle.Out)) -- eases the weight to 1 after 1 second
  109.    
  110.     Easing the speed (let's assume its current speed is 0):
  111.     anim:EaseSpeed(-1, TweenInfo.new(1.5, Enum.EasingStyle.Quint, Enum.EasingStyle.Out)) -- eases the speed to -1 after 1.5 seconds
  112.  
  113.  
  114. Once again, thanks for using my module! PM me on the DevForum @rek_kie or reply to the DevForum post if there are any bugs. Enjoy!
  115.  
  116. ]]--
  117.  
  118. local easimation = {}
  119.  
  120. local ts = game:GetService("TweenService")
  121.  
  122. function easimation.New(anim, humanoid)
  123.    
  124.     if not humanoid or not humanoid:IsA("Humanoid") then
  125.         warn("A humanoid has not been supplied to load the animation into.")
  126.         return
  127.     end
  128.    
  129.     if not anim or not anim:IsA("Animation") then
  130.         warn("An animation has not been supplied. Please supply an animation.")
  131.         return
  132.     end
  133.    
  134.     print("Created")
  135.     --events
  136.    
  137.     local PlayEvent = Instance.new("BindableEvent")
  138.     local StoppedEvent = Instance.new("BindableEvent")
  139.     local PlayCompletedEvent = Instance.new("BindableEvent")
  140.     local StopCompletedEvent = Instance.new("BindableEvent")
  141.     local WeightEaseStartedEvent = Instance.new("BindableEvent")
  142.     local WeightEaseCompletedEvent = Instance.new("BindableEvent")
  143.     local SpeedEaseStartedEvent = Instance.new("BindableEvent")
  144.     local SpeedEaseCompletedEvent = Instance.new("BindableEvent")
  145.     local EasedEvent = Instance.new("BindableEvent")
  146.    
  147.     --object
  148.    
  149.     local easimationObject = {
  150.         ["animTrack"] = humanoid:LoadAnimation(anim),
  151.         ["Played"] = PlayEvent.Event,
  152.         ["Stopped"] = StoppedEvent.Event,
  153.         ["PlayCompleted"] = PlayCompletedEvent.Event,
  154.         ["StopCompleted"] = StopCompletedEvent.Event,
  155.         ["WeightEaseStarted"] = WeightEaseStartedEvent.Event,
  156.         ["WeightEaseCompleted"] = WeightEaseCompletedEvent.Event,
  157.         ["SpeedEaseStarted"] = SpeedEaseStartedEvent.Event,
  158.         ["SpeedEaseCompleted"] = SpeedEaseCompletedEvent.Event,
  159.         ["Eased"] = EasedEvent.Event     
  160.     }
  161.    
  162.     function easimationObject:Play(finalWeight, speed, tweenInfo)
  163.         speed = speed or 1
  164.        
  165.         local weightForTween
  166.    
  167.         if finalWeight > 1 then
  168.             weightForTween = 1
  169.         else
  170.             weightForTween = finalWeight
  171.         end
  172.        
  173.         easimationObject["animTrack"]:Play(0, .000001, speed) -- "Play" it at that weight so that it counts as Playing by ROBLOX, but have no visible effect
  174.         PlayEvent:Fire()
  175.        
  176.         local number = Instance.new("NumberValue")
  177.         number.Value = easimationObject["animTrack"].WeightCurrent
  178.        
  179.         number:GetPropertyChangedSignal("Value"):Connect(function()
  180.             easimationObject["animTrack"]:AdjustWeight(number.Value, 0)
  181.             EasedEvent:Fire()
  182.         end)
  183.        
  184.         local tween = ts:Create(number, tweenInfo, {Value = weightForTween})
  185.         tween:Play()
  186.        
  187.         tween.Completed:Connect(function()         
  188.             number:Destroy()
  189.             tween:Destroy()
  190.             if easimationObject["animTrack"].WeightCurrent ~= finalWeight then
  191.                 easimationObject["animTrack"]:AdjustWeight(finalWeight, 0)
  192.             end
  193.             weightForTween = nil
  194.             PlayCompletedEvent:Fire()
  195.         end)
  196.     end
  197.    
  198.     function easimationObject:Stop(tweenInfo)
  199.         local number = Instance.new("NumberValue")
  200.         number.Value = easimationObject["animTrack"].WeightCurrent
  201.        
  202.         easimationObject["animTrack"]:Stop(100000000000) -- So that when you call :Stop() with this, .IsPlaying will be set to false immediately
  203.         StoppedEvent:Fire()                             -- without visually interrupting the tween, so it functions like stopping an animation with
  204.                                      -- normal fade time
  205.        
  206.         number:GetPropertyChangedSignal("Value"):Connect(function()
  207.             easimationObject["animTrack"]:AdjustWeight(number.Value, 0)
  208.             EasedEvent:Fire()
  209.         end)
  210.        
  211.         local tween = ts:Create(number, tweenInfo, {Value = 0.000001})
  212.         tween:Play()
  213.        
  214.         tween.Completed:Connect(function()
  215.             number:Destroy()
  216.             tween:Destroy()
  217.             easimationObject["animTrack"]:Stop(0)
  218.             StopCompletedEvent:Fire()
  219.         end)
  220.     end
  221.    
  222.     function easimationObject:EaseWeight(finalWeight, tweenInfo)
  223.         local weightForTween
  224.    
  225.         if finalWeight > 1 then
  226.             weightForTween = 1
  227.         else
  228.             weightForTween = finalWeight
  229.         end
  230.        
  231.         local number = Instance.new("NumberValue")
  232.         number.Value = easimationObject["animTrack"].WeightCurrent
  233.        
  234.         number:GetPropertyChangedSignal("Value"):Connect(function()
  235.             easimationObject["animTrack"]:AdjustWeight(number.Value, 0)
  236.             EasedEvent:Fire()
  237.         end)
  238.        
  239.         local tween = ts:Create(number, tweenInfo, {Value = weightForTween})
  240.         tween:Play()
  241.         WeightEaseStartedEvent:Fire()
  242.        
  243.         tween.Completed:Connect(function()
  244.             number:Destroy()
  245.             tween:Destroy()
  246.             if easimationObject["animTrack"].WeightCurrent ~= finalWeight then
  247.                 easimationObject["animTrack"]:AdjustWeight(finalWeight, 0)
  248.             end
  249.             weightForTween = nil
  250.             WeightEaseCompletedEvent:Fire()
  251.         end)
  252.     end
  253.    
  254.     function easimationObject:EaseSpeed(finalSpeed, tweenInfo)
  255.         local speedForTween = finalSpeed
  256.        
  257.         local number = Instance.new("NumberValue")
  258.         number.Value = easimationObject["animTrack"].Speed
  259.        
  260.         number:GetPropertyChangedSignal("Value"):Connect(function()
  261.             easimationObject["animTrack"]:AdjustSpeed(number.Value)
  262.             EasedEvent:Fire()
  263.         end)
  264.        
  265.         local tween = ts:Create(number, tweenInfo, {Value = speedForTween})
  266.         tween:Play()
  267.         SpeedEaseStartedEvent:Fire()
  268.        
  269.         tween.Completed:Connect(function()
  270.             number:Destroy()
  271.             tween:Destroy()
  272.             if easimationObject["animTrack"].WeightCurrent ~= finalSpeed then
  273.                 easimationObject["animTrack"]:AdjustSpeed(finalSpeed, 0)
  274.             end
  275.             speedForTween = nil
  276.             SpeedEaseCompletedEvent:Fire()
  277.         end)
  278.     end
  279.    
  280.     return easimationObject
  281. end
  282.  
  283. return easimation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement