Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.15 KB | None | 0 0
  1. local runService = game:GetService("RunService")
  2. local heartbeat = runService.Heartbeat
  3.  
  4.  
  5.  
  6.  
  7.  
  8. local ikModule = require(script:WaitForChild("IK"))
  9. local ragdollModule = require(script:WaitForChild("Ragdoll"))
  10.  
  11.  
  12.  
  13.  
  14.  
  15. local abs = math.abs
  16. local min = math.min
  17. local max = math.max
  18. local clamp = math.clamp
  19.  
  20. local cf = CFrame.new
  21. local cfa = CFrame.Angles
  22. local v3n = Vector3.new
  23.  
  24. local table_insert = table.insert
  25. local table_remove = table.remove
  26.  
  27. local profilebegin = debug.profilebegin
  28. local profileend = debug.profileend
  29.  
  30. local originV3 = v3n(0, 0, 0)
  31. local originCF = cf(0, 0, 0)
  32.  
  33.  
  34.  
  35.  
  36.  
  37. local animator = {}
  38. local animators = {}
  39. local animatorMeta = {
  40.     __index = animator,
  41.     __tostring = function(self)
  42.         if self.Destroyed then
  43.             return "[Destroyed Animator]"
  44.         else
  45.             return "[Animator | "..tostring(self.Character).."]"
  46.         end
  47.     end,
  48. }
  49. local animatorParts = {
  50.     -- Root part
  51.     "HumanoidRootPart",
  52.     -- Head & torso parts
  53.     "Head",
  54.     "UpperTorso",
  55.     "LowerTorso",
  56.     -- Arm parts
  57.     "LeftUpperArm",
  58.     "LeftLowerArm",
  59.     "Left_Hand",
  60.     "RightUpperArm",
  61.     "RightLowerArm",
  62.     "Right_Hand",
  63.     -- Leg parts
  64.     "LeftUpperLeg",
  65.     "LeftLowerLeg",
  66.     "Left_Foot",
  67.     "RightUpperLeg",
  68.     "RightLowerLeg",
  69.     "Right_Foot",
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. local destroyAnimator
  92. local createAnimator
  93. local isAnimator
  94. do -- Animator class
  95.     do -- Animation object functions
  96.         function animator:AddAnimation(animationType, animationName, animationData)
  97.             assert(type(animationType) == "string", "animationType must be a string")
  98.             assert(type(animationName) == "string", "animationName must be a string")
  99.             assert(type(animationData) == "table", "animationData must be a table")
  100.             -- Get the animation id from the data table
  101.             local contentId = "" do
  102.                 if type(animationData.Id) == "number" then
  103.                     contentId = "rbxassetid://" .. animationData.Id
  104.                 elseif type(animationData.Id) == "string" then
  105.                     contentId = animationData.Id
  106.                 else
  107.                     error("animationData.Id must be a number or string")
  108.                 end
  109.             end
  110.             do -- Add the animation track to the list of animation tracks
  111.                 -- Create a new animation track
  112.                 local controller = self.Controller
  113.                 local newAnimation = Instance.new("Animation")
  114.                 newAnimation.AnimationId = contentId
  115.                 newAnimation.Parent = controller
  116.                 local animationTrack = controller:LoadAnimation(newAnimation)
  117.                 -- Add it into the table
  118.                 local animationTracks = self.AnimationTracks
  119.                 local typeAnimations = animationTracks
  120.                 if type(typeAnimations) ~= "table" then
  121.                     typeAnimations = {}
  122.                     animationTracks[animationType] = typeAnimations
  123.                 end
  124.                 typeAnimations[animationName] = animationTrack
  125.             end
  126.             do -- Add the animation data to the list of animation datas
  127.                 local animationDatas = self.AnimationDatas
  128.                 local typeAnimations = animationDatas
  129.                 if type(typeAnimations) ~= "table" then
  130.                     typeAnimations = {}
  131.                     animationDatas[animationType] = typeAnimations
  132.                 end
  133.                 typeAnimations[animationName] = animationData
  134.             end
  135.         end
  136.        
  137.         function animator:AddAnimations(animationTypes)
  138.             assert(type(animationTypes) == "table", "animationTypes must be a table")
  139.             for animationType, animationNames in pairs(animationTypes) do
  140.                 for animationName, animationData in pairs(animationNames) do
  141.                     self:AddAnimation(animationType, animationName, animationData)
  142.                 end
  143.             end
  144.         end
  145.        
  146.         function animator:StopAnimation(animationType, animationName, ...)
  147.             assert(type(animationType) == "string", "animationType must be a string")
  148.             assert(type(animationName) == "string", "animationName must be a string")
  149.             local animationTracks = self.AnimationTracks[animationType]
  150.             local animationTrack = animationTracks and animationTracks[animationName]
  151.             assert(animationTrack, "animationTrack is missing for animation: "..animationType..", "..animationName)
  152.             local animationDatas = self.AnimationDatas[animationType]
  153.             local animationData = animationDatas and animationDatas[animationName]
  154.             assert(animationData, "animationData is missing for animation: "..animationType..", "..animationName)
  155.             if animationTrack.IsPlaying then
  156.                 animationTrack:Stop(...)
  157.             end
  158.         end
  159.        
  160.         function animator:StopAnimations(animationType, ...)
  161.             assert(type(animationType) == "string", "animationType must be a string")
  162.             local playingAnimations = self.PlayingAnimationTracks[animationType]
  163.             if playingAnimations and next(playingAnimations) then
  164.                 for animationName, animationTrack in pairs(playingAnimations) do
  165.                     if animationTrack.IsPlaying then
  166.                         animationTrack:Stop(...)
  167.                     end
  168.                 end
  169.             end
  170.         end
  171.        
  172.         function animator:PlayAnimation(animationType, animationName, stopPrevious, ...)
  173.             if stopPrevious == nil then stopPrevious = false end
  174.             assert(type(animationType) == "string", "animationType must be a string")
  175.             assert(type(animationName) == "string", "animationName must be a string")
  176.             assert(type(stopPrevious) == "boolean", "stopPrevious must be a boolean")
  177.             local animationTracks = self.AnimationTracks[animationType]
  178.             local animationTrack = animationTracks and animationTracks[animationName]
  179.             assert(animationTrack, "animationTrack is missing for animation: "..animationType..", "..animationName)
  180.             local animationDatas = self.AnimationDatas[animationType]
  181.             local animationData = animationDatas and animationDatas[animationName]
  182.             assert(animationData, "animationData is missing for animation: "..animationType..", "..animationName)
  183.             if stopPrevious then
  184.                 self:StopAnimations(animationType, ...)
  185.             end
  186.             if animationTrack then
  187.                 animationTrack:Play(...)
  188.             end
  189.         end
  190.     end
  191.    
  192.    
  193.    
  194.    
  195.    
  196.     do -- Update functions
  197.         local function updateState(animator, delta)
  198.             local rootPart = animator.RootPart
  199.             local thisCFrame = rootPart.CFrame
  200.             local lastCFrame = animator.CFrame
  201.            
  202.             local inverseDelta = 1 / delta
  203.             local objectDiff = lastCFrame:toObjectSpace(thisCFrame).p
  204.             local worldDiff = thisCFrame.p - lastCFrame.p
  205.            
  206.             animator.ObjectDirection = objectDiff.Unit
  207.             animator.ObjectVelocity = objectDiff * inverseDelta
  208.             animator.WorldDirection = worldDiff.Unit
  209.             animator.WorldVelocity = worldDiff * inverseDelta
  210.             animator.CFrame = thisCFrame
  211.         end
  212.        
  213.         local function updateAnimations(animator, delta)
  214.            
  215.         end
  216.        
  217.         local function updateIK(animator, delta)
  218.             profilebegin("UpdateIKLegs")
  219.            
  220.             profileend()
  221.            
  222.             profilebegin("UpdateIKArms")
  223.            
  224.             profileend()
  225.            
  226.             profilebegin("UpdateIKLook")
  227.            
  228.             profileend()
  229.         end
  230.        
  231.         function animator:Update()
  232.             if self.Destroyed then
  233.                 return
  234.             end
  235.            
  236.             local thisUpdate = tick()
  237.             local lastUpdate = self.LastUpdate or 0
  238.             local delta = clamp(thisUpdate - lastUpdate, 0, 1)
  239.             self.LastUpdate = thisUpdate
  240.            
  241.             profilebegin("UpdateState")
  242.             updateState(self, delta)
  243.             profileend()
  244.            
  245.             profilebegin("UpdateAnimations")
  246.             updateAnimations(self, delta)
  247.             profileend()
  248.            
  249.             profilebegin("UpdateIK")
  250.             updateIK(self, delta)
  251.             profileend()
  252.         end
  253.     end
  254.    
  255.    
  256.    
  257.    
  258.    
  259.     do -- Auto update functions
  260.         local autoUpdatedAnimators = {}
  261.        
  262.         function animator:AddToAutoUpdate()
  263.             if not self.AddedToAutoUpdate then
  264.                 self.AddedToAutoUpdate = true
  265.                 table_insert(autoUpdatedAnimators, self)
  266.             end
  267.         end
  268.        
  269.         function animator:RemoveFromAutoUpdate()
  270.             if self.AddedToAutoUpdate then
  271.                 self.AddedToAutoUpdate = nil
  272.                 for i, animator in pairs(autoUpdatedAnimators) do
  273.                     if animator == self then
  274.                         local num = #autoUpdatedAnimators
  275.                         autoUpdatedAnimators[i] = autoUpdatedAnimators[num]
  276.                         autoUpdatedAnimators[num] = nil
  277.                         break
  278.                     end
  279.                 end
  280.             end
  281.         end
  282.        
  283.         heartbeat:Connect(function()
  284.             profilebegin("UpdateAnimators")
  285.             for _, animator in pairs(autoUpdatedAnimators) do
  286.                 animator:Update()
  287.             end
  288.             profileend()
  289.         end)
  290.     end
  291.    
  292.    
  293.    
  294.    
  295.    
  296.     do -- Other functions
  297.         function animator:Destroy()
  298.             if not self.Destroyed then
  299.                 self.Destroyed = true
  300.                 -- Remove animator from auto updating
  301.                 self:RemoveFromAutoUpdate()
  302.                 -- Remove character from animators hash
  303.                 local character = self.Character
  304.                 if animators[character] then
  305.                     animators[character] = nil
  306.                 end
  307.                 -- Disconnect any connections
  308.                 local connections = self.Connections
  309.                 if type(connections) == "table" then
  310.                     for _, connection in pairs(connections) do
  311.                         if connection.Connected then
  312.                             connection:Disconnect()
  313.                         end
  314.                     end
  315.                 end
  316.                 -- Clean up memory
  317.                 self.Connections = nil
  318.                 self.Character = nil
  319.                 self.RootPart = nil
  320.                 self.CFrame = nil
  321.                 self.ObjectDirection = nil
  322.                 self.ObjectVelocity = nil
  323.                 self.WorldDirection = nil
  324.                 self.WorldVelocity = nil
  325.                 self.PlayingAnimationTracks = nil
  326.                 self.AnimationTracks = nil
  327.                 self.AnimationDatas = nil
  328.             end
  329.         end
  330.        
  331.         function animator:Connect()
  332.             if not self.Connected then
  333.                 self.Connected = true
  334.                 -- Variables used for connecting
  335.                 local character = self.Character
  336.                 local rootPart = self.RootPart
  337.                 -- Gather joint & attachment data
  338.                 local joints = {}
  339.                 local jointC0s = {}
  340.                 local jointC1s = {}
  341.                 local jointParts = {}
  342.                 local jointPart0s = {}
  343.                 local jointPart1s = {}
  344.                 local attachments = {}
  345.                 for _, descendant in pairs(character:GetDescendants()) do
  346.                     if descendant:IsA("Motor") then
  347.                         local jointName = descendant.Name
  348.                         local part0 = descendant.Part0
  349.                         local part1 = descendant.Part1
  350.                         joints[jointName] = descendant
  351.                         jointC0s[jointName] = descendant.C0
  352.                         jointC1s[jointName] = descendant.C1
  353.                         jointPart0s[jointName] = part0
  354.                         jointPart1s[jointName] = part1
  355.                         if not jointParts[part0.Name] then
  356.                             jointParts[part0.Name] = part0
  357.                         end
  358.                         if not jointParts[part1.Name] then
  359.                             jointParts[part1.Name] = part1
  360.                         end
  361.                     elseif descendant:IsA("Attachment") then
  362.                         local attachmentName = descendant.Name
  363.                         if not attachmentName:find("Rig") then
  364.                             attachments[attachmentName] = descendant
  365.                         end
  366.                     end
  367.                 end
  368.                 self.Joints = joints
  369.                 self.JointC0s = jointC0s
  370.                 self.JointC1s = jointC1s
  371.                 self.JointParts = jointParts
  372.                 self.JointPart0s = jointPart0s
  373.                 self.JointPart1s = jointPart1s
  374.                 self.Attachments = attachments
  375.                 -- Automatically destroy animator if character or root part is destroyed
  376.                 local connections = {}
  377.                 local function checkAutoDestroy()
  378.                     if character.Parent == nil
  379.                     or rootPart.Parent == nil then
  380.                         self:Destroy()
  381.                     end
  382.                 end
  383.                 table_insert(connections, character.AncestryChanged:Connect(checkAutoDestroy))
  384.                 table_insert(connections, character:GetPropertyChangedSignal("Parent"):Connect(checkAutoDestroy))
  385.                 table_insert(connections, rootPart:GetPropertyChangedSignal("Parent"):Connect(checkAutoDestroy))
  386.                 self.Connections = connections
  387.                 -- Set any current animation controller, or create one if one wasn't found
  388.                 local controller
  389.                 local animationController = character:FindFirstChildOfClass("AnimationController")
  390.                 local humanoid = character:FindFirstChildOfClass("Humanoid")
  391.                 if humanoid then
  392.                     controller = humanoid
  393.                 elseif animationController then
  394.                     controller = animationController
  395.                 else
  396.                     controller = Instance.new("AnimationController")
  397.                     controller.Parent = character
  398.                 end
  399.                 self.Controller = controller
  400.                 -- Add the animator to the animators hash
  401.                 animators[character] = self
  402.             end
  403.         end
  404.     end
  405.    
  406.    
  407.    
  408.    
  409.    
  410.     do -- Create / destroy / typecheck functions (public)
  411.         function destroyAnimator(character)
  412.             assert(character ~= nil, "character must not be nil")
  413.             local animator = animators[character]
  414.             if animator and not animator.Destroyed then
  415.                 animator:Destroy()
  416.                 return true
  417.             end
  418.             return false
  419.         end
  420.        
  421.         function createAnimator(character)
  422.             assert(character ~= nil,                "character must not be nil")
  423.             assert(typeof(character) == "Instance", "character must be an Instance")
  424.             assert(character:IsA("Model"),          "character must be a Model")
  425.             assert(character.PrimaryPart,           "character must have a PrimaryPart")
  426.             if animators[character] then
  427.                 return animators[character]
  428.             end
  429.             local newAnimator = setmetatable({}, animatorMeta)
  430.             newAnimator.RootPart = character.PrimaryPart
  431.             newAnimator.Character = character
  432.             newAnimator.CFrame = originCF
  433.             newAnimator.ObjectDirection = originV3
  434.             newAnimator.ObjectVelocity = originV3
  435.             newAnimator.WorldDirection = originV3
  436.             newAnimator.WorldVelocity = originV3
  437.             newAnimator.PlayingAnimationTracks = {}
  438.             newAnimator.AnimationTracks = {}
  439.             newAnimator.AnimationDatas = {}
  440.             newAnimator:Connect()
  441.             return newAnimator
  442.         end
  443.        
  444.         function isAnimator(object)
  445.             if type(object) == "table" then
  446.                 if getmetatable(object) == animatorMeta then
  447.                     return true
  448.                 end
  449.             end
  450.             return false
  451.         end
  452.     end
  453. end
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471.  
  472.  
  473.  
  474. local module = setmetatable({}, {
  475.     __call = function(self, ...)
  476.         return createAnimator(...)
  477.     end
  478. })
  479.  
  480. function module:Destroy(...)
  481.     return destroyAnimator(...)
  482. end
  483.  
  484. function module:Create(...)
  485.     return createAnimator(...)
  486. end
  487.  
  488. function module:IsAnimator(...)
  489.     return isAnimator(...)
  490. end
  491.  
  492. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement