main.lua 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. require("mobdebug").start()
  2. local spine = require "spine-corona.spine"
  3. local skeletons = {}
  4. local activeSkeleton = 1
  5. local lastTime = 0
  6. function loadSkeleton(atlasFile, jsonFile, x, y, scale, animation, skin)
  7. -- to load an atlas, we need to define a function that returns
  8. -- a Corona paint object. This allows you to resolve images
  9. -- however you see fit
  10. local imageLoader = function (path)
  11. local paint = { type = "image", filename = "data/" .. path }
  12. return paint
  13. end
  14. -- load the atlas
  15. local atlas = spine.TextureAtlas.new(spine.utils.readFile("data/" .. atlasFile), imageLoader)
  16. -- load the JSON and create a Skeleton from it
  17. local json = spine.SkeletonJson.new(spine.AtlasAttachmentLoader.new(atlas))
  18. json.scale = scale
  19. local skeletonData = json:readSkeletonDataFile("data/" .. jsonFile)
  20. local skeleton = spine.Skeleton.new(skeletonData)
  21. skeleton.flipY = true -- Corona's coordinate system has its y-axis point downwards
  22. skeleton.group.x = x
  23. skeleton.group.y = y
  24. -- Set the skin if we got one
  25. if skin then skeleton:setSkin(skin) end
  26. -- create an animation state object to apply animations to the skeleton
  27. local animationStateData = spine.AnimationStateData.new(skeletonData)
  28. animationStateData.defaultMix = 0.2
  29. local animationState = spine.AnimationState.new(animationStateData)
  30. -- set the skeleton invisible
  31. skeleton.group.isVisible = false
  32. -- set a name on the group of the skeleton so we can find it during debugging
  33. skeleton.group.name = jsonFile
  34. -- set some event callbacks
  35. animationState.onStart = function (entry)
  36. print(entry.trackIndex.." start: "..entry.animation.name)
  37. end
  38. animationState.onInterrupt = function (entry)
  39. print(entry.trackIndex.." interrupt: "..entry.animation.name)
  40. end
  41. animationState.onEnd = function (entry)
  42. print(entry.trackIndex.." end: "..entry.animation.name)
  43. end
  44. animationState.onComplete = function (entry)
  45. print(entry.trackIndex.." complete: "..entry.animation.name)
  46. end
  47. animationState.onDispose = function (entry)
  48. print(entry.trackIndex.." dispose: "..entry.animation.name)
  49. end
  50. animationState.onEvent = function (entry, event)
  51. print(entry.trackIndex.." event: "..entry.animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'")
  52. end
  53. if atlasFile == "spineboy.atlas" then
  54. animationStateData:setMix("walk", "jump", 0.4)
  55. animationStateData:setMix("jump", "run", 0.4);
  56. animationState:setAnimationByName(0, "walk", true)
  57. local jumpEntry = animationState:addAnimationByName(0, "jump", false, 3)
  58. animationState:addAnimationByName(0, "run", true, 0)
  59. else
  60. animationState:setAnimationByName(0, animation, true)
  61. end
  62. -- return the skeleton an animation state
  63. return { skeleton = skeleton, state = animationState }
  64. end
  65. -- table.insert(skeletons, loadSkeleton("test.atlas", "test.json", 240, 300, 0.4, "animation"))
  66. table.insert(skeletons, loadSkeleton("coin.atlas", "coin.json", 240, 300, 0.4, "rotate"))
  67. table.insert(skeletons, loadSkeleton("spineboy.atlas", "spineboy.json", 240, 300, 0.4, "walk"))
  68. table.insert(skeletons, loadSkeleton("raptor.atlas", "raptor.json", 200, 300, 0.25, "walk"))
  69. table.insert(skeletons, loadSkeleton("goblins.atlas", "goblins-mesh.json", 240, 300, 0.8, "walk", "goblin"))
  70. table.insert(skeletons, loadSkeleton("stretchyman.atlas", "stretchyman.json", 40, 300, 0.5, "sneak"))
  71. table.insert(skeletons, loadSkeleton("tank.atlas", "tank.json", 400, 300, 0.2, "drive"))
  72. table.insert(skeletons, loadSkeleton("vine.atlas", "vine.json", 240, 300, 0.3, "animation"))
  73. local triangulator = spine.Triangulator.new()
  74. local polygon = { 411, 219, 199, 230, 161, 362, 534, 407, 346, 305, 596, 265 }
  75. local indices = triangulator:triangulate(polygon)
  76. print(indices)
  77. print(triangulator:decompose(polygon, indices))
  78. local skeletonClipping = spine.SkeletonClipping.new()
  79. local polygon2 = {0, 0, 100, 0, 100, 100, 0, 100 }
  80. skeletonClipping:makeClockwise(polygon2)
  81. print(polygon2)
  82. local bounds = spine.SkeletonBounds.new()
  83. skeletons[1].skeleton:updateWorldTransform()
  84. bounds:update(skeletons[1].skeleton, true)
  85. local offset = {}
  86. local size = {}
  87. skeletons[1].skeleton:getBounds(offset, size)
  88. display.setDefault("background", 0.2, 0.2, 0.2, 1)
  89. Runtime:addEventListener("enterFrame", function (event)
  90. local currentTime = event.time / 1000
  91. local delta = currentTime - lastTime
  92. lastTime = currentTime
  93. skeleton = skeletons[activeSkeleton].skeleton
  94. skeleton.group.isVisible = true
  95. state = skeletons[activeSkeleton].state
  96. state:update(delta)
  97. state:apply(skeleton)
  98. skeleton:updateWorldTransform()
  99. -- uncomment if you want to know how many batches a skeleton renders to
  100. -- print(skeleton.batches)
  101. end)
  102. Runtime:addEventListener("key", function(event)
  103. if activeSkeleton == 2 and event.phase == "down" then
  104. state = skeletons[activeSkeleton].state
  105. state:setAnimationByName(0, "jump", false)
  106. state:addAnimationByName(0, "walk", true, 0)
  107. end
  108. return false
  109. end)
  110. Runtime:addEventListener("tap", function(event)
  111. skeletons[activeSkeleton].skeleton.group.isVisible = false
  112. activeSkeleton = activeSkeleton + 1
  113. if activeSkeleton > #skeletons then activeSkeleton = 1 end
  114. skeletons[activeSkeleton].skeleton.group.isVisible = true
  115. end)