ExampleGame.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. using System;
  31. using System.IO;
  32. using System.Collections.Generic;
  33. using Microsoft.Xna.Framework;
  34. using Microsoft.Xna.Framework.Audio;
  35. using Microsoft.Xna.Framework.Content;
  36. using Microsoft.Xna.Framework.Graphics;
  37. using Microsoft.Xna.Framework.Input;
  38. using Microsoft.Xna.Framework.Media;
  39. using Spine;
  40. namespace Spine {
  41. public class Example : Microsoft.Xna.Framework.Game {
  42. GraphicsDeviceManager graphics;
  43. SkeletonMeshRenderer skeletonRenderer;
  44. Skeleton skeleton;
  45. Slot headSlot;
  46. AnimationState state;
  47. SkeletonBounds bounds = new SkeletonBounds();
  48. private string assetsFolder = "data/";
  49. public Example () {
  50. IsMouseVisible = true;
  51. graphics = new GraphicsDeviceManager(this);
  52. graphics.IsFullScreen = false;
  53. graphics.PreferredBackBufferWidth = 800;
  54. graphics.PreferredBackBufferHeight = 600;
  55. }
  56. protected override void Initialize () {
  57. // TODO: Add your initialization logic here
  58. base.Initialize();
  59. }
  60. protected override void LoadContent () {
  61. skeletonRenderer = new SkeletonMeshRenderer(GraphicsDevice);
  62. skeletonRenderer.PremultipliedAlpha = true;
  63. // String name = "spineboy";
  64. // String name = "goblins-mesh";
  65. // String name = "raptor";
  66. String name = "tank";
  67. // String name = "star";
  68. bool binaryData = true;
  69. Atlas atlas = new Atlas(assetsFolder + name + ".atlas", new XnaTextureLoader(GraphicsDevice));
  70. float scale = 1;
  71. if (name == "spineboy") scale = 0.6f;
  72. if (name == "raptor") scale = 0.5f;
  73. if (name == "tank") scale = 0.3f;
  74. SkeletonData skeletonData;
  75. if (binaryData) {
  76. SkeletonBinary binary = new SkeletonBinary(atlas);
  77. binary.Scale = scale;
  78. skeletonData = binary.ReadSkeletonData(assetsFolder + name + ".skel");
  79. } else {
  80. SkeletonJson json = new SkeletonJson(atlas);
  81. json.Scale = scale;
  82. skeletonData = json.ReadSkeletonData(assetsFolder + name + ".json");
  83. }
  84. skeleton = new Skeleton(skeletonData);
  85. if (name == "goblins-mesh") skeleton.SetSkin("goblin");
  86. // Define mixing between animations.
  87. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  88. state = new AnimationState(stateData);
  89. if (name == "spineboy") {
  90. stateData.SetMix("run", "jump", 0.2f);
  91. stateData.SetMix("jump", "run", 0.4f);
  92. // Event handling for all animations.
  93. state.Start += Start;
  94. state.End += End;
  95. state.Complete += Complete;
  96. state.Event += Event;
  97. state.SetAnimation(0, "test", false);
  98. TrackEntry entry = state.AddAnimation(0, "jump", false, 0);
  99. entry.End += End; // Event handling for queued animations.
  100. state.AddAnimation(0, "run", true, 0);
  101. }
  102. else if (name == "raptor") {
  103. state.SetAnimation(0, "walk", true);
  104. state.AddAnimation(1, "gungrab", false, 2);
  105. }
  106. else if (name == "star") {
  107. // no animation in star
  108. }
  109. else if (name == "tank") {
  110. state.SetAnimation(0, "drive", true);
  111. } else {
  112. state.SetAnimation(0, "walk", true);
  113. }
  114. skeleton.X = 400 + (name == "tank" ? 300: 0);
  115. skeleton.Y = 580;
  116. skeleton.UpdateWorldTransform();
  117. headSlot = skeleton.FindSlot("head");
  118. }
  119. protected override void UnloadContent () {
  120. }
  121. protected override void Update (GameTime gameTime) {
  122. base.Update(gameTime);
  123. }
  124. protected override void Draw (GameTime gameTime) {
  125. GraphicsDevice.Clear(Color.Black);
  126. state.Update(gameTime.ElapsedGameTime.Milliseconds / 1000f);
  127. state.Apply(skeleton);
  128. skeleton.UpdateWorldTransform();
  129. skeletonRenderer.Begin();
  130. skeletonRenderer.Draw(skeleton);
  131. skeletonRenderer.End();
  132. bounds.Update(skeleton, true);
  133. MouseState mouse = Mouse.GetState();
  134. if (headSlot != null) {
  135. headSlot.G = 1;
  136. headSlot.B = 1;
  137. if (bounds.AabbContainsPoint(mouse.X, mouse.Y)) {
  138. BoundingBoxAttachment hit = bounds.ContainsPoint(mouse.X, mouse.Y);
  139. if (hit != null) {
  140. headSlot.G = 0;
  141. headSlot.B = 0;
  142. }
  143. }
  144. }
  145. base.Draw(gameTime);
  146. }
  147. public void Start (TrackEntry entry) {
  148. Console.WriteLine(entry + ": start");
  149. }
  150. public void End (TrackEntry entry) {
  151. Console.WriteLine(entry + ": end");
  152. }
  153. public void Complete (TrackEntry entry) {
  154. Console.WriteLine(entry + ": complete ");
  155. }
  156. public void Event (TrackEntry entry, Event e) {
  157. Console.WriteLine(entry + ": event " + e);
  158. }
  159. }
  160. }