ExampleGame.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Microsoft.Xna.Framework;
  32. using Microsoft.Xna.Framework.Graphics;
  33. using Microsoft.Xna.Framework.Input;
  34. namespace Spine {
  35. public class Example : Microsoft.Xna.Framework.Game {
  36. GraphicsDeviceManager graphics;
  37. SkeletonRenderer skeletonRenderer;
  38. Skeleton skeleton;
  39. Slot headSlot;
  40. AnimationState state;
  41. SkeletonBounds bounds = new SkeletonBounds();
  42. private string assetsFolder = "data/";
  43. public Example() {
  44. IsMouseVisible = true;
  45. graphics = new GraphicsDeviceManager(this);
  46. graphics.IsFullScreen = false;
  47. graphics.PreferredBackBufferWidth = 800;
  48. graphics.PreferredBackBufferHeight = 600;
  49. }
  50. protected override void LoadContent() {
  51. // Two color tint effect, comment line 80 to disable
  52. var spineEffect = Content.Load<Effect>("Content\\SpineEffect");
  53. spineEffect.Parameters["World"].SetValue(Matrix.Identity);
  54. spineEffect.Parameters["View"].SetValue(Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 1.0f), Vector3.Zero, Vector3.Up));
  55. skeletonRenderer = new SkeletonRenderer(GraphicsDevice);
  56. skeletonRenderer.PremultipliedAlpha = false;
  57. skeletonRenderer.Effect = spineEffect;
  58. // String name = "spineboy-ess";
  59. // String name = "goblins-pro";
  60. String name = "raptor-pro";
  61. // String name = "tank-pro";
  62. // String name = "coin-pro";
  63. String atlasName = name.Replace("-pro", "").Replace("-ess", "");
  64. bool binaryData = false;
  65. Atlas atlas = new Atlas(assetsFolder + atlasName + ".atlas", new XnaTextureLoader(GraphicsDevice));
  66. float scale = 1;
  67. if (name == "spineboy-ess") scale = 0.6f;
  68. if (name == "raptor-pro") scale = 0.5f;
  69. if (name == "tank-pro") scale = 0.3f;
  70. if (name == "coin-pro") scale = 1;
  71. SkeletonData skeletonData;
  72. if (binaryData) {
  73. SkeletonBinary binary = new SkeletonBinary(atlas);
  74. binary.Scale = scale;
  75. skeletonData = binary.ReadSkeletonData(assetsFolder + name + ".skel");
  76. }
  77. else {
  78. SkeletonJson json = new SkeletonJson(atlas);
  79. json.Scale = scale;
  80. skeletonData = json.ReadSkeletonData(assetsFolder + name + ".json");
  81. }
  82. skeleton = new Skeleton(skeletonData);
  83. if (name == "goblins-pro") skeleton.SetSkin("goblin");
  84. // Define mixing between animations.
  85. AnimationStateData stateData = new AnimationStateData(skeleton.Data);
  86. state = new AnimationState(stateData);
  87. if (name == "spineboy-ess") {
  88. skeleton.SetAttachment("head-bb", "head"); // Activate the head BoundingBoxAttachment.
  89. stateData.SetMix("run", "jump", 0.2f);
  90. stateData.SetMix("jump", "run", 0.4f);
  91. // Event handling for all animations.
  92. state.Start += Start;
  93. state.End += End;
  94. state.Complete += Complete;
  95. state.Event += Event;
  96. state.SetAnimation(0, "run", false);
  97. TrackEntry entry = state.AddAnimation(0, "jump", false, 0);
  98. entry.End += End; // Event handling for queued animations.
  99. state.AddAnimation(0, "run", true, 0);
  100. }
  101. else if (name == "raptor-pro") {
  102. state.SetAnimation(0, "walk", true);
  103. state.AddAnimation(1, "gun-grab", false, 2);
  104. }
  105. else if (name == "coin-pro") {
  106. state.SetAnimation(0, "animation", true);
  107. }
  108. else if (name == "tank-pro") {
  109. state.SetAnimation(0, "drive", true);
  110. }
  111. else {
  112. state.SetAnimation(0, "walk", true);
  113. }
  114. skeleton.X = 400 + (name == "tank-pro" ? 300 : 0);
  115. skeleton.Y = GraphicsDevice.Viewport.Height;
  116. skeleton.ScaleY = -1;
  117. skeleton.UpdateWorldTransform();
  118. headSlot = skeleton.FindSlot("head");
  119. }
  120. protected override void Update(GameTime gameTime) {
  121. base.Update(gameTime);
  122. }
  123. protected override void Draw(GameTime gameTime) {
  124. GraphicsDevice.Clear(Color.Black);
  125. state.Update(gameTime.ElapsedGameTime.Milliseconds / 1000f);
  126. state.Apply(skeleton);
  127. skeleton.UpdateWorldTransform();
  128. if (skeletonRenderer.Effect is BasicEffect) {
  129. ((BasicEffect)skeletonRenderer.Effect).Projection = Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 1, 0);
  130. }
  131. else {
  132. skeletonRenderer.Effect.Parameters["Projection"].SetValue(Matrix.CreateOrthographicOffCenter(0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height, 0, 1, 0));
  133. }
  134. skeletonRenderer.Begin();
  135. skeletonRenderer.Draw(skeleton);
  136. skeletonRenderer.End();
  137. bounds.Update(skeleton, true);
  138. MouseState mouse = Mouse.GetState();
  139. if (headSlot != null) {
  140. headSlot.G = 1;
  141. headSlot.B = 1;
  142. if (bounds.AabbContainsPoint(mouse.X, mouse.Y)) {
  143. BoundingBoxAttachment hit = bounds.ContainsPoint(mouse.X, mouse.Y);
  144. if (hit != null) {
  145. headSlot.G = 0;
  146. headSlot.B = 0;
  147. }
  148. }
  149. }
  150. base.Draw(gameTime);
  151. }
  152. public void Start(TrackEntry entry) {
  153. Console.WriteLine(entry + ": start");
  154. }
  155. public void End(TrackEntry entry) {
  156. Console.WriteLine(entry + ": end");
  157. }
  158. public void Complete(TrackEntry entry) {
  159. Console.WriteLine(entry + ": complete ");
  160. }
  161. public void Event(TrackEntry entry, Event e) {
  162. Console.WriteLine(entry + ": event " + e);
  163. }
  164. }
  165. }