main.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 ChainToy::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 the scene gravity.
  30. SandboxScene.setGravity(0, -29.8);
  31. // Configure the toy.
  32. ChainToy.GroundWidth = 80;
  33. ChainToy.ChainLinks = 25;
  34. ChainToy.ChainCount = 2;
  35. ChainToy.ChainLimit = true;
  36. ChainToy.BallDensity = 4;
  37. // Add configuration option.
  38. addNumericOption( "Chain Links", 1, 46, 1, "setChainLinks", ChainToy.ChainLinks, true, "Sets the number of links in each chain." );
  39. addNumericOption( "Chain Count", 1, 20, 1, "setChainCount", ChainToy.ChainCount, true, "Sets the number of chains to create." );
  40. addFlagOption("Chain Limit", "setChainLimit", ChainToy.ChainLimit, true, "Whether to restrict the length of the chain with a rope joint or not." );
  41. addNumericOption( "Ball Density", 1, 10, 1, "setBallDensity", ChainToy.BallDensity, true, "Sets the ball density." );
  42. // Reset the toy initially.
  43. ChainToy.reset();
  44. }
  45. //-----------------------------------------------------------------------------
  46. function ChainToy::destroy( %this )
  47. {
  48. }
  49. //-----------------------------------------------------------------------------
  50. function ChainToy::reset(%this)
  51. {
  52. // Clear the scene.
  53. SandboxScene.clear();
  54. // Set the camera size.
  55. SandboxWindow.setCameraSize( 40, 30 );
  56. // Create a background.
  57. %this.createBackground();
  58. // Create the chains.
  59. %chainSpacing = 2;
  60. %chainOffset = (ChainToy.ChainCount-1) * -(%chainSpacing*0.5);
  61. for( %n = 0; %n < ChainToy.ChainCount; %n++ )
  62. {
  63. %this.createChain(%chainOffset, 15);
  64. %chainOffset += %chainSpacing;
  65. }
  66. // Create the ground.
  67. %this.createGround();
  68. }
  69. //-----------------------------------------------------------------------------
  70. function ChainToy::createBackground( %this )
  71. {
  72. // Create the sprite.
  73. %object = new Sprite();
  74. // Set the sprite as "static" so it is not affected by gravity.
  75. %object.setBodyType( static );
  76. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  77. // Set the position.
  78. %object.Position = "0 0";
  79. // Set the size.
  80. %object.Size = "40 30";
  81. // Set to the furthest background layer.
  82. %object.SceneLayer = 31;
  83. // Set an image.
  84. %object.Image = "ToyAssets:jungleSky";
  85. // Add the sprite to the scene.
  86. SandboxScene.add( %object );
  87. }
  88. //-----------------------------------------------------------------------------
  89. function ChainToy::createGround( %this )
  90. {
  91. // Create the ground.
  92. %ground = new Scroller();
  93. %ground.setBodyType("static");
  94. %ground.Image = "ToyAssets:dirtGround";
  95. %ground.setPosition(0, -12);
  96. %ground.setSize(ChainToy.GroundWidth, 6);
  97. %ground.setRepeatX(ChainToy.GroundWidth / 60);
  98. %ground.createEdgeCollisionShape(ChainToy.GroundWidth/-2, 3, ChainToy.GroundWidth/2, 3);
  99. // Add to the scene.
  100. SandboxScene.add(%ground);
  101. // Create the grass.
  102. %grass = new Sprite();
  103. %grass.setBodyType("static");
  104. %grass.Image = "ToyAssets:grassForeground";
  105. %grass.setPosition(0, -8.5);
  106. %grass.setSize(ChainToy.GroundWidth, 2);
  107. // Add to the scene.
  108. SandboxScene.add(%grass);
  109. }
  110. //-----------------------------------------------------------------------------
  111. function ChainToy::createChain(%this, %posX, %posY)
  112. {
  113. // Set-up some initial dimensions.
  114. %linkWidth = 0.25;
  115. %linkHeight = %linkWidth * 2;
  116. %halfLinkHeight = %linkHeight * 0.5;
  117. %weightSize = 1.5;
  118. %weightHalfSize = %weightSize * 0.5;
  119. %pivotDistance = %linkHeight * %this.ChainLinks;
  120. // Create a fixed pivot object.
  121. %fixedObject = new Sprite();
  122. %fixedObject.BodyType = static;
  123. %fixedObject.Image = "ToyAssets:chain";
  124. %fixedObject.setPosition( %posX, %posY );
  125. %fixedObject.setSize( %linkWidth, %linkHeight );
  126. SandboxScene.add( %fixedObject );
  127. // Set-up the last linked object as the fixed pivot.
  128. %lastLinkObj = %fixedObject;
  129. // Now add the rest of the links.
  130. for ( %n = 1; %n <= %this.ChainLinks; %n++ )
  131. {
  132. // Create the link object.
  133. %obj = new Sprite();
  134. %obj.setImage( "ToyAssets:chain" );
  135. %obj.setPosition( %posX, %posY - (%n*%linkHeight) );
  136. %obj.setSize( %linkWidth, %linkHeight );
  137. %obj.setDefaultDensity( 20 );
  138. %obj.setDefaultFriction( 0.2 );
  139. %obj.createPolygonBoxCollisionShape( %linkWidth, %linkHeight );
  140. %obj.setAngularDamping( 0.1 );
  141. %obj.setLinearDamping( 0.1 );
  142. SandboxScene.add( %obj );
  143. // Create a revolute joint from the last link to this one.
  144. SandboxScene.createRevoluteJoint( %lastLinkObj, %obj, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
  145. // Reference this link as the last link.
  146. %lastLinkObj = %obj;
  147. }
  148. // Create the weight.
  149. %weight = new Sprite();
  150. %weight.setImage( "ToyAssets:whitesphere" );
  151. %weight.BlendColor = DarkGreen;
  152. %weight.setSize( %weightSize );
  153. %weight.setPosition( %posX, %posY - %pivotDistance - %weightHalfSize );
  154. %weight.setDefaultFriction( 0.2 );
  155. %weight.setDefaultDensity( 1 );
  156. %weight.createCircleCollisionShape( %weightHalfSize );
  157. SandboxScene.add( %weight );
  158. // Create a revolute joint from the last link to the weight.
  159. SandboxScene.createRevoluteJoint( %lastLinkObj, %weight, 0, -%halfLinkHeight, 0, %halfLinkHeight, false );
  160. // If the chain limit is on then create a rope join from the fixed pivot to the weight.
  161. if ( %this.ChainLimit )
  162. SandboxScene.createRopeJoint(%fixedObject, %weight, 0, 0, 0, %weightHalfSize, %pivotDistance, false);
  163. }
  164. //-----------------------------------------------------------------------------
  165. function ChainToy::setChainLinks(%this, %value)
  166. {
  167. %this.ChainLinks = %value;
  168. }
  169. //-----------------------------------------------------------------------------
  170. function ChainToy::setChainCount(%this, %value)
  171. {
  172. %this.ChainCount = %value;
  173. }
  174. //-----------------------------------------------------------------------------
  175. function ChainToy::setChainLimit(%this, %value)
  176. {
  177. %this.ChainLimit = %value;
  178. }
  179. //-----------------------------------------------------------------------------
  180. function ChainToy::setBallDensity(%this, %value)
  181. {
  182. %this.BallDensity = %value;
  183. }