main.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 SoftbodyToy::create( %this )
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pull );
  26. // Set the manipulation mode.
  27. Sandbox.useManipulation( pull );
  28. // Configure the toy.
  29. SoftbodyToy.GroundWidth = 100;
  30. SoftbodyToy.NodeSize = 5;
  31. SoftbodyToy.NodeX = 5;
  32. SoftbodyToy.NodeY = 5;
  33. SoftbodyToy.Frequency = 3;
  34. SoftbodyToy.DampingRatio = 0;
  35. SoftbodyToy.Density = 0.1;
  36. SoftbodyToy.Restitution = 0.5;
  37. // Add configuration option.
  38. addNumericOption( "Node Size", 1, 5, 1, "setNodeSize", SoftbodyToy.NodeSize, true, "Sets the size of each node sprite." );
  39. addNumericOption( "Node Count X", 1, 100, 1, "setNodeX", SoftbodyToy.NodeX, true, "Sets the number of nodes in the X axis." );
  40. addNumericOption( "Node Count Y", 1, 100, 1, "setNodeY", SoftbodyToy.NodeY, true, "Sets the number of nodes in the Y axis." );
  41. addNumericOption( "Node Density", 0, 10, 0.1, "setDensity", SoftbodyToy.Density, true, "Sets the density of each node." );
  42. addNumericOption( "Node Restitution", 0, 5, 0.1, "setRestitution", SoftbodyToy.Restitution, true, "Sets the restitution of each node." );
  43. addNumericOption( "Joint Frequency", 0, 120, 1, "setFrequency", SoftbodyToy.Frequency, true, "Sets the frequency of the distance joints." );
  44. addNumericOption( "Joint Damping Ratio", 0, 10, 0.1, "setDampingRatio", SoftbodyToy.DampingRatio , true, "Sets the damping ratio of the distance joints." );
  45. // Reset the toy initially.
  46. SoftbodyToy.reset();
  47. }
  48. //-----------------------------------------------------------------------------
  49. function SoftbodyToy::destroy( %this )
  50. {
  51. }
  52. //-----------------------------------------------------------------------------
  53. function SoftbodyToy::reset( %this )
  54. {
  55. // Clear the scene.
  56. SandboxScene.clear();
  57. // Set the scene gravity.
  58. SandboxScene.setGravity( 0, -9.8 );
  59. // Create the background.
  60. %this.createBackground();
  61. // Create the surround.
  62. %this.createSurround();
  63. // Create a cube soft body.
  64. %this.createCubeSoftbody();
  65. }
  66. //-----------------------------------------------------------------------------
  67. function SoftbodyToy::createBackground( %this )
  68. {
  69. // Create the sprite.
  70. %object = new Sprite();
  71. // Stop the background from being affected by gravity.
  72. %object.setBodyType( "static" );
  73. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  74. // Set the position.
  75. %object.Position = "0 0";
  76. // Set the size.
  77. %object.Size = "100 75";
  78. // Set to the furthest background layer.
  79. %object.SceneLayer = 31;
  80. // Set an image.
  81. %object.Image = "ToyAssets:skyBackground";
  82. // Add the sprite to the scene.
  83. SandboxScene.add( %object );
  84. }
  85. //-----------------------------------------------------------------------------
  86. function SoftbodyToy::createSurround( %this )
  87. {
  88. // Create the bottom
  89. %obj = new Scroller();
  90. %obj.setBodyType("static");
  91. %obj.Image = "ToyAssets:dirtGround";
  92. %obj.setPosition(0, -35);
  93. %obj.setSize(SoftbodyToy.GroundWidth, 6);
  94. %obj.setRepeatX(SoftbodyToy.GroundWidth / 60);
  95. %obj.createEdgeCollisionShape(SoftbodyToy.GroundWidth/-2, 3, SoftbodyToy.GroundWidth/2, 3);
  96. %obj.createEdgeCollisionShape(SoftbodyToy.GroundWidth/-2, 72, SoftbodyToy.GroundWidth/2, 72);
  97. %obj.createEdgeCollisionShape(SoftbodyToy.GroundWidth/-2, 3, SoftbodyToy.GroundWidth/-2, 72);
  98. %obj.createEdgeCollisionShape(SoftbodyToy.GroundWidth/2, 3, SoftbodyToy.GroundWidth/2, 72);
  99. SandboxScene.add(%obj);
  100. // Create the grass.
  101. %obj = new Sprite();
  102. %obj.setBodyType("static");
  103. %obj.Image = "ToyAssets:grassForeground";
  104. %obj.setPosition(0, -31.5);
  105. %obj.setSize(SoftbodyToy.GroundWidth, 2);
  106. SandboxScene.add(%obj);
  107. }
  108. //-----------------------------------------------------------------------------
  109. function SoftbodyToy::createCubeSoftbody( %this )
  110. {
  111. // Set cube start.
  112. %positionX = 0;
  113. %positionY = 0;
  114. // Calculate cube offset.
  115. %halfNodeSize = SoftbodyToy.NodeSize * 0.5;
  116. %nodeStride = SoftbodyToy.NodeSize * 0.8;
  117. %offsetX = %positionX - (( %nodeStride * (SoftbodyToy.NodeX-1)) * 0.5);
  118. %offsetY = %positionY - (( %nodeStride * (SoftbodyToy.NodeY-1)) * 0.5);
  119. // Create the sprites.
  120. for ( %x = 0; %x < SoftbodyToy.NodeX; %x++ )
  121. {
  122. for ( %y = 0; %y < SoftbodyToy.NodeY; %y++ )
  123. {
  124. // Create the sprite.
  125. %object = new Sprite();
  126. // Set the position.
  127. %object.setPosition( %offsetX + (%x * %nodeStride), %offsetY + (%y * %nodeStride) );
  128. // Set the static image.
  129. //%object.Image = "ToyAssets:WhiteSphere";
  130. %object.Animation = "ToyAssets:PoisonCloudWobble";
  131. // Set a useful size.
  132. %object.Size = SoftbodyToy.NodeSize;
  133. // Set the object at a fixed angle so it cannot rotate.
  134. %object.setFixedAngle( true );
  135. // Set default density.
  136. %object.setDefaultDensity( SoftbodyToy.Density );
  137. // Set default restitution.
  138. %object.setDefaultRestitution( SoftbodyToy.Restitution );
  139. // Create a collision shape.
  140. %object.createCircleCollisionShape( %halfNodeSize * 0.7 );
  141. // Add to the scene.
  142. SandboxScene.add( %object );
  143. // Store object.
  144. SoftbodyToy.body[%x,%y] = %object;
  145. }
  146. }
  147. // Create distance joints.
  148. for ( %x = 0; %x < SoftbodyToy.NodeX; %x++ )
  149. {
  150. for ( %y = 0; %y < (SoftbodyToy.NodeY-1); %y++ )
  151. {
  152. // Fetch objects.
  153. %objectA = SoftbodyToy.body[%x,%y];
  154. %objectB = SoftbodyToy.body[%x,%y+1];
  155. // Create a distance joint between them.
  156. %jointId = SandboxScene.createDistanceJoint( %objectA, %objectB );
  157. // Configure the joint.
  158. SandboxScene.setDistanceJointFrequency( %jointId, SoftbodyToy.Frequency );
  159. SandboxScene.setDistanceJointDampingRatio( %jointId, SoftbodyToy.DampingRatio );
  160. }
  161. }
  162. // Create distance joints.
  163. for ( %y = 0; %y < SoftbodyToy.NodeY; %y++ )
  164. {
  165. for ( %x = 0; %x < (SoftbodyToy.NodeX-1); %x++ )
  166. {
  167. // Fetch objects.
  168. %objectA = SoftbodyToy.body[%x,%y];
  169. %objectB = SoftbodyToy.body[%x+1,%y];
  170. // Create a distance joint between them.
  171. %jointId = SandboxScene.createDistanceJoint( %objectA, %objectB );
  172. // Configure the joint.
  173. SandboxScene.setDistanceJointFrequency( %jointId, SoftbodyToy.Frequency );
  174. SandboxScene.setDistanceJointDampingRatio( %jointId, SoftbodyToy.DampingRatio );
  175. }
  176. }
  177. // Create distance joints.
  178. for ( %y = 0; %y < (SoftbodyToy.NodeY-1); %y++ )
  179. {
  180. for ( %x = 0; %x < (SoftbodyToy.NodeX-1); %x++ )
  181. {
  182. // Fetch objects.
  183. %objectA = SoftbodyToy.body[%x,%y];
  184. %objectB = SoftbodyToy.body[%x+1,%y];
  185. %objectC = SoftbodyToy.body[%x+1,%y+1];
  186. %objectD = SoftbodyToy.body[%x,%y+1];
  187. // Create a distance joint between them.
  188. %jointId1 = SandboxScene.createDistanceJoint( %objectA, %objectC );
  189. %jointId2 = SandboxScene.createDistanceJoint( %objectB, %objectD );
  190. // Configure the joints.
  191. SandboxScene.setDistanceJointFrequency( %jointId1, SoftbodyToy.Frequency );
  192. SandboxScene.setDistanceJointFrequency( %jointId2, SoftbodyToy.Frequency );
  193. SandboxScene.setDistanceJointDampingRatio( %jointId1, SoftbodyToy.DampingRatio );
  194. SandboxScene.setDistanceJointDampingRatio( %jointId2, SoftbodyToy.DampingRatio );
  195. }
  196. }
  197. }
  198. //-----------------------------------------------------------------------------
  199. function SoftbodyToy::setNodeSize( %this, %value )
  200. {
  201. %this.NodeSize = %value;
  202. }
  203. //-----------------------------------------------------------------------------
  204. function SoftbodyToy::setNodeX(%this, %value)
  205. {
  206. %this.NodeX = %value;
  207. }
  208. //-----------------------------------------------------------------------------
  209. function SoftbodyToy::setNodeY(%this, %value)
  210. {
  211. %this.NodeY = %value;
  212. }
  213. //-----------------------------------------------------------------------------
  214. function SoftbodyToy::setDensity(%this, %value)
  215. {
  216. %this.Density = %value;
  217. }
  218. //-----------------------------------------------------------------------------
  219. function SoftbodyToy::setRestitution(%this, %value)
  220. {
  221. %this.Restitution = %value;
  222. }
  223. //-----------------------------------------------------------------------------
  224. function SoftbodyToy::setFrequency(%this, %value)
  225. {
  226. %this.Frequency = %value;
  227. }
  228. //-----------------------------------------------------------------------------
  229. function SoftbodyToy::setDampingRatio(%this, %value)
  230. {
  231. %this.DampingRatio = %value;
  232. }