Guest User

Animation Code

a guest
Jun 2nd, 2017
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.71 KB | None | 0 0
  1. local function waitForChild(parent, childName)
  2.     local child = parent:findFirstChild(childName)
  3.     if child then return child end
  4.     while true do
  5.         child = parent.ChildAdded:wait()
  6.         if child.Name==childName then return child end
  7.     end
  8. end
  9.  
  10. local Figure = script.Parent
  11. local Humanoid = waitForChild(Figure, "Humanoid")
  12. local pose = "Standing"
  13.  
  14. local currentAnim = ""
  15. local currentAnimInstance = nil
  16. local currentAnimTrack = nil
  17. local currentAnimKeyframeHandler = nil
  18. local currentAnimSpeed = 1.0
  19.  
  20. local runAnimTrack = nil
  21. local runAnimKeyframeHandler = nil
  22.  
  23. local animTable = {}
  24. local animNames = {
  25.     idle =  {  
  26.                 { id = "http://www.roblox.com/asset/?id=507766666", weight = 1 },
  27.                 { id = "http://www.roblox.com/asset/?id=507766951", weight = 1 },
  28.                 { id = "http://www.roblox.com/asset/?id=507766388", weight = 9 }
  29.             },
  30.     walk =  {  
  31.                 { id = "http://www.roblox.com/asset/?id=507777826", weight = 10 }
  32.             },
  33.     run =   {
  34.                 { id = "http://www.roblox.com/asset/?id=507767714", weight = 10 }
  35.             },
  36.     swim =  {
  37.                 { id = "http://www.roblox.com/asset/?id=507784897", weight = 10 }
  38.             },
  39.     swimidle =  {
  40.                 { id = "http://www.roblox.com/asset/?id=507785072", weight = 10 }
  41.             },
  42.     jump =  {
  43.                 { id = "http://www.roblox.com/asset/?id=507765000", weight = 10 }
  44.             },
  45.     fall =  {
  46.                 { id = "http://www.roblox.com/asset/?id=507767968", weight = 10 }
  47.             },
  48.     climb = {
  49.                 { id = "http://www.roblox.com/asset/?id=507765644", weight = 10 }
  50.             },
  51.     sit =   {
  52.                 { id = "http://www.roblox.com/asset/?id=507768133", weight = 10 }
  53.             }, 
  54.     toolnone = {
  55.                 { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  56.             },
  57.     toolslash = {
  58.                 { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  59. --              { id = "slash.xml", weight = 10 }
  60.             },
  61.     toollunge = {
  62.                 { id = "http://www.roblox.com/asset/?id=507768375", weight = 10 }
  63.             },
  64.     wave = {
  65.                 { id = "http://www.roblox.com/asset/?id=507770239", weight = 10 }
  66.             },
  67.     point = {
  68.                 { id = "http://www.roblox.com/asset/?id=507770453", weight = 10 }
  69.             },
  70.     dance = {
  71.                 { id = "http://www.roblox.com/asset/?id=507771019", weight = 10 },
  72.                 { id = "http://www.roblox.com/asset/?id=507771955", weight = 10 },
  73.                 { id = "http://www.roblox.com/asset/?id=507772104", weight = 10 }
  74.             },
  75.     dance2 = {
  76.                 { id = "http://www.roblox.com/asset/?id=507776043", weight = 10 },
  77.                 { id = "http://www.roblox.com/asset/?id=507776720", weight = 10 },
  78.                 { id = "http://www.roblox.com/asset/?id=507776879", weight = 10 }
  79.             },
  80.     dance3 = {
  81.                 { id = "http://www.roblox.com/asset/?id=507777268", weight = 10 },
  82.                 { id = "http://www.roblox.com/asset/?id=507777451", weight = 10 },
  83.                 { id = "http://www.roblox.com/asset/?id=507777623", weight = 10 }
  84.             },
  85.     laugh = {
  86.                 { id = "http://www.roblox.com/asset/?id=507770818", weight = 10 }
  87.             },
  88.     cheer = {
  89.                 { id = "http://www.roblox.com/asset/?id=507770677", weight = 10 }
  90.             },
  91. }
  92.  
  93. -- Existance in this list signifies that it is an emote, the value indicates if it is a looping emote
  94. local emoteNames = { wave = false, point = false, dance = true, dance2 = true, dance3 = true, laugh = false, cheer = false}
  95.  
  96. math.randomseed(tick())
  97.  
  98. function configureAnimationSet(name, fileList)
  99.     if (animTable[name] ~= nil) then
  100.         for _, connection in pairs(animTable[name].connections) do
  101.             connection:disconnect()
  102.         end
  103.     end
  104.     animTable[name] = {}
  105.     animTable[name].count = 0
  106.     animTable[name].totalWeight = 0
  107.     animTable[name].connections = {}
  108.  
  109.     local allowCustomAnimations = true
  110.     local AllowDisableCustomAnimsUserFlag = true
  111.  
  112.     local success, msg = pcall(function()
  113.         AllowDisableCustomAnimsUserFlag = UserSettings():IsUserFeatureEnabled("UserAllowDisableCustomAnims")
  114.     end)
  115.  
  116.     if (AllowDisableCustomAnimsUserFlag) then
  117.         local ps = game:GetService("StarterPlayer"):FindFirstChild("PlayerSettings")
  118.         if (ps ~= nil) then
  119.             allowCustomAnimations = not require(ps).UseDefaultAnimations
  120.         end
  121.     end
  122.  
  123.     -- check for config values
  124.     local config = script:FindFirstChild(name)
  125.     if (allowCustomAnimations and config ~= nil) then
  126.         table.insert(animTable[name].connections, config.ChildAdded:connect(function(child) configureAnimationSet(name, fileList) end))
  127.         table.insert(animTable[name].connections, config.ChildRemoved:connect(function(child) configureAnimationSet(name, fileList) end))
  128.         local idx = 1
  129.         for _, childPart in pairs(config:GetChildren()) do
  130.             if (childPart:IsA("Animation")) then
  131.                 table.insert(animTable[name].connections, childPart.Changed:connect(function(property) configureAnimationSet(name, fileList) end))
  132.                 animTable[name][idx] = {}
  133.                 animTable[name][idx].anim = childPart
  134.                 local weightObject = childPart:FindFirstChild("Weight")
  135.                 if (weightObject == nil) then
  136.                     animTable[name][idx].weight = 1
  137.                 else
  138.                     animTable[name][idx].weight = weightObject.Value
  139.                 end
  140.                 animTable[name].count = animTable[name].count + 1
  141.                 animTable[name].totalWeight = animTable[name].totalWeight + animTable[name][idx].weight
  142. --              print(name .. " [" .. idx .. "] " .. animTable[name][idx].anim.AnimationId .. " (" .. animTable[name][idx].weight .. ")")
  143.                 idx = idx + 1
  144.             end
  145.         end
  146.     end
  147.  
  148.     -- fallback to defaults
  149.     if (animTable[name].count <= 0) then
  150.         for idx, anim in pairs(fileList) do
  151.             animTable[name][idx] = {}
  152.             animTable[name][idx].anim = Instance.new("Animation")
  153.             animTable[name][idx].anim.Name = name
  154.             animTable[name][idx].anim.AnimationId = anim.id
  155.             animTable[name][idx].weight = anim.weight
  156.             animTable[name].count = animTable[name].count + 1
  157.             animTable[name].totalWeight = animTable[name].totalWeight + anim.weight
  158. --          print(name .. " [" .. idx .. "] " .. anim.id .. " (" .. anim.weight .. ")")
  159.         end
  160.     end
  161. end
  162.  
  163. -- Setup animation objects
  164. function scriptChildModified(child)
  165.     local fileList = animNames[child.Name]
  166.     if (fileList ~= nil) then
  167.         configureAnimationSet(child.Name, fileList)
  168.     end
  169. end
  170.  
  171. script.ChildAdded:connect(scriptChildModified)
  172. script.ChildRemoved:connect(scriptChildModified)
  173.  
  174.  
  175. for name, fileList in pairs(animNames) do
  176.     configureAnimationSet(name, fileList)
  177. end
  178.  
  179. -- ANIMATION
  180.  
  181. -- declarations
  182. local toolAnim = "None"
  183. local toolAnimTime = 0
  184.  
  185. local jumpAnimTime = 0
  186. local jumpAnimDuration = 0.31
  187.  
  188. local toolTransitionTime = 0.1
  189. local fallTransitionTime = 0.2
  190.  
  191. -- functions
  192.  
  193. function stopAllAnimations()
  194.     local oldAnim = currentAnim
  195.  
  196.     -- return to idle if finishing an emote
  197.     if (emoteNames[oldAnim] ~= nil and emoteNames[oldAnim] == false) then
  198.         oldAnim = "idle"
  199.     end
  200.  
  201.     currentAnim = ""
  202.     currentAnimInstance = nil
  203.     if (currentAnimKeyframeHandler ~= nil) then
  204.         currentAnimKeyframeHandler:disconnect()
  205.     end
  206.  
  207.     if (currentAnimTrack ~= nil) then
  208.         currentAnimTrack:Stop()
  209.         currentAnimTrack:Destroy()
  210.         currentAnimTrack = nil
  211.     end
  212.  
  213.     -- clean up walk if there is one
  214.     if (runAnimKeyframeHandler ~= nil) then
  215.         runAnimKeyframeHandler:disconnect()
  216.     end
  217.    
  218.     if (runAnimTrack ~= nil) then
  219.         runAnimTrack:Stop()
  220.         runAnimTrack:Destroy()
  221.         runAnimTrack = nil
  222.     end
  223.    
  224.     return oldAnim
  225. end
  226.  
  227. local smallButNotZero = 0.0001
  228. function setRunSpeed(speed)
  229.  
  230.     if speed < 0.33 then
  231.         currentAnimTrack:AdjustWeight(1.0)     
  232.         runAnimTrack:AdjustWeight(smallButNotZero)
  233.     elseif speed < 0.66 then
  234.         local weight = ((speed - 0.33) / 0.33)
  235.         currentAnimTrack:AdjustWeight(1.0 - weight + smallButNotZero)
  236.         runAnimTrack:AdjustWeight(weight + smallButNotZero)
  237.     else
  238.         currentAnimTrack:AdjustWeight(smallButNotZero)
  239.         runAnimTrack:AdjustWeight(1.0)
  240.     end
  241.    
  242.     local speedScaled = speed * 1.25
  243.     runAnimTrack:AdjustSpeed(speedScaled)
  244.     currentAnimTrack:AdjustSpeed(speedScaled)
  245. end
  246.  
  247.  
  248. function setAnimationSpeed(speed)
  249.     if speed ~= currentAnimSpeed then
  250.         currentAnimSpeed = speed
  251.         if currentAnim == "walk" then
  252.             setRunSpeed(speed)
  253.         else
  254.             currentAnimTrack:AdjustSpeed(currentAnimSpeed)
  255.         end
  256.     end
  257. end
  258.  
  259. function keyFrameReachedFunc(frameName)
  260. --  print("CurrentAnim ", currentAnim, " ", frameName)
  261.     if (frameName == "End") then
  262.         if currentAnim == "walk" then
  263.             runAnimTrack.TimePosition = 0.0
  264.             currentAnimTrack.TimePosition = 0.0
  265.         else
  266.     --      print("Keyframe : ".. frameName)
  267.    
  268.             local repeatAnim = currentAnim
  269.             -- return to idle if finishing an emote
  270.             if (emoteNames[repeatAnim] ~= nil and emoteNames[repeatAnim] == false) then
  271.                 repeatAnim = "idle"
  272.             end
  273.            
  274.             local animSpeed = currentAnimSpeed
  275.             playAnimation(repeatAnim, 0.15, Humanoid)
  276.             setAnimationSpeed(animSpeed)
  277.         end
  278.     end
  279. end
  280.  
  281. function rollAnimation(animName)
  282.     local roll = math.random(1, animTable[animName].totalWeight)
  283.     local origRoll = roll
  284.     local idx = 1
  285.     while (roll > animTable[animName][idx].weight) do
  286.         roll = roll - animTable[animName][idx].weight
  287.         idx = idx + 1
  288.     end
  289.     return idx
  290. end
  291.  
  292. function playAnimation(animName, transitionTime, humanoid)
  293.        
  294.     local idx = rollAnimation(animName)
  295.    
  296. --  print(animName .. " " .. idx .. " [" .. origRoll .. "]")
  297.    
  298.     local anim = animTable[animName][idx].anim
  299.  
  300.     -- switch animation    
  301.     if (anim ~= currentAnimInstance) then
  302.        
  303.         if (currentAnimTrack ~= nil) then
  304.             currentAnimTrack:Stop(transitionTime)
  305.             currentAnimTrack:Destroy()
  306.         end
  307.  
  308.         if (runAnimTrack ~= nil) then
  309.             runAnimTrack:Stop(transitionTime)
  310.             runAnimTrack:Destroy()
  311.         end
  312.  
  313.         currentAnimSpeed = 1.0
  314.    
  315.         -- load it to the humanoid; get AnimationTrack
  316.         currentAnimTrack = humanoid:LoadAnimation(anim)
  317.          
  318.         -- play the animation
  319.         currentAnimTrack:Play(transitionTime)
  320.         currentAnim = animName
  321.         currentAnimInstance = anim
  322.  
  323.         -- set up keyframe name triggers
  324.         if (currentAnimKeyframeHandler ~= nil) then
  325.             currentAnimKeyframeHandler:disconnect()
  326.         end
  327.         currentAnimKeyframeHandler = currentAnimTrack.KeyframeReached:connect(keyFrameReachedFunc)
  328.        
  329.         -- check to see if we need to blend a walk/run animation
  330.         if animName == "walk" then
  331.             local runAnimName = "run"
  332.             local runIdx = rollAnimation(runAnimName)
  333.  
  334.             runAnimTrack = humanoid:LoadAnimation(animTable[runAnimName][runIdx].anim)       
  335.             runAnimTrack:Play(transitionTime)      
  336.            
  337.             if (runAnimKeyframeHandler ~= nil) then
  338.                 runAnimKeyframeHandler:disconnect()
  339.             end
  340.             runAnimKeyframeHandler = runAnimTrack.KeyframeReached:connect(keyFrameReachedFunc) 
  341.         end
  342.     end
  343.  
  344. end
  345.  
  346. -------------------------------------------------------------------------------------------
  347. -------------------------------------------------------------------------------------------
  348.  
  349. local toolAnimName = ""
  350. local toolAnimTrack = nil
  351. local toolAnimInstance = nil
  352. local currentToolAnimKeyframeHandler = nil
  353.  
  354. function toolKeyFrameReachedFunc(frameName)
  355.     if (frameName == "End") then
  356. --      print("Keyframe : ".. frameName)   
  357.         playToolAnimation(toolAnimName, 0.0, Humanoid)
  358.     end
  359. end
  360.  
  361.  
  362. function playToolAnimation(animName, transitionTime, humanoid)   
  363.        
  364.         local idx = rollAnimation(animName)
  365. --      print(animName .. " * " .. idx .. " [" .. origRoll .. "]")
  366.         local anim = animTable[animName][idx].anim
  367.  
  368.         if (toolAnimInstance ~= anim) then
  369.            
  370.             if (toolAnimTrack ~= nil) then
  371.                 toolAnimTrack:Stop()
  372.                 toolAnimTrack:Destroy()
  373.                 transitionTime = 0
  374.             end
  375.                    
  376.             -- load it to the humanoid; get AnimationTrack
  377.             toolAnimTrack = humanoid:LoadAnimation(anim)
  378.              
  379.             -- play the animation
  380.             toolAnimTrack:Play(transitionTime)
  381.             toolAnimName = animName
  382.             toolAnimInstance = anim
  383.  
  384.             currentToolAnimKeyframeHandler = toolAnimTrack.KeyframeReached:connect(toolKeyFrameReachedFunc)
  385.         end
  386. end
  387.  
  388. function stopToolAnimations()
  389.     local oldAnim = toolAnimName
  390.  
  391.     if (currentToolAnimKeyframeHandler ~= nil) then
  392.         currentToolAnimKeyframeHandler:disconnect()
  393.     end
  394.  
  395.     toolAnimName = ""
  396.     toolAnimInstance = nil
  397.     if (toolAnimTrack ~= nil) then
  398.         toolAnimTrack:Stop()
  399.         toolAnimTrack:Destroy()
  400.         toolAnimTrack = nil
  401.     end
  402.  
  403.  
  404.     return oldAnim
  405. end
  406.  
  407. -------------------------------------------------------------------------------------------
  408. -------------------------------------------------------------------------------------------
  409.  
  410.  
  411. function onRunning(speed)
  412.     if speed > 0.01 then
  413.         local scale = 16.0
  414.         playAnimation("walk", 0.1, Humanoid)
  415.         setAnimationSpeed(speed / scale)
  416.         pose = "Running"
  417.     else
  418.         if emoteNames[currentAnim] == nil then
  419.             playAnimation("idle", 0.1, Humanoid)
  420.             pose = "Standing"
  421.         end
  422.     end
  423. end
  424.  
  425. function onDied()
  426.     pose = "Dead"
  427. end
  428.  
  429. function onJumping()
  430.     playAnimation("jump", 0.1, Humanoid)
  431.     jumpAnimTime = jumpAnimDuration
  432.     pose = "Jumping"
  433. end
  434.  
  435. function onClimbing(speed)
  436.     local scale = 5.0
  437.     playAnimation("climb", 0.1, Humanoid)
  438.     setAnimationSpeed(speed / scale)
  439.     pose = "Climbing"
  440. end
  441.  
  442. function onGettingUp()
  443.     pose = "GettingUp"
  444. end
  445.  
  446. function onFreeFall()
  447.     if (jumpAnimTime <= 0) then
  448.         playAnimation("fall", fallTransitionTime, Humanoid)
  449.     end
  450.     pose = "FreeFall"
  451. end
  452.  
  453. function onFallingDown()
  454.     pose = "FallingDown"
  455. end
  456.  
  457. function onSeated()
  458.     pose = "Seated"
  459. end
  460.  
  461. function onPlatformStanding()
  462.     pose = "PlatformStanding"
  463. end
  464.  
  465. function onSwimming(speed)
  466.     if speed > 1.00 then
  467.         local scale = 10.0
  468.         playAnimation("swim", 0.4, Humanoid)
  469.         setAnimationSpeed(speed / scale)
  470.         pose = "Swimming"
  471.     else
  472.         playAnimation("swimidle", 0.4, Humanoid)
  473.         pose = "Standing"
  474.     end
  475. end
  476.  
  477. function getTool() 
  478.     for _, kid in ipairs(Figure:GetChildren()) do
  479.         if kid.className == "Tool" then return kid end
  480.     end
  481.     return nil
  482. end
  483.  
  484.  
  485. function animateTool()
  486.    
  487.     if (toolAnim == "None") then
  488.         playToolAnimation("toolnone", toolTransitionTime, Humanoid)
  489.         return
  490.     end
  491.  
  492.     if (toolAnim == "Slash") then
  493.         playToolAnimation("toolslash", 0, Humanoid)
  494.         return
  495.     end
  496.  
  497.     if (toolAnim == "Lunge") then
  498.         playToolAnimation("toollunge", 0, Humanoid)
  499.         return
  500.     end
  501. end
  502.  
  503. function getToolAnim(tool)
  504.     for _, c in ipairs(tool:GetChildren()) do
  505.         if c.Name == "toolanim" and c.className == "StringValue" then
  506.             return c
  507.         end
  508.     end
  509.     return nil
  510. end
  511.  
  512. local lastTick = 0
  513.  
  514. function move(time)
  515.     local amplitude = 1
  516.     local frequency = 1
  517.     local deltaTime = time - lastTick
  518.     lastTick = time
  519.  
  520.     local climbFudge = 0
  521.     local setAngles = false
  522.  
  523.     if (jumpAnimTime > 0) then
  524.         jumpAnimTime = jumpAnimTime - deltaTime
  525.     end
  526.  
  527.     if (pose == "FreeFall" and jumpAnimTime <= 0) then
  528.         playAnimation("fall", fallTransitionTime, Humanoid)
  529.     elseif (pose == "Seated") then
  530.         playAnimation("sit", 0.5, Humanoid)
  531.         return
  532.     elseif (pose == "Running") then
  533.         playAnimation("walk", 0.1, Humanoid)
  534.     elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  535.         stopAllAnimations()
  536.         amplitude = 0.1
  537.         frequency = 1
  538.         setAngles = true
  539.     end
  540.  
  541.     -- Tool Animation handling
  542.     local tool = getTool()
  543.     if tool and (tool.RequiresHandle or tool:FindFirstChild("Handle")) then
  544.    
  545.         local animStringValueObject = getToolAnim(tool)
  546.  
  547.         if animStringValueObject then
  548.             toolAnim = animStringValueObject.Value
  549.             -- message recieved, delete StringValue
  550.             animStringValueObject.Parent = nil
  551.             toolAnimTime = time + .3
  552.         end
  553.  
  554.         if time > toolAnimTime then
  555.             toolAnimTime = 0
  556.             toolAnim = "None"
  557.         end
  558.  
  559.         animateTool()      
  560.     else
  561.         stopToolAnimations()
  562.         toolAnim = "None"
  563.         toolAnimInstance = nil
  564.         toolAnimTime = 0
  565.     end
  566. end
  567.  
  568. -- connect events
  569. Humanoid.Died:connect(onDied)
  570. Humanoid.Running:connect(onRunning)
  571. Humanoid.Jumping:connect(onJumping)
  572. Humanoid.Climbing:connect(onClimbing)
  573. Humanoid.GettingUp:connect(onGettingUp)
  574. Humanoid.FreeFalling:connect(onFreeFall)
  575. Humanoid.FallingDown:connect(onFallingDown)
  576. Humanoid.Seated:connect(onSeated)
  577. Humanoid.PlatformStanding:connect(onPlatformStanding)
  578. Humanoid.Swimming:connect(onSwimming)
  579.  
  580.  
  581.  
  582. -- initialize to idle
  583. playAnimation("idle", 0.1, Humanoid)
  584. pose = "Standing"
  585.  
  586. -- loop to handle timed state transitions and tool animations
  587. while Figure.Parent~=nil do
  588.     local _, time = wait(0.1)
  589.     move(time)
  590. end
Advertisement
Add Comment
Please, Sign In to add comment