itchyzombie

r15

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