main.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 SphereStackToy::create( %this )
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pan );
  26. Sandbox.allowManipulation( pull );
  27. // Set the manipulation mode.
  28. Sandbox.useManipulation( pull );
  29. // Initialize the toys settings.
  30. SphereStackToy.maxBalls = 10;
  31. SphereStackToy.ballStacks = 1;
  32. SphereStackToy.GroundWidth = 150;
  33. // Add the custom controls.
  34. addNumericOption("Balls to stack", 1, 15, 1, "setMaxBalls", SphereStackToy.maxBalls, true, "Sets the number of balls to stack.");
  35. addNumericOption("Balls stacks", 1, 10, 1, "setBallStacks", SphereStackToy.ballStacks, true, "Sets the number of ball stacks.");
  36. // Reset the toy initially.
  37. SphereStackToy.reset();
  38. }
  39. //-----------------------------------------------------------------------------
  40. function SphereStackToy::destroy( %this )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. function SphereStackToy::reset(%this)
  45. {
  46. // Clear the scene.
  47. SandboxScene.clear();
  48. // Zoom the camera in
  49. SandboxWindow.setCameraSize( 50, 37.5 );
  50. // Prefer the collision option off as it severely affects the performance.
  51. setCollisionOption( false );
  52. // Set the scene gravity.
  53. SandboxScene.setGravity( 0, -9.8 );
  54. // Reset the ball count.
  55. %this.currentBalls = 0;
  56. // Create the background.
  57. %this.createBackground();
  58. // Create the ground.
  59. %this.createGround();
  60. // Start the timer.
  61. SphereStackToy.startTimer( "createball", 100 );
  62. }
  63. //-----------------------------------------------------------------------------
  64. function SphereStackToy::createBackground( %this )
  65. {
  66. // Create the sprite.
  67. %object = new Sprite();
  68. // Stop the background from being affected by gravity.
  69. %object.setBodyType( "static" );
  70. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  71. // Set the position.
  72. %object.Position = "0 0";
  73. // Set the size.
  74. %object.Size = "100 75";
  75. // Set to the furthest background layer.
  76. %object.SceneLayer = 31;
  77. // Set an image.
  78. %object.Image = "ToyAssets:skyBackground";
  79. // Add the sprite to the scene.
  80. SandboxScene.add( %object );
  81. }
  82. //-----------------------------------------------------------------------------
  83. function SphereStackToy::createGround( %this )
  84. {
  85. // Create the ground
  86. %ground = new Scroller();
  87. %ground.setBodyType("static");
  88. %ground.Image = "ToyAssets:dirtGround";
  89. %ground.setPosition(0, -16);
  90. %ground.setSize(SphereStackToy.GroundWidth, 6);
  91. %ground.setRepeatX(SphereStackToy.GroundWidth / 60);
  92. %ground.createEdgeCollisionShape(SphereStackToy.GroundWidth/-2, 3, SphereStackToy.GroundWidth/2, 3);
  93. SandboxScene.add(%ground);
  94. // Create the grass.
  95. %grass = new Sprite();
  96. %grass.setBodyType("static");
  97. %grass.Image = "ToyAssets:grassForeground";
  98. %grass.setPosition(0, -12.5);
  99. %grass.setSize(SphereStackToy.GroundWidth, 2);
  100. SandboxScene.add(%grass);
  101. }
  102. //-----------------------------------------------------------------------------
  103. function SphereStackToy::createBall(%this)
  104. {
  105. // Finish if exceeded the required number of balls.
  106. if ( %this.currentBalls >= %this.maxBalls)
  107. {
  108. // Stop the timer.
  109. SphereStackToy.stopTimer();
  110. return;
  111. }
  112. // Set the ball size.
  113. %ballSize = 2;
  114. // Calculate ball-stack offset and stride.
  115. %ballOffset = ((SphereStackToy.ballStacks-1) * %ballSize * 2) * -0.5;
  116. // Add a ball to each stack.
  117. for( %n = 0; %n < SphereStackToy.ballStacks; %n++ )
  118. {
  119. // Calculate the stack offset.
  120. %stackOffset = %ballOffset + (%n * %ballSize * 2);
  121. // Create the ball.
  122. %ball = new Sprite();
  123. %ball.Position = %stackOffset SPC (5 + 6 * %this.currentBalls);
  124. %ball.Size = %ballSize;
  125. %ball.Image = "ToyAssets:Football";
  126. %ball.setDefaultDensity( 0.1 );
  127. %ball.setDefaultRestitution( 0.5 );
  128. %ball.setLinearVelocity(0, -20);
  129. %ball.createCircleCollisionShape( %ballSize * 0.5 );
  130. // Add to the scene.
  131. SandboxScene.add( %ball );
  132. }
  133. // Increase ball count.
  134. %this.currentBalls++;
  135. // Finish if exceeded the required number of balls.
  136. if ( %this.currentBalls == %this.maxBalls)
  137. {
  138. // Stop the timer.
  139. SphereStackToy.stopTimer();
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. function SphereStackToy::setMaxBalls(%this, %value)
  144. {
  145. %this.maxBalls = %value;
  146. }
  147. //-----------------------------------------------------------------------------
  148. function SphereStackToy::setBallStacks(%this, %value)
  149. {
  150. %this.ballStacks = %value;
  151. }