thngf

Untitled

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