Donor2222

Anima

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