main.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 PointForceControllerToy::create( %this )
  23. {
  24. // Activate the package.
  25. activatePackage( PointForceControllerToyPackage );
  26. // Set the sandbox drag mode availability.
  27. Sandbox.allowManipulation( pull );
  28. // Set the manipulation mode.
  29. Sandbox.useManipulation( pull );
  30. // Initialize the toys settings.
  31. PointForceControllerToy.autoSpawnAsteroids = false;
  32. PointForceControllerToy.showPlanetoid = true;
  33. PointForceControllerToy.showExplosions = true;
  34. PointForceControllerToy.nonLinearController = true;
  35. PointForceControllerToy.controllerForce = 35;
  36. PointForceControllerToy.controllerRadius = 36;
  37. PointForceControllerToy.controllerLinearDrag = 0.1;
  38. PointForceControllerToy.controllerAngularDrag = 0;
  39. PointForceControllerToy.planetoidSize = 26;
  40. PointForceControllerToy.asteroidSize = 4;
  41. PointForceControllerToy.asteroidDensity = 0.2;
  42. PointForceControllerToy.asteroidLifetime = 10;
  43. PointForceControllerToy.asteroidSpeed = 30;
  44. // Add the custom controls.
  45. addFlagOption("Auto Spawn Asteroids", "setAutoSpawnAsteroids", PointForceControllerToy.autoSpawnAsteroids, true, "Whether to auto-spawn asteroids or not." );
  46. addFlagOption("Show Planetoid", "setShowPlanetoid", PointForceControllerToy.showPlanetoid, true, "Whether to show the planetoid or not or not." );
  47. addFlagOption("Show Explosions", "setShowExplosions", PointForceControllerToy.showExplosions, false, "Whether to show the explosions or not or not." );
  48. addFlagOption("Controller Non-Linear", "setNonLinearController", PointForceControllerToy.nonLinearController, true, " Whether to apply the controller force non-linearly (using the inverse square law) or linearly" );
  49. addNumericOption("Controller Force", -1000, 1000, 10, "setControllerForce", PointForceControllerToy.controllerForce, true, "Sets the controller force.");
  50. addNumericOption("Controller Radius", 1, 30, 1, "setControllerRadius", PointForceControllerToy.controllerRadius, true, "Sets the controller radius.");
  51. addNumericOption("Controller Linear Drag", 0, 1, 0.1, "setControllerLinearDrag", PointForceControllerToy.controllerLinearDrag, true, "Sets the controller linear drag.");
  52. addNumericOption("Controller Angular Drag", 0, 1, 0.1, "setControllerAngularDrag", PointForceControllerToy.controllerAngularDrag, true, "Sets the controller angular drag.");
  53. addNumericOption("Planetoid Size", 1, 30, 1, "setPlanetoidSize", PointForceControllerToy.planetoidSize, true, "Sets the planetoid size.");
  54. addNumericOption("Asteroid Size", 1, 10, 1, "setAsteroidSize", PointForceControllerToy.asteroidSize, true, "Sets the asteroid size.");
  55. addNumericOption("Asteroid Density", 1, 10, 1, "setAsteroidDensity", PointForceControllerToy.asteroidDensity, true, "Sets the asteroid density.");
  56. addNumericOption("Asteroid Lifetime", 1, 10, 1, "setAsteroidLifetime", PointForceControllerToy.asteroidLifetime, true, "Sets the asteroid lifetime.");
  57. addNumericOption("Asteroid Speed", 1, 100, 1, "setAsteroidSpeed", PointForceControllerToy.asteroidSpeed, true, "Sets the asteroid speed.");
  58. // Reset the toy.
  59. PointForceControllerToy.reset();
  60. }
  61. //-----------------------------------------------------------------------------
  62. function PointForceControllerToy::destroy( %this )
  63. {
  64. // Deactivate the package.
  65. deactivatePackage( PointForceControllerToyPackage );
  66. }
  67. //-----------------------------------------------------------------------------
  68. function PointForceControllerToy::reset( %this )
  69. {
  70. // Clear the scene.
  71. SandboxScene.clear();
  72. // Create background.
  73. %this.createBackground();
  74. // Create the planetoid.
  75. %this.createPlanetoid();
  76. // Start a timer throwing asteroids.
  77. if ( PointForceControllerToy.autoSpawnAsteroids )
  78. %this.startTimer( "createAsteroid", 1000 );
  79. }
  80. //-----------------------------------------------------------------------------
  81. function PointForceControllerToy::createBackground( %this )
  82. {
  83. // Create the sprite.
  84. %object = new Scroller();
  85. %object.BodyType = static;
  86. %object.Size = "200 150";
  87. %object.SceneLayer = 31;
  88. %object.Image = "ToyAssets:SkyBackground";
  89. %object.ScrollX = 2;
  90. SandboxScene.add( %object );
  91. }
  92. //-----------------------------------------------------------------------------
  93. function PointForceControllerToy::createPlanetoid( %this )
  94. {
  95. if ( PointForceControllerToy.showPlanetoid )
  96. {
  97. // Create the planetoid.
  98. %object = new Sprite()
  99. {
  100. class = "Planetoid";
  101. };
  102. //%object.BodyType = static;
  103. %object.Size = PointForceControllerToy.planetoidSize;
  104. %object.Image = "ToyAssets:Planetoid";
  105. %object.AngularVelocity = -5;
  106. %object.setDefaultDensity( 10000 );
  107. %object.createCircleCollisionShape( PointForceControllerToy.planetoidSize * 0.48 );
  108. %object.CollisionCallback = true;
  109. SandboxScene.add( %object );
  110. }
  111. // Create planetoid bubble.
  112. %player = new ParticlePlayer();
  113. %player.BodyType = static;
  114. %player.Particle = "ToyAssets:ForceBubble";
  115. %player.SceneLayer = 0;
  116. SandboxScene.add( %player );
  117. // Create a new controller.
  118. %controller = new PointForceController();
  119. %controller.setControlLayers( 10 ); // Only affect asteroids.
  120. %controller.Radius = PointForceControllerToy.controllerRadius;
  121. %controller.Force = PointForceControllerToy.ControllerForce;
  122. %controller.NonLinear = PointForceControllerToy.nonLinearController;
  123. %controller.LinearDrag = PointForceControllerToy.controllerLinearDrag;
  124. %controller.AngularDrag = PointForceControllerToy.controllerAngularDrag;
  125. SandboxScene.Controllers.add( %controller );
  126. // This is so we can reference it in the toy, no other reason.
  127. PointForceControllerToy.Controller = %controller;
  128. }
  129. //-----------------------------------------------------------------------------
  130. function Planetoid::onCollision( %this, %object, %collisionDetails )
  131. {
  132. // Are we showing explosions?
  133. if ( PointForceControllerToy.showExplosions )
  134. {
  135. // Yes, so calculate position angle.
  136. %positionDelta = Vector2Sub( %object.Position, %this.Position );
  137. %angle = -mRadToDeg( mAtan( %positionDelta._0, %positionDelta._1 ) );
  138. // Fetch contact position.
  139. %contactPosition = %collisionDetails._4 SPC %collisionDetails._5;
  140. // Calculate total impact force.
  141. %impactForce = mAbs(%collisionDetails._6 / 100) + mAbs(%collisionDetails._7 / 20);
  142. // Create explosion.
  143. %player = new ParticlePlayer();
  144. %player.BodyType = static;
  145. %player.Particle = "ToyAssets:impactExplosion";
  146. %player.Position = %contactPosition;
  147. %player.Angle = %angle;
  148. %player.SizeScale = mClamp( %impactForce, 0.1, 10 );
  149. %player.SceneLayer = 0;
  150. SandboxScene.add( %player );
  151. }
  152. // Delete the asteroid.
  153. %object.Trail.LinearVelocity = 0;
  154. %object.Trail.AngularVelocity = 0;
  155. %object.Trail.safeDelete();
  156. %object.safeDelete();
  157. }
  158. //-----------------------------------------------------------------------------
  159. function PointForceControllerToy::createAsteroid( %this, %position )
  160. {
  161. // Create an asteroid.
  162. %object = new Sprite();
  163. %object.Position = %position !$= "" ? %position : -40 SPC getRandom(-35,35);
  164. %object.Size = PointForceControllerToy.asteroidSize;
  165. %object.Image = "ToyAssets:Asteroids";
  166. %object.ImageFrame = getRandom(0,3);
  167. %object.SceneLayer = 10;
  168. %object.setDefaultDensity( PointForceControllerToy.asteroidDensity );
  169. %object.createCircleCollisionShape( PointForceControllerToy.asteroidSize * 0.4 );
  170. %object.setLinearVelocity( PointForceControllerToy.asteroidSpeed, 0 );
  171. %object.setAngularVelocity( getRandom(-90,90) );
  172. %object.setLifetime( PointForceControllerToy.asteroidLifetime );
  173. SandboxScene.add( %object );
  174. // Create fire trail.
  175. %player = new ParticlePlayer();
  176. %player.Particle = "ToyAssets:bonfire";
  177. %player.Position = %object.Position;
  178. %player.EmissionRateScale = 3;
  179. %player.SizeScale = 2;
  180. %player.SceneLayer = 11;
  181. %player.setLifetime( PointForceControllerToy.asteroidLifetime );
  182. SandboxScene.add( %player );
  183. %jointId = SandboxScene.createRevoluteJoint( %object, %player );
  184. SandboxScene.setRevoluteJointLimit( %jointId, 0, 0 );
  185. %object.Trail = %player;
  186. return %object;
  187. }
  188. //-----------------------------------------------------------------------------
  189. function PointForceControllerToy::setAutoSpawnAsteroids( %this, %value )
  190. {
  191. %this.autoSpawnAsteroids = %value;
  192. }
  193. //-----------------------------------------------------------------------------
  194. function PointForceControllerToy::setShowPlanetoid( %this, %value )
  195. {
  196. %this.showPlanetoid = %value;
  197. }
  198. //-----------------------------------------------------------------------------
  199. function AngryBirdsSpaceToy::setShowExplosions( %this, %value )
  200. {
  201. %this.showExplosions = %value;
  202. }
  203. //-----------------------------------------------------------------------------
  204. function PointForceControllerToy::setNonLinearController( %this, %value )
  205. {
  206. %this.nonLinearController = %value;
  207. }
  208. //-----------------------------------------------------------------------------
  209. function PointForceControllerToy::setControllerForce( %this, %value )
  210. {
  211. %this.controllerForce = %value;
  212. }
  213. //-----------------------------------------------------------------------------
  214. function PointForceControllerToy::setControllerRadius( %this, %value )
  215. {
  216. %this.controllerRadius = %value;
  217. }
  218. //-----------------------------------------------------------------------------
  219. function PointForceControllerToy::setControllerLinearDrag( %this, %value )
  220. {
  221. %this.controllerLinearDrag = %value;
  222. }
  223. //-----------------------------------------------------------------------------
  224. function PointForceControllerToy::setControllerAngularDrag( %this, %value )
  225. {
  226. %this.controllerAngularDrag = %value;
  227. }
  228. //-----------------------------------------------------------------------------
  229. function PointForceControllerToy::setPlanetoidSize( %this, %value )
  230. {
  231. %this.planetoidSize = %value;
  232. }
  233. //-----------------------------------------------------------------------------
  234. function PointForceControllerToy::setAsteroidSize( %this, %value )
  235. {
  236. %this.asteroidSize = %value;
  237. }
  238. //-----------------------------------------------------------------------------
  239. function PointForceControllerToy::setAsteroidDensity( %this, %value )
  240. {
  241. %this.asteroidDensity = %value;
  242. }
  243. //-----------------------------------------------------------------------------
  244. function PointForceControllerToy::setAsteroidLifetime( %this, %value )
  245. {
  246. %this.asteroidLifetime = %value;
  247. }
  248. //-----------------------------------------------------------------------------
  249. function PointForceControllerToy::setAsteroidSpeed( %this, %value )
  250. {
  251. %this.asteroidSpeed = %value;
  252. }
  253. //-----------------------------------------------------------------------------
  254. package PointForceControllerToyPackage
  255. {
  256. function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
  257. {
  258. // Call parent.
  259. Parent::onTouchDown(%this, %touchID, %worldPosition );
  260. // Create an asteroid.
  261. %object = PointForceControllerToy.createAsteroid( %worldPosition );
  262. if ( %worldPosition.x < PointForceControllerToy.Controller.Position.x )
  263. %object.setLinearVelocity( PointForceControllerToy.asteroidSpeed, 0 );
  264. else
  265. %object.setLinearVelocity( -PointForceControllerToy.asteroidSpeed, 0 );
  266. }
  267. };