Advertisement
redpawed

R15 Animation

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