2
0

main.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 RestitutionToy::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. // Initialize the toys settings.
  30. RestitutionToy.maxBalls = 20;
  31. RestitutionToy.GroundWidth = 150;
  32. // Add the custom controls.
  33. addNumericOption("Ball Count", 1, 100, 1, "setMaxBalls", RestitutionToy.maxBalls, true, "Sets the number of balls to bounce.");
  34. // Reset the toy initially.
  35. RestitutionToy.reset();
  36. }
  37. //-----------------------------------------------------------------------------
  38. function RestitutionToy::destroy( %this )
  39. {
  40. }
  41. //-----------------------------------------------------------------------------
  42. function RestitutionToy::reset(%this)
  43. {
  44. // Clear the scene.
  45. SandboxScene.clear();
  46. // Zoom the camera in
  47. SandboxWindow.setCameraSize( 50, 37.5 );
  48. // Prefer the collision option off as it severely affects the performance.
  49. setCollisionOption( false );
  50. // Set the scene gravity.
  51. SandboxScene.setGravity( 0, -9.8 );
  52. // Create the background.
  53. %this.createBackground();
  54. // Create the ground.
  55. %this.createGround();
  56. // Create the balls.
  57. %this.createBalls();
  58. }
  59. //-----------------------------------------------------------------------------
  60. function RestitutionToy::createBackground( %this )
  61. {
  62. // Create the sprite.
  63. %object = new Sprite();
  64. // Stop the background from being affected by gravity.
  65. %object.setBodyType( "static" );
  66. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  67. // Set the position.
  68. %object.Position = "0 0";
  69. // Set the size.
  70. %object.Size = "100 75";
  71. // Set to the furthest background layer.
  72. %object.SceneLayer = 31;
  73. // Set an image.
  74. %object.Image = "ToyAssets:skyBackground";
  75. // Add the sprite to the scene.
  76. SandboxScene.add( %object );
  77. }
  78. //-----------------------------------------------------------------------------
  79. function RestitutionToy::createGround( %this )
  80. {
  81. // Create the ground
  82. %ground = new Scroller();
  83. %ground.setBodyType("static");
  84. %ground.Image = "ToyAssets:dirtGround";
  85. %ground.setPosition(0, -16);
  86. %ground.setSize(RestitutionToy.GroundWidth, 6);
  87. %ground.setRepeatX(RestitutionToy.GroundWidth / 60);
  88. %ground.setSceneGroup( 1 );
  89. %ground.setCollisionGroups( none );
  90. %ground.createEdgeCollisionShape(RestitutionToy.GroundWidth/-2, 3, RestitutionToy.GroundWidth/2, 3);
  91. SandboxScene.add(%ground);
  92. // Create the grass.
  93. %grass = new Sprite();
  94. %grass.setBodyType("static");
  95. %grass.Image = "ToyAssets:grassForeground";
  96. %grass.setPosition(0, -12.5);
  97. %grass.setSize(RestitutionToy.GroundWidth, 2);
  98. SandboxScene.add(%grass);
  99. }
  100. //-----------------------------------------------------------------------------
  101. function RestitutionToy::createBalls(%this)
  102. {
  103. // Calculate restitution stride.
  104. %restitutionStride = 1.0 / RestitutionToy.maxBalls;
  105. // Set the ball size.
  106. %virtualBallSize = 20 / RestitutionToy.maxBalls;
  107. %ballSize = 2;
  108. // Calculate ball-stack offset and stride.
  109. %ballOffset = ((RestitutionToy.maxBalls-1) * %virtualBallSize * 2) * -0.5;
  110. // Add a ball to each stack.
  111. for( %n = 0; %n < RestitutionToy.maxBalls; %n++ )
  112. {
  113. // Calculate the stack offset.
  114. %stackOffset = %ballOffset + (%n * %virtualBallSize * 2);
  115. // Create the ball.
  116. %ball = new Sprite();
  117. %ball.SetPosition( %stackOffset, 14 );
  118. %ball.Size = %ballSize;
  119. %ball.Image = "ToyAssets:Football";
  120. %ball.setDefaultDensity( 0.1 );
  121. %ball.setDefaultRestitution( 1 - (%restitutionStride * %n) );
  122. %ball.setSceneGroup( 2 );
  123. %ball.setCollisionGroups( 1 );
  124. %ball.createCircleCollisionShape( %ballSize * 0.5 );
  125. // Add to the scene.
  126. SandboxScene.add( %ball );
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. function RestitutionToy::setMaxBalls(%this, %value)
  131. {
  132. %this.maxBalls = %value;
  133. }