main.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 AquariumToy::create( %this )
  23. {
  24. exec("./scripts/aquarium.cs");
  25. exec("./scripts/fish.cs");
  26. // Configure settings.
  27. AquariumToy.maxFish = 10;
  28. AquariumToy.currentFish = 0;
  29. AquariumToy.selectedAnimation = "TropicalAssets:angelfish1Anim";
  30. // Set the fish scene-layer to sort in "batch" mode
  31. // so that all the fish will be sorted into a batchable order to reduce draw calls.
  32. SandboxScene.setLayerSortMode( 15, batch );
  33. addNumericOption("Max Fish", 1, 50, 1, "setMaxFish", %this.maxFish, true, "Sets the maximum number of fish to be created.");
  34. addSelectionOption(getFishAnimationList(), "Fish Animation", 5, "setSelectedAnimation", false, "Selects the fish animation that can be spawned manually.");
  35. addButtonOption("Spawn fish", "spawnOneFish", false, "Spawns the selected fish animation." );
  36. // Reset the toy initially.
  37. AquariumToy.reset();
  38. }
  39. //-----------------------------------------------------------------------------
  40. function AquariumToy::destroy( %this )
  41. {
  42. }
  43. //-----------------------------------------------------------------------------
  44. function AquariumToy::reset(%this)
  45. {
  46. // Clear the scene.
  47. SandboxScene.clear();
  48. // Set the gravity.
  49. SandboxScene.setGravity(0, 0);
  50. buildAquarium(SandboxScene);
  51. createAquariumEffects(SandboxScene);
  52. // Reset the ball count.
  53. %this.currentFish = 0;
  54. // Start the timer.
  55. AquariumToy.startTimer( "spawnFish", 100 );
  56. }
  57. //-----------------------------------------------------------------------------
  58. function AquariumToy::spawnFish(%this)
  59. {
  60. %position = getRandom(-55, 55) SPC getRandom(-20, 20);
  61. %index = getRandom(0, 5);
  62. %anim = getUnit(getFishAnimationList(), %index, ",");
  63. %fishSize = getFishSize(%anim);
  64. %fish = new Sprite()
  65. {
  66. Animation = %anim;
  67. class = "FishClass";
  68. position = %position;
  69. size = %fishSize;
  70. SceneLayer = "15";
  71. SceneGroup = "14";
  72. minSpeed = "5";
  73. maxSpeed = "15";
  74. CollisionCallback = true;
  75. };
  76. // aquarium boundary triggers are in group 15. See TropicalAssets/scripts/aquarium.cs
  77. %fish.setCollisionGroups( 15 );
  78. %fish.createPolygonBoxCollisionShape(%fishSize);
  79. %fish.setDefaultDensity( 1 );
  80. SandboxScene.add( %fish );
  81. %this.currentFish++;
  82. // Have we reached the maximum number of fish?
  83. if ( %this.currentFish >= %this.maxFish)
  84. {
  85. // Yes, so stop the timer.
  86. AquariumToy.stopTimer();
  87. }
  88. }
  89. //-----------------------------------------------------------------------------
  90. function AquariumToy::setMaxFish(%this, %value)
  91. {
  92. %this.maxFish = %value;
  93. }
  94. //-----------------------------------------------------------------------------
  95. function AquariumToy::setSelectedAnimation(%this, %value)
  96. {
  97. %this.selectedAnimation = %value;
  98. }
  99. //-----------------------------------------------------------------------------
  100. function AquariumToy::spawnOneFish(%this)
  101. {
  102. %position = getRandom(-55, 55) SPC getRandom(-20, 20);
  103. %fishSize = getFishSize(%this.selectedAnimation);
  104. %fish = new Sprite()
  105. {
  106. Animation = %this.selectedAnimation;
  107. class = "FishClass";
  108. position = %position;
  109. size = %fishSize;
  110. SceneLayer = "2";
  111. SceneGroup = "14";
  112. minSpeed = "5";
  113. maxSpeed = "15";
  114. CollisionCallback = true;
  115. };
  116. %fish.createPolygonBoxCollisionShape( 15, 15);
  117. %fish.setCollisionGroups( 15 );
  118. %fish.setDefaultDensity( 1 );
  119. %fish.setDefaultFriction( 1.0 );
  120. SandboxScene.add( %fish );
  121. }