main.lua 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. -------------------------------------------------------------------------------
  2. -- Spine Runtimes Software License v2.5
  3. --
  4. -- Copyright (c) 2013-2016, Esoteric Software
  5. -- All rights reserved.
  6. --
  7. -- You are granted a perpetual, non-exclusive, non-sublicensable, and
  8. -- non-transferable license to use, install, execute, and perform the Spine
  9. -- Runtimes software and derivative works solely for personal or internal
  10. -- use. Without the written permission of Esoteric Software (see Section 2 of
  11. -- the Spine Software License Agreement), you may not (a) modify, translate,
  12. -- adapt, or develop new applications using the Spine Runtimes or otherwise
  13. -- create derivative works or improvements of the Spine Runtimes or (b) remove,
  14. -- delete, alter, or obscure any trademarks or any copyright, trademark, patent,
  15. -- or other intellectual property or proprietary rights notices on or in the
  16. -- Software, including any copy thereof. Redistributions in binary or source
  17. -- form must include this license and terms.
  18. --
  19. -- THIS SOFTWARE IS PROVIDED BY ESOTERIC SOFTWARE "AS IS" AND ANY EXPRESS OR
  20. -- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. -- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  22. -- EVENT SHALL ESOTERIC SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. -- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. -- PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, BUSINESS INTERRUPTION, OR LOSS OF
  25. -- USE, DATA, OR PROFITS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. -- IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. -- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. -- POSSIBILITY OF SUCH DAMAGE.
  29. -------------------------------------------------------------------------------
  30. local spine = require "spine-love.spine"
  31. local skeletons = {}
  32. local activeSkeleton = 1
  33. local swirl = spine.SwirlEffect.new(400)
  34. local swirlTime = 0
  35. function loadSkeleton (jsonFile, atlasFile, animation, skin, scale, x, y)
  36. local loader = function (path) return love.graphics.newImage("data/" .. path) end
  37. local atlas = spine.TextureAtlas.new(spine.utils.readFile("data/" .. atlasFile .. ".atlas"), loader)
  38. local json = spine.SkeletonJson.new(spine.AtlasAttachmentLoader.new(atlas))
  39. json.scale = scale
  40. local skeletonData = json:readSkeletonDataFile("data/" .. jsonFile .. ".json")
  41. local skeleton = spine.Skeleton.new(skeletonData)
  42. skeleton.x = x
  43. skeleton.y = y
  44. skeleton.scaleY = -1
  45. if skin then
  46. skeleton:setSkin(skin)
  47. end
  48. skeleton:setToSetupPose()
  49. local stateData = spine.AnimationStateData.new(skeletonData)
  50. local state = spine.AnimationState.new(stateData)
  51. state:setAnimationByName(0, animation, true)
  52. if (jsonFile == "spineboy-ess") then
  53. stateData:setMix("walk", "jump", 0.5)
  54. stateData:setMix("jump", "run", 0.5)
  55. state:addAnimationByName(0, "jump", false, 3)
  56. state:addAnimationByName(0, "run", true, 0)
  57. end
  58. if (jsonFile == "raptor-pro") then
  59. swirl.centerY = -200
  60. skeleton.vertexEffect = swirl
  61. -- skeleton.vertexEffect = spine.JitterEffect.new(10, 10)
  62. end
  63. -- set some event callbacks
  64. state.onStart = function (entry)
  65. print(entry.trackIndex.." start: "..entry.animation.name)
  66. end
  67. state.onInterrupt = function (entry)
  68. print(entry.trackIndex.." interrupt: "..entry.animation.name)
  69. end
  70. state.onEnd = function (entry)
  71. print(entry.trackIndex.." end: "..entry.animation.name)
  72. end
  73. state.onComplete = function (entry)
  74. print(entry.trackIndex.." complete: "..entry.animation.name)
  75. end
  76. state.onDispose = function (entry)
  77. print(entry.trackIndex.." dispose: "..entry.animation.name)
  78. end
  79. state.onEvent = function (entry, event)
  80. print(entry.trackIndex.." event: "..entry.animation.name..", "..event.data.name..", "..event.intValue..", "..event.floatValue..", '"..(event.stringValue or "").."'" .. ", " .. event.volume .. ", " .. event.balance)
  81. end
  82. state:update(0.5)
  83. state:apply(skeleton)
  84. return { state = state, skeleton = skeleton }
  85. end
  86. function love.load(arg)
  87. if arg[#arg] == "-debug" then require("mobdebug").start() end
  88. skeletonRenderer = spine.SkeletonRenderer.new(true)
  89. table.insert(skeletons, loadSkeleton("spineboy-pro", "spineboy", "walk", nil, 0.5, 400, 500))
  90. table.insert(skeletons, loadSkeleton("stretchyman-pro", "stretchyman", "sneak", nil, 0.3, 200, 500))
  91. table.insert(skeletons, loadSkeleton("coin-pro", "coin", "rotate", nil, 0.5, 400, 500))
  92. table.insert(skeletons, loadSkeleton("raptor-pro", "raptor", "walk", nil, 0.3, 400, 500))
  93. table.insert(skeletons, loadSkeleton("goblins-pro", "goblins", "walk", "goblin", 1, 400, 500))
  94. table.insert(skeletons, loadSkeleton("tank-pro", "tank", "drive", nil, 0.2, 600, 500))
  95. table.insert(skeletons, loadSkeleton("vine-pro", "vine", "grow", nil, 0.3, 400, 500))
  96. table.insert(skeletons, loadSkeleton("stretchyman-stretchy-ik-pro", "stretchyman", "sneak", nil, 0.3, 200, 500))
  97. end
  98. function love.update (delta)
  99. -- Update the state with the delta time, apply it, and update the world transforms.
  100. local state = skeletons[activeSkeleton].state
  101. local skeleton = skeletons[activeSkeleton].skeleton
  102. state:update(delta)
  103. state:apply(skeleton)
  104. skeleton:updateWorldTransform()
  105. if (skeleton.vertexEffect) then
  106. skeletonRenderer.vertexEffect = skeleton.vertexEffect
  107. if (skeleton.vertexEffect == swirl) then
  108. swirlTime = swirlTime + delta
  109. local percent = swirlTime % 2
  110. if (percent > 1) then percent = 1 - (percent - 1) end
  111. swirl.angle = spine.Interpolation.apply(spine.Interpolation.pow2, -60, 60, percent)
  112. end
  113. else
  114. skeletonRenderer.vertexEffect = nil
  115. end
  116. end
  117. function love.draw ()
  118. love.graphics.setBackgroundColor(0, 0, 0, 255)
  119. love.graphics.setColor(255, 255, 255)
  120. local skeleton = skeletons[activeSkeleton].skeleton
  121. skeletonRenderer:draw(skeleton)
  122. end
  123. function love.mousepressed (x, y, button, istouch)
  124. activeSkeleton = activeSkeleton + 1
  125. if activeSkeleton > #skeletons then activeSkeleton = 1 end
  126. end