Advertisement
MrFinn

ComputerCraft - Animatronic test

Nov 30th, 2024 (edited)
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. -- Assuming 'content' contains the parsed animation data
  2. local parsed = {
  3. format_version = "1.8.0",
  4. animations = {
  5. ["animation.template"] = {
  6. animation_length = 10,
  7. bones = {
  8. head = {
  9. rotation= {
  10. ["0.0"] = {0, 0, 0},
  11. ["1.0"] = {0, -40, 0},
  12. ["2.0"] = {0, -130, 0},
  13. ["3.0"] = {0, -180, 0},
  14. ["4.0"] = {0, 160, 0},
  15. ["5.0"] = {0, 70, 0},
  16. ["6.0"] = {0, 50, 0},
  17. ["7.0"] = {0, -50, 0},
  18. ["8.0"] = {0, -10, 0},
  19. ["9.0"] = {0, 0, 0},
  20. ["10.0"] = {0, 0, 0}
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27.  
  28. -- Create a table to hold the baked animations
  29. local animations = {}
  30.  
  31. -- Loop through each animation entry in parsed data
  32. for name, data in pairs(parsed.animations) do
  33. local baked = {} -- Create a table to hold the baked animation data for this entry
  34.  
  35. -- Loop through each bone in the animation data
  36. for part, rots in pairs(data.bones) do
  37. baked[part] = {} -- Create a table to hold the baked bone rotation data
  38.  
  39. -- Check if the rotation data is a single set of rotations or a series
  40. if type(rots.rotation[1]) == "number" then
  41. baked[part][1] = rots.rotation -- If single, add it as-is
  42. else
  43. local indexes = {}
  44.  
  45. -- Collect and sort the frame indexes
  46. for index, rot in pairs(rots.rotation) do
  47. table.insert(indexes, index)
  48. end
  49. table.sort(indexes, function(a, b) return tonumber(a) < tonumber(b) end)
  50.  
  51. -- Reorganize the rotation data using the sorted indexes
  52. for new_index, real_index in ipairs(indexes) do
  53. baked[part][new_index] = rots.rotation[real_index]
  54. end
  55. end
  56. end
  57.  
  58. -- Add the baked animation data to the animations table
  59. animations[name] = baked
  60. end
  61.  
  62. -- Obtenir l'accès à l'animatronique connecté
  63. local animatronic = peripheral.wrap("top") -- Changez "top" si le périphérique est ailleurs
  64.  
  65. -- Vérifier si le périphérique est bien un animatronique
  66. if not animatronic or not animatronic.setHeadRot then
  67. error("Aucun animatronique détecté sur le port 'top'. Vérifiez la connexion.")
  68. end
  69.  
  70. -- Fonction pour jouer une animation
  71. function playAnimation(animName, animations, frameTime)
  72. local anim = animations[animName]
  73. if not anim then
  74. error("Animation non trouvée : " .. animName)
  75. end
  76.  
  77. -- Obtenir la durée de l'animation et les os concernés
  78. local bones = anim
  79. local maxFrames = 0
  80. for _, frames in pairs(bones) do
  81. maxFrames = math.max(maxFrames, #frames)
  82. end
  83.  
  84. -- Jouer les frames de l'animation
  85. for frame = 1, maxFrames do
  86. -- Appliquer les rotations pour chaque os
  87. for part, frames in pairs(bones) do
  88. local rotation = frames[frame]
  89. if rotation then
  90. if part == "head" then
  91. animatronic.setHeadRot(rotation[1], rotation[2], rotation[3])
  92. elseif part == "body" then
  93. animatronic.setBodyRot(rotation[1], rotation[2], rotation[3])
  94. elseif part == "left_arm" then
  95. animatronic.setLeftArmRot(rotation[1], rotation[2], rotation[3])
  96. elseif part == "right_arm" then
  97. animatronic.setRightArmRot(rotation[1], rotation[2], rotation[3])
  98. end
  99. end
  100. end
  101.  
  102. -- Pousser les rotations vers l'animatronique
  103. animatronic.push()
  104.  
  105. -- Attendre jusqu'à la prochaine frame
  106. sleep(frameTime)
  107. end
  108. end
  109. -- Exemple d'utilisation
  110. local frameTime = 0.5 -- 0.5 secondes par frame
  111. playAnimation("animation.template", animations, frameTime)
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement