main.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 AmbientForceControllerToy::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. // Configure settings.
  30. AmbientForceControllerToy.ForceAngle = 45;
  31. AmbientForceControllerToy.ForceMagnitude = 50;
  32. AmbientForceControllerToy.DebrisCount = 100;
  33. // Add options.
  34. addNumericOption("Force Angle", -359, 359, 1, "setForceAngle", AmbientForceControllerToy.ForceAngle, false, "The angle of the ambient force.");
  35. addNumericOption("Force Magnitude", 0, 1000, 10, "setForceMagnitude", AmbientForceControllerToy.ForceMagnitude, false, "The magnitude of the ambient force.");
  36. addNumericOption("Debris Count", 10, 1000, 10, "setDebrisCount", AmbientForceControllerToy.DebrisCount, true, "The amount of debris affected by the ambient force controller.");
  37. // Reset the toy.
  38. AmbientForceControllerToy.reset();
  39. }
  40. //-----------------------------------------------------------------------------
  41. function AmbientForceControllerToy::destroy( %this )
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. function AmbientForceControllerToy::reset( %this )
  46. {
  47. // Clear the scene.
  48. SandboxScene.clear();
  49. // Create background.
  50. %this.createBackground();
  51. // Create ambient force controller.
  52. %this.createAmbientForceController();
  53. }
  54. //-----------------------------------------------------------------------------
  55. function AmbientForceControllerToy::createBackground( %this )
  56. {
  57. // Create the sprite.
  58. %object = new Sprite();
  59. // Set the sprite as "static" so it is not affected by gravity.
  60. %object.setBodyType( static );
  61. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  62. // Set the position.
  63. %object.Position = "0 0";
  64. // Set the size.
  65. %object.Size = "100 75";
  66. // Set to the furthest background layer.
  67. %object.SceneLayer = 31;
  68. // Set the scroller to use an animation!
  69. %object.Image = "ToyAssets:highlightBackground";
  70. // Set the blend color.
  71. %object.BlendColor = DarkGray;
  72. // Create border collisions.
  73. %object.createEdgeCollisionShape( -50, -37.5, -50, 37.5 );
  74. %object.createEdgeCollisionShape( 50, -37.5, 50, 37.5 );
  75. %object.createEdgeCollisionShape( -50, 37.5, 50, 37.5 );
  76. %object.createEdgeCollisionShape( -50, -34.5, 50, -34.5 );
  77. // Add the sprite to the scene.
  78. SandboxScene.add( %object );
  79. }
  80. //-----------------------------------------------------------------------------
  81. function AmbientForceControllerToy::createSprite( %this, %asset, %position, %size, %angle, %blendColor )
  82. {
  83. // Create the sprite.
  84. %object = new Sprite();
  85. // Set the position.
  86. %object.Position = %position;
  87. // Set the size.
  88. %object.Size = %size;
  89. // Set the angle.
  90. %object.Angle = %angle;
  91. // Set the scroller to use an animation!
  92. %object.Image = %asset;
  93. // Set the blend color.
  94. %object.BlendColor = %blendColor $= "" ? White : %blendColor;
  95. // Add the sprite to the scene.
  96. SandboxScene.add( %object );
  97. return %object;
  98. }
  99. //-----------------------------------------------------------------------------
  100. function AmbientForceControllerToy::createAmbientForceController( %this )
  101. {
  102. // Create a new controller.
  103. %controller = new AmbientForceController();
  104. // Set scene controller.
  105. AmbientForceControllerToy.SceneController = %controller;
  106. // Update the ambient force controller.
  107. %this.updateAmbientForceController();
  108. // Add the controller.
  109. SandboxScene.Controllers.add( %controller );
  110. // Create some sprites used by the controller.
  111. for( %n = 0; %n < AmbientForceControllerToy.DebrisCount; %n++ )
  112. {
  113. %sizeX = getRandom(1,4);
  114. %sizeY = getRandom(1,4);
  115. %size = %sizeX SPC %sizeY;
  116. // Create some sprites.
  117. %sprite = %this.createSprite( "ToyAssets:tiles", getRandom(-40,40) SPC getRandom(-20,20), %size, getRandom(0,360), White );
  118. %sprite.Frame = getRandom(0,15);
  119. %sprite.createPolygonBoxCollisionShape( %sizeX, %sizeY );
  120. %sprite.setAngularVelocity(getRandom(-180,180));
  121. // Add to the controller.
  122. %controller.add( %sprite );
  123. }
  124. // Create some sprites NOT used by the controller.
  125. for( %n = 0; %n < 10; %n++ )
  126. {
  127. %size = getRandom(3,4);
  128. // Create some sprites.
  129. %sprite = %this.createSprite( "ToyAssets:football", getRandom(-40,40) SPC getRandom(-20,20), %size, getRandom(0,360), White );
  130. %sprite.createCircleCollisionShape( %size * 0.5 );
  131. %sprite.setAngularVelocity(getRandom(-180,180));
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. function AmbientForceControllerToy::updateAmbientForceController( %this )
  136. {
  137. // Calculate the force.
  138. AmbientForceControllerToy.SceneController.Force = Vector2Direction( %this.ForceAngle, %this.ForceMagnitude );
  139. }
  140. //-----------------------------------------------------------------------------
  141. function AmbientForceControllerToy::setForceAngle(%this, %value)
  142. {
  143. %this.ForceAngle = %value;
  144. // Update the ambient force.
  145. %this.updateAmbientForceController();
  146. }
  147. //-----------------------------------------------------------------------------
  148. function AmbientForceControllerToy::setForceMagnitude(%this, %value)
  149. {
  150. %this.ForceMagnitude = %value;
  151. // Update the ambient force.
  152. %this.updateAmbientForceController();
  153. }
  154. //-----------------------------------------------------------------------------
  155. function AmbientForceControllerToy::setDebrisCount(%this, %value)
  156. {
  157. %this.DebrisCount = %value;
  158. }