main.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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", 0.1, 10, 0.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. else
  80. %this.stopTimer();
  81. }
  82. //-----------------------------------------------------------------------------
  83. function PointForceControllerToy::createBackground( %this )
  84. {
  85. // Create the sprite.
  86. %object = new Scroller();
  87. %object.BodyType = static;
  88. %object.Size = "200 150";
  89. %object.SceneLayer = 31;
  90. %object.Image = "ToyAssets:SkyBackground";
  91. %object.ScrollX = 2;
  92. SandboxScene.add( %object );
  93. }
  94. //-----------------------------------------------------------------------------
  95. function PointForceControllerToy::createPlanetoid( %this )
  96. {
  97. // Choose a position for the planetoid.
  98. %position = 0;
  99. if ( PointForceControllerToy.showPlanetoid )
  100. {
  101. // Create the planetoid.
  102. %object = new Sprite()
  103. {
  104. class = "Planetoid";
  105. };
  106. %object.Position = %position;
  107. %object.Size = PointForceControllerToy.planetoidSize;
  108. %object.Image = "ToyAssets:Planetoid";
  109. %object.AngularVelocity = -5;
  110. %object.setDefaultDensity( 10000 );
  111. %object.createCircleCollisionShape( PointForceControllerToy.planetoidSize * 0.48 );
  112. %object.CollisionCallback = true;
  113. SandboxScene.add( %object );
  114. }
  115. // Create planetoid bubble.
  116. %player = new ParticlePlayer();
  117. %player.BodyType = static;
  118. %player.Position = %position;
  119. %player.Particle = "ToyAssets:ForceBubble";
  120. %player.SceneLayer = 0;
  121. SandboxScene.add( %player );
  122. // Create a new controller.
  123. %controller = new PointForceController();
  124. %controller.setControlLayers( 10 ); // Only affect asteroids.
  125. %controller.Radius = PointForceControllerToy.controllerRadius;
  126. %controller.Force = PointForceControllerToy.ControllerForce;
  127. %controller.NonLinear = PointForceControllerToy.nonLinearController;
  128. %controller.LinearDrag = PointForceControllerToy.controllerLinearDrag;
  129. %controller.AngularDrag = PointForceControllerToy.controllerAngularDrag;
  130. SandboxScene.Controllers.add( %controller );
  131. if ( isObject(%object) )
  132. %controller.setTrackedObject( %object );
  133. else
  134. %controller.Position = %position;
  135. // This is so we can reference it in the toy, no other reason.
  136. PointForceControllerToy.Controller = %controller;
  137. }
  138. //-----------------------------------------------------------------------------
  139. function Planetoid::onCollision( %this, %object, %collisionDetails )
  140. {
  141. // Are we showing explosions?
  142. if ( PointForceControllerToy.showExplosions )
  143. {
  144. // Yes, so calculate position angle.
  145. %positionDelta = Vector2Sub( %object.Position, %this.Position );
  146. %angle = -mRadToDeg( mAtan( %positionDelta._0, %positionDelta._1 ) );
  147. // Fetch contact position.
  148. %contactPosition = %collisionDetails._4 SPC %collisionDetails._5;
  149. // Calculate total impact force.
  150. %impactForce = mAbs(%collisionDetails._6 / 100) + mAbs(%collisionDetails._7 / 20);
  151. // Create explosion.
  152. %player = new ParticlePlayer();
  153. %player.BodyType = static;
  154. %player.Particle = "ToyAssets:impactExplosion";
  155. %player.Position = %contactPosition;
  156. %player.Angle = %angle;
  157. %player.SizeScale = mClamp( %impactForce, 0.1, 10 );
  158. %player.SceneLayer = 0;
  159. SandboxScene.add( %player );
  160. }
  161. // Delete the asteroid.
  162. %object.Trail.LinearVelocity = 0;
  163. %object.Trail.AngularVelocity = 0;
  164. %object.Trail.safeDelete();
  165. %object.safeDelete();
  166. }
  167. //-----------------------------------------------------------------------------
  168. function PointForceControllerToy::createAsteroid( %this, %position )
  169. {
  170. // Create an asteroid.
  171. %object = new Sprite();
  172. %object.Position = %position !$= "" ? %position : -40 SPC getRandom(-35,35);
  173. %object.Size = PointForceControllerToy.asteroidSize;
  174. %object.Image = "ToyAssets:Asteroids";
  175. %object.ImageFrame = getRandom(0,3);
  176. %object.SceneLayer = 10;
  177. %object.setDefaultDensity( PointForceControllerToy.asteroidDensity );
  178. %object.createCircleCollisionShape( PointForceControllerToy.asteroidSize * 0.4 );
  179. %object.setLinearVelocity( PointForceControllerToy.asteroidSpeed, 0 );
  180. %object.setAngularVelocity( getRandom(-90,90) );
  181. %object.setLifetime( PointForceControllerToy.asteroidLifetime );
  182. SandboxScene.add( %object );
  183. // Create fire trail.
  184. %player = new ParticlePlayer();
  185. %player.Particle = "ToyAssets:bonfire";
  186. %player.Position = %object.Position;
  187. %player.EmissionRateScale = 3;
  188. %player.SizeScale = 2;
  189. %player.SceneLayer = 11;
  190. %player.setLifetime( PointForceControllerToy.asteroidLifetime );
  191. SandboxScene.add( %player );
  192. %jointId = SandboxScene.createRevoluteJoint( %object, %player );
  193. SandboxScene.setRevoluteJointLimit( %jointId, 0, 0 );
  194. %object.Trail = %player;
  195. return %object;
  196. }
  197. //-----------------------------------------------------------------------------
  198. function PointForceControllerToy::setAutoSpawnAsteroids( %this, %value )
  199. {
  200. %this.autoSpawnAsteroids = %value;
  201. }
  202. //-----------------------------------------------------------------------------
  203. function PointForceControllerToy::setShowPlanetoid( %this, %value )
  204. {
  205. %this.showPlanetoid = %value;
  206. }
  207. //-----------------------------------------------------------------------------
  208. function AngryBirdsSpaceToy::setShowExplosions( %this, %value )
  209. {
  210. %this.showExplosions = %value;
  211. }
  212. //-----------------------------------------------------------------------------
  213. function PointForceControllerToy::setNonLinearController( %this, %value )
  214. {
  215. %this.nonLinearController = %value;
  216. }
  217. //-----------------------------------------------------------------------------
  218. function PointForceControllerToy::setControllerForce( %this, %value )
  219. {
  220. %this.controllerForce = %value;
  221. }
  222. //-----------------------------------------------------------------------------
  223. function PointForceControllerToy::setControllerRadius( %this, %value )
  224. {
  225. %this.controllerRadius = %value;
  226. }
  227. //-----------------------------------------------------------------------------
  228. function PointForceControllerToy::setControllerLinearDrag( %this, %value )
  229. {
  230. %this.controllerLinearDrag = %value;
  231. }
  232. //-----------------------------------------------------------------------------
  233. function PointForceControllerToy::setControllerAngularDrag( %this, %value )
  234. {
  235. %this.controllerAngularDrag = %value;
  236. }
  237. //-----------------------------------------------------------------------------
  238. function PointForceControllerToy::setPlanetoidSize( %this, %value )
  239. {
  240. %this.planetoidSize = %value;
  241. }
  242. //-----------------------------------------------------------------------------
  243. function PointForceControllerToy::setAsteroidSize( %this, %value )
  244. {
  245. %this.asteroidSize = %value;
  246. }
  247. //-----------------------------------------------------------------------------
  248. function PointForceControllerToy::setAsteroidDensity( %this, %value )
  249. {
  250. %this.asteroidDensity = %value;
  251. }
  252. //-----------------------------------------------------------------------------
  253. function PointForceControllerToy::setAsteroidLifetime( %this, %value )
  254. {
  255. %this.asteroidLifetime = %value;
  256. }
  257. //-----------------------------------------------------------------------------
  258. function PointForceControllerToy::setAsteroidSpeed( %this, %value )
  259. {
  260. %this.asteroidSpeed = %value;
  261. }
  262. //-----------------------------------------------------------------------------
  263. package PointForceControllerToyPackage
  264. {
  265. function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
  266. {
  267. // Call parent.
  268. Parent::onTouchDown(%this, %touchID, %worldPosition );
  269. // Create an asteroid.
  270. %object = PointForceControllerToy.createAsteroid( %worldPosition );
  271. if ( %worldPosition.x < PointForceControllerToy.Controller.Position.x )
  272. %object.setLinearVelocity( PointForceControllerToy.asteroidSpeed, 0 );
  273. else
  274. %object.setLinearVelocity( -PointForceControllerToy.asteroidSpeed, 0 );
  275. }
  276. };