Advertisement
cat568

Untitled

Mar 8th, 2019
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 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 = "rbxassetid://2932025247", weight = 9 },
  29. { id = "rbxassetid://125750618", weight = 1 }
  30. },
  31. walk = {
  32. { id = "rbxassetid://2932031592", weight = 10 }
  33. },
  34. run = {
  35. { id = "run.xml", weight = 10 }
  36. },
  37. jump = {
  38. { id = "rbxassetid://2932036872", weight = 10 }
  39. },
  40. fall = {
  41. { id = "rbxassetid://2932040543", weight = 10 }
  42. },
  43. climb = {
  44. { id = "rbxassetid://125750800", weight = 10 }
  45. },
  46. toolnone = {
  47. { id = "rbxassetid://125750867", weight = 10 }
  48. },
  49. toolslash = {
  50. { id = "rbxassetid://129967390", weight = 10 }
  51. -- { id = "slash.xml", weight = 10 }
  52. },
  53. toollunge = {
  54. { id = "rbxassetid://129967478", weight = 10 }
  55. },
  56. wave = {
  57. { id = "rbxassetid://128777973", weight = 10 }
  58. },
  59. point = {
  60. { id = "rbxassetid://128853357", weight = 10 }
  61. },
  62. dance = {
  63. { id = "rbxassetid://130018893", weight = 10 },
  64. { id = "rbxassetid://132546839", weight = 10 },
  65. { id = "rbxassetid://132546884", weight = 10 }
  66. },
  67. laugh = {
  68. { id = "rbxassetid://129423131", weight = 10 }
  69. },
  70. cheer = {
  71. { id = "rbxassetid://=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(3.14 /2)
  381. LeftShoulder:SetDesiredAngle(-3.14 /2)
  382. RightHip:SetDesiredAngle(3.14 /2)
  383. LeftHip:SetDesiredAngle(-3.14 /2)
  384. end
  385.  
  386. local lastTick = 0
  387.  
  388. function move(time)
  389. local amplitude = 1
  390. local frequency = 1
  391. local deltaTime = time - lastTick
  392. lastTick = time
  393.  
  394. local climbFudge = 0
  395. local setAngles = false
  396.  
  397. if (jumpAnimTime > 0) then
  398. jumpAnimTime = jumpAnimTime - deltaTime
  399. end
  400.  
  401. if (pose == "FreeFall" and jumpAnimTime <= 0) then
  402. playAnimation("fall", fallTransitionTime, Humanoid)
  403. elseif (pose == "Seated") then
  404. stopAllAnimations()
  405. moveSit()
  406. return
  407. elseif (pose == "Running") then
  408. playAnimation("walk", 0.1, Humanoid)
  409. elseif (pose == "Dead" or pose == "GettingUp" or pose == "FallingDown" or pose == "Seated" or pose == "PlatformStanding") then
  410. -- print("Wha " .. pose)
  411. amplitude = 0.1
  412. frequency = 1
  413. setAngles = true
  414. end
  415.  
  416. if (setAngles) then
  417. desiredAngle = amplitude * math.sin(time * frequency)
  418.  
  419. RightShoulder:SetDesiredAngle(desiredAngle + climbFudge)
  420. LeftShoulder:SetDesiredAngle(desiredAngle - climbFudge)
  421. RightHip:SetDesiredAngle(-desiredAngle)
  422. LeftHip:SetDesiredAngle(-desiredAngle)
  423. end
  424.  
  425. -- Tool Animation handling
  426. local tool = getTool()
  427. if tool then
  428.  
  429. animStringValueObject = getToolAnim(tool)
  430.  
  431. if animStringValueObject then
  432. toolAnim = animStringValueObject.Value
  433. -- message recieved, delete StringValue
  434. animStringValueObject.Parent = nil
  435. toolAnimTime = time + .3
  436. end
  437.  
  438. if time > toolAnimTime then
  439. toolAnimTime = 0
  440. toolAnim = "None"
  441. end
  442.  
  443. animateTool()
  444. else
  445. stopToolAnimations()
  446. toolAnim = "None"
  447. toolAnimTime = 0
  448. end
  449. end
  450.  
  451. -- connect events
  452. Humanoid.Died:connect(onDied)
  453. Humanoid.Running:connect(onRunning)
  454. Humanoid.Jumping:connect(onJumping)
  455. Humanoid.Climbing:connect(onClimbing)
  456. Humanoid.GettingUp:connect(onGettingUp)
  457. Humanoid.FreeFalling:connect(onFreeFall)
  458. Humanoid.FallingDown:connect(onFallingDown)
  459. Humanoid.Seated:connect(onSeated)
  460. Humanoid.PlatformStanding:connect(onPlatformStanding)
  461. Humanoid.Swimming:connect(onSwimming)
  462.  
  463. -- setup emote chat hook
  464. Game.Players.LocalPlayer.Chatted:connect(function(msg)
  465. local emote = ""
  466. if (string.sub(msg, 1, 3) == "/e ") then
  467. emote = string.sub(msg, 4)
  468. elseif (string.sub(msg, 1, 7) == "/emote ") then
  469. emote = string.sub(msg, 8)
  470. end
  471.  
  472. if (pose == "Standing" and emoteNames[emote] ~= nil) then
  473. playAnimation(emote, 0.1, Humanoid)
  474. end
  475. -- print("===> " .. string.sub(msg, 1, 3) .. "(" .. emote .. ")")
  476. end)
  477.  
  478.  
  479. -- main program
  480.  
  481. local runService = game:service("RunService");
  482.  
  483. -- initialize to idle
  484. playAnimation("idle", 0.1, Humanoid)
  485. pose = "Standing"
  486.  
  487. while Figure.Parent~=nil do
  488. local _, time = wait(0.1)
  489. move(time)
  490. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement