main.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function SpineToy::create(%this)
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pan );
  26. // Set the manipulation mode.
  27. Sandbox.useManipulation( pan );
  28. %this.asset = "SpineToy:goblins";
  29. %this.skin = "goblin";
  30. %this.animation = "walk";
  31. addSelectionOption( "goblin,goblingirl", "Select Skin", 4, "setSkin", false, "Sets the skin for the skeleton object." );
  32. // Reset the toy.
  33. SpineToy.reset();
  34. }
  35. //-----------------------------------------------------------------------------
  36. function SpineToy::destroy(%this)
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. function SpineToy::reset(%this)
  41. {
  42. // Clear the scene.
  43. SandboxScene.clear();
  44. if (%this.resetSchedule !$= "")
  45. cancel(%this.resetSchedule);
  46. // Set the camera size.
  47. SandboxWindow.setCameraSize( 40, 30 );
  48. %this.createBackground();
  49. %this.createGoblin();
  50. }
  51. //-----------------------------------------------------------------------------
  52. function SpineToy::setSkeleton(%this, %value)
  53. {
  54. %this.asset = %value;
  55. if (%value $= "SpineToy:goblins")
  56. %this.setSkin("goblin");
  57. else
  58. %this.setSkin("default");
  59. }
  60. //-----------------------------------------------------------------------------
  61. function SpineToy::setSkin(%this, %value)
  62. {
  63. %this.skin = %value;
  64. %this.walker.Skin = %this.skin;
  65. }
  66. //-----------------------------------------------------------------------------
  67. function SpineToy::createGoblin(%this)
  68. {
  69. // Create the skeleton object
  70. %goblin = new SkeletonObject();
  71. // Assign it an asset
  72. %goblin.Asset = %this.asset;
  73. %goblin.Skin = %this.skin;
  74. // Set the animation name
  75. %goblin.setAnimationName(%this.animation, true);
  76. %goblin.RootBoneScale = 0.025;
  77. %goblin.setRootBoneOffset(0, -5);
  78. %goblin.position = "-25 -8";
  79. %goblin.SceneLayer = 29;
  80. %goblin.setLinearVelocity(7.5, 0);
  81. %this.walker = %goblin;
  82. %this.resetSchedule = %this.schedule(8000, resetWalker);
  83. // Add it to the scene
  84. SandboxScene.add(%goblin);
  85. }
  86. //-----------------------------------------------------------------------------
  87. function SpineToy::createSpineBoy(%this)
  88. {
  89. // Create the skeleton object
  90. %object = new SkeletonObject()
  91. {
  92. class = "SpineBoy";
  93. };
  94. %object.Asset = "SpineToy:TestSkeleton";
  95. %object.setAnimationName("walk", true);
  96. %object.RootBoneScale = 0.025;
  97. %object.SceneLayer = 29;
  98. %object.Position = 0;
  99. %object.setMix("walk", "jump", 0.2);
  100. %object.setMix("jump", "walk", 0.4);
  101. SandboxScene.add(%object);
  102. %object.schedule(4000, "doJump");
  103. }
  104. //-----------------------------------------------------------------------------
  105. function SpineBoy::onAnimationFinished(%this, %animationName)
  106. {
  107. if (%animationName $= "jump")
  108. {
  109. %this.setAnimationName("walk", true);
  110. %this.schedule(4000, "doJump");
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. function SpineBoy::doJump(%this)
  115. {
  116. %this.setAnimationName("jump", false);
  117. }
  118. //-----------------------------------------------------------------------------
  119. function SpineToy::resetWalker(%this)
  120. {
  121. %this.walker.setPosition("-25 -8");
  122. %this.resetSchedule = %this.schedule(8000, resetWalker);
  123. }
  124. //-----------------------------------------------------------------------------
  125. function SpineToy::createBackground(%this)
  126. {
  127. // Create the sprite.
  128. %object = new Sprite();
  129. // Set the sprite as "static" so it is not affected by gravity.
  130. %object.setBodyType( static );
  131. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  132. // Set the position.
  133. %object.Position = "0 0";
  134. // Set the size.
  135. %object.Size = "40 30";
  136. // Set to the furthest background layer.
  137. %object.SceneLayer = 31;
  138. // Set an image.
  139. %object.Image = "SpineToy:background";
  140. // Add the sprite to the scene.
  141. SandboxScene.add( %object );
  142. %this.createPowerup(-14, 6);
  143. %this.createPowerup(14, 6);
  144. // Create the skeleton object
  145. %animatedMenu = new SkeletonObject();
  146. // Assign it an asset
  147. %animatedMenu.Asset = "SpineToy:spinosaurus";
  148. // Set properties
  149. %animatedMenu.setAnimationName("Animation", false);
  150. %duration = %animatedMenu.getAnimationDuration();
  151. %animatedMenu.schedule(%duration*1000, "setAnimationName", "loop", true);
  152. %animatedMenu.position = "0 0";
  153. %animatedMenu.SceneLayer = 30;
  154. %animatedMenu.RootBoneScale = 0.025;
  155. %animatedMenu.setRootBoneOffset(0, -10);
  156. // Add it to the scene
  157. SandboxScene.add(%animatedMenu);
  158. }
  159. //-----------------------------------------------------------------------------
  160. function SpineToy::createPowerup(%this, %xPos, %yPos)
  161. {
  162. // Create the skeleton object
  163. %powerup = new SkeletonObject();
  164. // Assign it an asset
  165. %powerup.Asset = "SpineToy:powerup";
  166. // Set properties
  167. %powerup.setAnimationName("Animation", true);
  168. %powerup.position = %xPos SPC %yPos;
  169. %powerup.SceneLayer = 30;
  170. %powerup.RootBoneScale = 0.025;
  171. %powerup.setRootBoneOffset(0, -5);
  172. // Add it to the scene
  173. SandboxScene.add(%powerup);
  174. }
  175. //-----------------------------------------------------------------------------