main.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 TumblerToy::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. TumblerToy.maxBalls = 100;
  31. TumblerToy.currentBalls = 0;
  32. TumblerToy.MotorSpeed = 10;
  33. // Add the custom controls.
  34. addNumericOption("Number of balls", 10, 200, 10, "setMaxBalls", TumblerToy.maxBalls, true, "Sets the maximum number of balls to create." );
  35. addNumericOption("Motor Speed", 0, 360, 10, "setMotorSpeed", TumblerToy.MotorSpeed, false, "Sets the motor angular motor speed of the tumbler." );
  36. // Reset the toy initially.
  37. TumblerToy.reset();
  38. }
  39. //-----------------------------------------------------------------------------
  40. function TumblerToy::destroy( %this )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. function TumblerToy::reset(%this)
  45. {
  46. // Clear the scene.
  47. SandboxScene.clear();
  48. // Prefer the collision option off as it severely affects the performance.
  49. setCollisionOption( false );
  50. // Set the scene gravity.
  51. SandboxScene.setGravity( 0, -39.8 );
  52. // Create the tumbler.
  53. %this.createTumbler();
  54. // Reset the ball count.
  55. %this.currentBalls = 0;
  56. // Start the timer.
  57. TumblerToy.startTimer( "createBall", 100 );
  58. }
  59. //-----------------------------------------------------------------------------
  60. function TumblerToy::setMaxBalls(%this, %value)
  61. {
  62. %this.maxBalls = %value;
  63. }
  64. //-----------------------------------------------------------------------------
  65. function TumblerToy::setMotorSpeed(%this, %value)
  66. {
  67. TumblerToy.MotorSpeed = %value;
  68. SandboxScene.setRevoluteJointMotor( TumblerToy.MotorJoint, true, TumblerToy.MotorSpeed, 1000000 );
  69. }
  70. //-----------------------------------------------------------------------------
  71. function TumblerToy::createTumbler(%this)
  72. {
  73. // Create the tumbler.
  74. %tumbler = new Sprite();
  75. %tumbler.Image = "ToyAssets:checkered";
  76. %tumbler.BlendColor = BlueViolet;
  77. %tumbler.Size = 120.5;
  78. %tumbler.setDefaultDensity( 0.1 );
  79. %tumbler.createPolygonBoxCollisionShape( 1, 50, 25, 0, 0 );
  80. %tumbler.createPolygonBoxCollisionShape( 1, 50, -25, 0, 0 );
  81. %tumbler.createPolygonBoxCollisionShape( 50, 1, 0, 25, 0 );
  82. %tumbler.createPolygonBoxCollisionShape( 50, 1, 0, -25, 0 );
  83. SandboxScene.add( %tumbler );
  84. // Create the motor joint.
  85. TumblerToy.MotorJoint = SandboxScene.createRevoluteJoint( %tumbler, "", "0 0" );
  86. SandboxScene.setRevoluteJointMotor( TumblerToy.MotorJoint, true, TumblerToy.MotorSpeed, 1000000 );
  87. }
  88. //-----------------------------------------------------------------------------
  89. function TumblerToy::createBall(%this)
  90. {
  91. // Fetch the stock color count.
  92. %stockColorCount = getStockColorCount();
  93. // Create some balls.
  94. for ( %n = 0; %n < 5; %n++ )
  95. {
  96. // Create the ball.
  97. %ball = new Sprite();
  98. %ball.Position = getRandom(-10,10) SPC "0";
  99. %ball.Size = "2";
  100. %ball.Image = "ToyAssets:football";
  101. %ball.setDefaultRestitution( 0.6 );
  102. %ball.createCircleCollisionShape( 1 );
  103. SandboxScene.add( %ball );
  104. // Increase ball count.
  105. %this.currentBalls++;
  106. // Stop the timer if exceeded the required number of balls.
  107. if ( %this.currentBalls >= %this.maxBalls)
  108. {
  109. // Cancel the timer.
  110. TumblerToy.stopTimer();
  111. return;
  112. }
  113. }
  114. }