Guest User

Untitled

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