main.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 CollisionToy::create( %this )
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pull );
  26. // Set the manipulation mode.
  27. Sandbox.useManipulation( pull );
  28. // Configure the toy.
  29. CollisionToy.MaxBlockers = 50;
  30. CollisionToy.MaxBalls = 10;
  31. // Add configuration option.
  32. addNumericOption("Max Blockers", 1, 100, 10, "setMaxBlockers", CollisionToy.MaxBlockers, true, "Sets the number of blockers created.");
  33. addNumericOption("Max Balls", 1, 100, 10, "setMaxBalls", CollisionToy.MaxBalls, true, "Sets the number of balls created.");
  34. // Reset the toy.
  35. CollisionToy.reset();
  36. }
  37. //-----------------------------------------------------------------------------
  38. function CollisionToy::destroy( %this )
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. function CollisionToy::reset( %this )
  43. {
  44. // Clear the scene.
  45. SandboxScene.clear();
  46. // Create background.
  47. %this.createBackground();
  48. // Create blockers.
  49. %this.createBlockers();
  50. // Create balls.
  51. %this.createBalls();
  52. }
  53. //-----------------------------------------------------------------------------
  54. function CollisionToy::createBackground( %this )
  55. {
  56. // Create the sprite.
  57. %object = new Sprite();
  58. // Set the sprite as "static" so it is not affected by gravity.
  59. %object.BodyType = static;
  60. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  61. // Set the position.
  62. %object.Position = "0 0";
  63. // Set the size.
  64. %object.Size = "100 75";
  65. // Set to the furthest background layer.
  66. %object.SceneLayer = 31;
  67. // Set the scroller to use an animation!
  68. %object.Image = "ToyAssets:highlightBackground";
  69. // Set the blend color.
  70. %object.BlendColor = SlateGray;
  71. // Create border collisions.
  72. %object.createEdgeCollisionShape( -50, -37.5, -50, 37.5 );
  73. %object.createEdgeCollisionShape( 50, -37.5, 50, 37.5 );
  74. %object.createEdgeCollisionShape( -50, 37.5, 50, 37.5 );
  75. %object.createEdgeCollisionShape( -50, -34.5, 50, -34.5 );
  76. // Add the sprite to the scene.
  77. SandboxScene.add( %object );
  78. }
  79. //-----------------------------------------------------------------------------
  80. function CollisionToy::createBlockers( %this )
  81. {
  82. // Create blockers.
  83. for( %n = 0; %n < CollisionToy.MaxBlockers; %n++ )
  84. {
  85. // Choose a uniform area.
  86. %sizeX = getRandom(1, 9);
  87. %sizeY = 10 - %sizeX;
  88. // Create sprite.
  89. %object = new Sprite();
  90. %object.BodyType = static;
  91. %object.Position = getRandom( -40, 40 ) SPC getRandom( -30, 30 );
  92. %object.Layer = 30;
  93. %object.Size = %sizeX SPC %sizeY;
  94. %object.createPolygonBoxCollisionShape();
  95. %object.Image = "ToyAssets:Blocks";
  96. %object.Frame = getRandom(0,55);
  97. SandboxScene.add( %object );
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. function CollisionToy::createBalls( %this )
  102. {
  103. // Create balls.
  104. for( %n = 0; %n < CollisionToy.MaxBalls; %n++ )
  105. {
  106. // Create the sprite.
  107. %object = new Sprite()
  108. {
  109. class = "CollisionToyBall";
  110. };
  111. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  112. // Set the position.
  113. %object.Position = getRandom(-30,30) SPC getRandom(-30,30);
  114. // If the size is to be square then we can simply pass a single value.
  115. // This applies to any 'Vector2' engine type.
  116. %object.Size = 3;
  117. // Set the layer.
  118. %object.Layer = 20;
  119. // Create a circle collision shape.
  120. %object.setDefaultRestitution( 1 );
  121. %object.setDefaultFriction( 0.1 );
  122. %object.createCircleCollisionShape( 1.5 );
  123. %object.CollisionCallback = true;
  124. // Set the sprite to use an image. This is known as "static" image mode.
  125. %object.Image = "ToyAssets:Football";
  126. // We don't really need to do this as the frame is set to zero by default.
  127. %object.Frame = 0;
  128. // Set velocities.
  129. %object.SetLinearVelocity( getRandom(-40,40) SPC getRandom(-30,30) );
  130. %object.SetAngularVelocity( getRandom(-360,360) );
  131. // Add the sprite to the scene.
  132. SandboxScene.add( %object );
  133. }
  134. }
  135. //-----------------------------------------------------------------------------
  136. function CollisionToyBall::onCollision(%this, %object, %collisionDetails)
  137. {
  138. // Finish if there are no contact points (this happens with sensors).
  139. if ( %collisionDetails.count <= 2 )
  140. return;
  141. // Fetch the first contact point (there may possibly be two but ignore that here).
  142. %contactPosition = %collisionDetails._4 SPC %collisionDetails._5;
  143. // Create a marker sprite with a limited lifetime.
  144. // Also set this so that it can't be picked so you can't drag it via the Sandbox "pull" feature.
  145. %object = new Sprite();
  146. %object.Position = %contactPosition;
  147. %object.Layer = 10;
  148. %object.Size = 3;
  149. %object.Image = "ToyAssets:Crosshair2";
  150. %object.BlendColor = LimeGreen;
  151. %object.AngularVelocity = -180;
  152. %object.Lifetime = 3;
  153. %object.PickingAllowed = false;
  154. SandboxScene.add( %object );
  155. }
  156. //-----------------------------------------------------------------------------
  157. function CollisionToy::setMaxBlockers(%this, %value)
  158. {
  159. %this.MaxBlockers = %value;
  160. }
  161. //-----------------------------------------------------------------------------
  162. function CollisionToy::setMaxBalls(%this, %value)
  163. {
  164. %this.MaxBalls = %value;
  165. }