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