main.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 BuoyancyControllerToy::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. // Set gravity.
  30. SandboxScene.setGravity( 0, -29.8 );
  31. // Configure settings.
  32. BuoyancyControllerToy.FluidArea = "-30 -34 30 0";
  33. BuoyancyControllerToy.FluidDensity = 2.5;
  34. BuoyancyControllerToy.LinearDrag = 3;
  35. BuoyancyControllerToy.AngularDrag = 3;
  36. BuoyancyControllerToy.FluidGravity = "0 -29.8";
  37. BuoyancyControllerToy.UseShapeDensity = false;
  38. BuoyancyControllerToy.FlowMagnitude = 13;
  39. BuoyancyControllerToy.MaxDebrisCount = 50;
  40. BuoyancyControllerToy.MinDebrisDensity = 1;
  41. BuoyancyControllerToy.MaxDebrisDensity = 1;
  42. BuoyancyControllerToy.GroundWidth = 140;
  43. // Add options.
  44. addNumericOption("Fluid Density", 0, 10, 0.1, "setFluidDensity", BuoyancyControllerToy.FluidDensity, false, "The fluid density.");
  45. addNumericOption("Fluid Flow Magnitude", 0, 1000, 10, "setFluidFlowMagnitude", BuoyancyControllerToy.FlowMagnitude, true, "The magnitude of the constant force.");
  46. addNumericOption("Linear Drag", 0, 10, 0.1, "setLinearDrag", BuoyancyControllerToy.LinearDrag, false, "The linear drag co-efficient for the fluid.");
  47. addNumericOption("Angular Drag", 0, 10, 0.1, "setAngularDrag", BuoyancyControllerToy.AngularDrag, false, "The angular drag co-efficient for the fluid.");
  48. addNumericOption("Maximum Debris Count", 1, 500, 1, "setMaxDebrisCount", BuoyancyControllerToy.MaxDebrisCount, true, "The maximum number of debris to create.");
  49. addNumericOption("Minimum Debris Density", 0, 10, 0.1, "setMinDebrisDensity", BuoyancyControllerToy.MinDebrisDensity, true, "The minimum density to randomly choose for the debris.");
  50. addNumericOption("Maximum Debris Density", 0, 10, 0.1, "setMaxDebrisDensity", BuoyancyControllerToy.MaxDebrisDensity, true, "The maximum density to randomly choose for the debris.");
  51. addFlagOption("Use Collision Shape Densities", "setUseShapeDensity", BuoyancyControllerToy.UseShapeDensity, false, "Whether to use the collision shape densities or assume a uniform density." );
  52. // Reset the toy.
  53. BuoyancyControllerToy.reset();
  54. }
  55. //-----------------------------------------------------------------------------
  56. function BuoyancyControllerToy::destroy( %this )
  57. {
  58. }
  59. //-----------------------------------------------------------------------------
  60. function BuoyancyControllerToy::reset( %this )
  61. {
  62. // Clear the scene.
  63. SandboxScene.clear();
  64. // Calculate fluid bounds to help layout only.
  65. BuoyancyControllerToy.FluidLeft = BuoyancyControllerToy.FluidArea._0;
  66. BuoyancyControllerToy.FluidBottom = BuoyancyControllerToy.FluidArea._1;
  67. BuoyancyControllerToy.FluidRight = BuoyancyControllerToy.FluidArea._2;
  68. BuoyancyControllerToy.FluidTop = BuoyancyControllerToy.FluidArea._3;
  69. // Create a background.
  70. %this.createBackground();
  71. // Create fluid.
  72. %this.createFluid( "-50 20 30 30", "1 0", Navy );
  73. %this.createFluid( "-10 2 50 12", "-1 0", Cyan );
  74. %this.createFluid("-40 -16 30 -6", "1 0", LightBlue );
  75. %this.createFluid( "-50 -34 50 -24", "-1 0", Green );
  76. %this.createFluid( "-50 -34 -40 30", "0 0", Blue );
  77. // Update the buoyancy controllers.
  78. %this.updateBuoyancyControllers();
  79. // Create debris.
  80. %this.createDebris();
  81. }
  82. //-----------------------------------------------------------------------------
  83. function BuoyancyControllerToy::createBackground( %this )
  84. {
  85. // Create the sprite.
  86. %object = new Sprite();
  87. %object.BodyType = static;
  88. %object.Position = "0 0";
  89. %object.Size = "100 75";
  90. %object.SceneLayer = 31;
  91. %object.Image = "ToyAssets:highlightBackground";
  92. %object.BlendColor = SlateGray;
  93. SandboxScene.add( %object );
  94. %object.setDefaultFriction( 0 );
  95. // Create border collisions.
  96. %object.createEdgeCollisionShape( -50, -37.5, -50, 100 );
  97. %object.createEdgeCollisionShape( 50, -37.5, 50, 100 );
  98. %object.createEdgeCollisionShape( -50, -34, 50, -34 );
  99. // Vertical fluid edge.
  100. %object.createEdgeCollisionShape( -39.8, -16.2, -39.8, 20 );
  101. %object.createEdgeCollisionShape( -39.8, -16.2, -20, -16.2 );
  102. // Add the sprite to the scene.
  103. SandboxScene.add( %object );
  104. }
  105. //-----------------------------------------------------------------------------
  106. function BuoyancyControllerToy::createSprite( %this, %asset, %position, %size, %angle )
  107. {
  108. // Create the sprite.
  109. %object = new Sprite();
  110. // Set the position.
  111. %object.Position = %position;
  112. // Set the size.
  113. %object.Size = %size;
  114. // Set the angle.
  115. %object.Angle = %angle;
  116. // Set the scroller to use an animation!
  117. %object.Image = %asset;
  118. // Set the scene layer.
  119. %object.SceneLayer = 10;
  120. // Add the sprite to the scene.
  121. SandboxScene.add( %object );
  122. return %object;
  123. }
  124. //-----------------------------------------------------------------------------
  125. function BuoyancyControllerToy::createDebris( %this )
  126. {
  127. // Fetch the debris count.
  128. %debrisCount = BuoyancyControllerToy.MaxDebrisCount;
  129. for( %debris = 0; %debris < %debrisCount; %debris++ )
  130. {
  131. // Choose a random debris (polygon or circle shapes).
  132. if ( getRandom(0,100) < 10 )
  133. {
  134. %size = 2;
  135. // Create some sprites.
  136. %sprite = %this.createSprite( "ToyAssets:football", getRandom(-45,20) SPC getRandom(40,50), %size, getRandom(0,360) );
  137. %sprite.setDefaultFriction( 0.5 );
  138. %sprite.setDefaultDensity( getRandom(%this.MinDebrisDensity, %this.MaxDebrisDensity) );
  139. %sprite.createCircleCollisionShape( %size * 0.5 );
  140. %sprite.setAngularVelocity(getRandom(-180,180));
  141. }
  142. else
  143. {
  144. %sizeX = getRandom(1,5);
  145. %sizeY = getRandom(1,2);
  146. %size = %sizeX SPC %sizeY;
  147. // Create some sprites.
  148. %sprite = %this.createSprite( "ToyAssets:Blocks", getRandom(-45,20) SPC getRandom(40,50), %size, getRandom(0,360) );
  149. %sprite.Frame = getRandom( 0, 55 );
  150. %sprite.setDefaultFriction( 0.5 );
  151. %sprite.setDefaultDensity( getRandom(%this.MinDebrisDensity, %this.MaxDebrisDensity) );
  152. %sprite.createPolygonBoxCollisionShape( %sizeX, %sizeY );
  153. %sprite.setAngularVelocity(getRandom(-180,180));
  154. }
  155. }
  156. }
  157. //-----------------------------------------------------------------------------
  158. function BuoyancyControllerToy::createFluid( %this, %area, %flowVelocity, %color )
  159. {
  160. %areaLeft = %area._0;
  161. %areaBottom = %area._1;
  162. %areaRight = %area._2;
  163. %areaTop = %area._3;
  164. %crestY = %areaTop - 3;
  165. %width = %areaRight - %areaLeft;
  166. // Create a new controller.
  167. %controller = new BuoyancyController();
  168. %controller.FluidArea = %area;
  169. %controller.FlowVelocity = %flowVelocity;
  170. SandboxScene.Controllers.add( %controller );
  171. // Add the water.
  172. %water = new Sprite();
  173. %water.BodyType = static;
  174. %water.setArea( %areaLeft SPC %areaBottom SPC %areaRight SPC %crestY);
  175. %water.Image = "ToyAssets:Blank";
  176. %water.BlendColor = %color;
  177. %water.SetBlendAlpha( 0.5 );
  178. SandboxScene.add( %water );
  179. // Create some wave crests.
  180. %crests1 = new Scroller();
  181. %crests1.setArea( %areaLeft SPC %crestY SPC %areaRight SPC %areaTop );
  182. %crests1.Image = "BuoyancyControllerToy:WaveCrests";
  183. %crests1.BlendColor = %color;
  184. %crests1.SetBlendAlpha( 0.3 );
  185. %crests1.RepeatX = %width / 50;
  186. %crests1.ScrollX = getRandom(5,10);
  187. %crests1.ScrollPositionX = getRandom(0,50);
  188. SandboxScene.add( %crests1 );
  189. // Create some wave crests.
  190. %crests2 = new Scroller();
  191. %crests2.setArea( %areaLeft SPC %crestY SPC %areaRight SPC %areaTop );
  192. %crests2.Image = "BuoyancyControllerToy:WaveCrests";
  193. %crests2.BlendColor = %color;
  194. %crests2.SetBlendAlpha( 0.3 );
  195. %crests2.RepeatX = %width / 50;
  196. %crests2.ScrollX = getRandom(-5,-10);
  197. %crests2.ScrollPositionX = getRandom(0,50);
  198. SandboxScene.add( %crests2 );
  199. }
  200. //-----------------------------------------------------------------------------
  201. function BuoyancyControllerToy::setFluidFlowMagnitude(%this, %value)
  202. {
  203. %this.FlowMagnitude = %value;
  204. // Update the controllers.
  205. %this.updateBuoyancyControllers();
  206. }
  207. //-----------------------------------------------------------------------------
  208. function BuoyancyControllerToy::setFluidDensity(%this, %value)
  209. {
  210. %this.FluidDensity = %value;
  211. // Update the controllers.
  212. %this.updateBuoyancyControllers();
  213. }
  214. //-----------------------------------------------------------------------------
  215. function BuoyancyControllerToy::setLinearDrag(%this, %value)
  216. {
  217. %this.LinearDrag = %value;
  218. // Update the controllers.
  219. %this.updateBuoyancyControllers();
  220. }
  221. //-----------------------------------------------------------------------------
  222. function BuoyancyControllerToy::setAngularDrag(%this, %value)
  223. {
  224. %this.AngularDrag = %value;
  225. // Update the controllers.
  226. %this.updateBuoyancyControllers();
  227. }
  228. //-----------------------------------------------------------------------------
  229. function BuoyancyControllerToy::setFluidGravity(%this, %value)
  230. {
  231. %this.FluidGravity = %value;
  232. // Update the controllers.
  233. %this.updateBuoyancyControllers();
  234. }
  235. //-----------------------------------------------------------------------------
  236. function BuoyancyControllerToy::setUseShapeDensity(%this, %value)
  237. {
  238. %this.UseShapeDensity = %value;
  239. // Update the controllers.
  240. %this.updateBuoyancyControllers();
  241. }
  242. //-----------------------------------------------------------------------------
  243. function BuoyancyControllerToy::setMaxDebrisCount(%this, %value)
  244. {
  245. %this.MaxDebrisCount = %value;
  246. }
  247. //-----------------------------------------------------------------------------
  248. function BuoyancyControllerToy::setMinDebrisDensity(%this, %value)
  249. {
  250. %this.MinDebrisDensity = %value;
  251. }
  252. //-----------------------------------------------------------------------------
  253. function BuoyancyControllerToy::setMaxDebrisDensity(%this, %value)
  254. {
  255. %this.MaxDebrisDensity = %value;
  256. }
  257. //-----------------------------------------------------------------------------
  258. function BuoyancyControllerToy::updateBuoyancyControllers( %this )
  259. {
  260. // Fetch the controller set.
  261. %controllerSet = SandboxScene.Controllers;
  262. // Fetch controller count.
  263. %controllerCount = %controllerSet.getCount();
  264. // Iterate controllers.
  265. for( %i = 0; %i < %controllerCount; %i++ )
  266. {
  267. %controller = %controllerSet.getObject(%i);
  268. if ( %controller.getClassName() !$= "BuoyancyController" )
  269. continue;
  270. // Update the controllers.
  271. %controller.FluidDensity = BuoyancyControllerToy.FluidDensity;
  272. %controller.FlowVelocity = Vector2Direction( Vector2AngleToPoint("0 0",%controller.FlowVelocity), %this.FlowMagnitude );
  273. %controller.LinearDrag = BuoyancyControllerToy.LinearDrag;
  274. %controller.AngularDrag = BuoyancyControllerToy.AngularDrag;
  275. %controller.FluidGravity = BuoyancyControllerToy.FluidGravity;
  276. %controller.UseShapeDensity = BuoyancyControllerToy.UseShapeDensity;
  277. }
  278. }