main.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 BridgeToy::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, -9.8);
  31. // Configure the toy.
  32. BridgeToy.GroundWidth = 40;
  33. BridgeToy.maxDebris = 3;
  34. // Add configuration option.
  35. addNumericOption("Amount of Debris", 0, 30, 1, "setMaxDebris", BridgeToy.maxDebris, true, "Sets the amount of debris created.");
  36. // Reset the toy initially.
  37. BridgeToy.reset();
  38. }
  39. //-----------------------------------------------------------------------------
  40. function BridgeToy::destroy(%this)
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. function BridgeToy::reset(%this)
  45. {
  46. // Clear the scene.
  47. SandboxScene.clear();
  48. // Set the camera size.
  49. SandboxWindow.setCameraSize( 40, 30 );
  50. // Create a background.
  51. %this.createBackground();
  52. // Create the links for the bridge
  53. %this.createBridge(-12.5, -1, 52);
  54. // Add some debris
  55. %this.createDebris();
  56. // Create the left side of the bridge
  57. %this.createBase(-13, 0, 0);
  58. // Create the right side of the bridge
  59. %this.createBase(13, 0, 0);
  60. // Create the ground.
  61. %this.createGround();
  62. }
  63. //-----------------------------------------------------------------------------
  64. function BridgeToy::createBackground( %this )
  65. {
  66. // Create the sprite.
  67. %object = new Sprite();
  68. // Set the sprite as "static" so it is not affected by gravity.
  69. %object.setBodyType( static );
  70. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  71. // Set the position.
  72. %object.Position = "0 0";
  73. // Set the size.
  74. %object.Size = "40 30";
  75. // Set to the furthest background layer.
  76. %object.SceneLayer = 31;
  77. // Set an image.
  78. %object.Image = "ToyAssets:jungleSky";
  79. // Add the sprite to the scene.
  80. SandboxScene.add( %object );
  81. }
  82. //-----------------------------------------------------------------------------
  83. function BridgeToy::createGround( %this )
  84. {
  85. // Create the ground
  86. %ground = new Scroller();
  87. %ground.setBodyType("static");
  88. %ground.Image = "ToyAssets:dirtGround";
  89. %ground.setPosition(0, -12);
  90. %ground.setSize(BridgeToy.GroundWidth, 6);
  91. %ground.setRepeatX(BridgeToy.GroundWidth / 60);
  92. %ground.createEdgeCollisionShape(BridgeToy.GroundWidth/-2, 3, BridgeToy.GroundWidth/2, 3);
  93. SandboxScene.add(%ground);
  94. // Create the grass.
  95. %grass = new Sprite();
  96. %grass.setBodyType("static");
  97. %grass.Image = "ToyAssets:grassForeground";
  98. %grass.setPosition(0, -8.5);
  99. %grass.setSize(BridgeToy.GroundWidth, 2);
  100. SandboxScene.add(%grass);
  101. }
  102. //-----------------------------------------------------------------------------
  103. function BridgeToy::createBase(%this, %posX, %posY, %angle)
  104. {
  105. %obj = new Sprite();
  106. %obj.setBodyType("static");
  107. %obj.setImage("ToyAssets:jungleTree");
  108. %obj.setSize(10, 18);
  109. %obj.setPosition(%posX, %posY);
  110. %obj.setAngle(%angle);
  111. SandboxScene.add(%obj);
  112. }
  113. //-----------------------------------------------------------------------------
  114. function BridgeToy::createBridge(%this, %posX, %posY, %linkCount)
  115. {
  116. %linkWidth = 0.5;
  117. %linkHeight = %linkWidth * 0.5;
  118. %halfLinkWidth = %linkWidth * 0.5;
  119. %rootObj = new Sprite();
  120. %rootObj.setBodyType("static");
  121. %rootObj.setImage("ToyAssets:cable");
  122. %rootObj.setPosition(%posX, %posY);
  123. %rootObj.setSize(%linkWidth, %linkHeight);
  124. %rootObj.setCollisionSuppress();
  125. SandboxScene.add(%rootObj);
  126. %lastLinkObj = %rootObj;
  127. for (%n = 1; %n <= %linkCount; %n++)
  128. {
  129. %obj = new Sprite();
  130. %obj.setImage("ToyAssets:cable");
  131. %obj.setPosition(%posX + (%n*%linkWidth), %posY);
  132. %obj.setSize(%linkWidth, %linkHeight);
  133. if (%n == %linkCount)
  134. {
  135. %obj.setBodyType("static");
  136. %obj.setCollisionSuppress();
  137. }
  138. else
  139. {
  140. %obj.setDefaultDensity(1);
  141. %obj.setDefaultFriction(1.0);
  142. %obj.createPolygonBoxCollisionShape(%linkWidth, %linkHeight);
  143. %obj.setAngularDamping(1.0);
  144. %obj.setLinearDamping(1.0);
  145. }
  146. SandboxScene.add(%obj);
  147. SandboxScene.createRevoluteJoint(%lastLinkObj, %obj, %halfLinkWidth, 0, -%halfLinkWidth, 0);
  148. %joint = SandboxScene.createMotorJoint(%lastLinkObj, %obj);
  149. SandboxScene.setMotorJointMaxForce(%joint, 100);
  150. %obj.setAwake(false);
  151. %lastLinkObj.setAwake(false);
  152. %lastLinkObj = %obj;
  153. }
  154. }
  155. //-----------------------------------------------------------------------------
  156. function BridgeToy::createDebris(%this)
  157. {
  158. for (%image = 0; %i < %this.maxDebris; %i++)
  159. {
  160. %randomPosition = getRandom(-10, 10) SPC getRandom(2, 8);
  161. %obj = new Sprite();
  162. %obj.setImage("ToyAssets:crate");
  163. %obj.setPosition(%randomPosition);
  164. %obj.setSize(1.5, 1.5);
  165. %obj.setDefaultFriction(1.0);
  166. %obj.setDefaultDensity(0.1);
  167. %obj.createPolygonBoxCollisionShape(1.4, 1.4);
  168. %obj.setBullet( true );
  169. SandboxScene.add(%obj);
  170. }
  171. }
  172. //-----------------------------------------------------------------------------
  173. function BridgeToy::setMaxDebris(%this, %value)
  174. {
  175. %this.maxDebris = %value;
  176. }