aquarium.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 getFishAnimationList()
  23. {
  24. %list = "TropicalAssets:angelfish1Anim" @ "," @ "TropicalAssets:angelfish2Anim" @ "," @ "TropicalAssets:butterflyfishAnim";
  25. %list = %list @ "," @ "TropicalAssets:pufferfishAnim" @ "," @ "TropicalAssets:rockfishAnim" @ "," @ "TropicalAssets:seahorseAnim";
  26. %list = %list @ "," @ "TropicalAssets:triggerfish1Anim" @ "," @ "TropicalAssets:eelAnim";
  27. }
  28. //-----------------------------------------------------------------------------
  29. function getFishSize(%anim)
  30. {
  31. switch$(%anim)
  32. {
  33. case "TropicalAssets:angelfish1Anim":
  34. %fishInfo = "15 15";
  35. case "TropicalAssets:angelfish2Anim":
  36. %fishInfo = "15 15";
  37. case "TropicalAssets:butterflyfishAnim":
  38. %fishInfo = "15 15";
  39. case "TropicalAssets:pufferfishAnim":
  40. %fishInfo = "15 15";
  41. case "TropicalAssets:rockfishAnim":
  42. %fishInfo = "15 7.5";
  43. case "TropicalAssets:seahorseAnim":
  44. %fishInfo = "7.5 15";
  45. case "TropicalAssets:triggerfish1Anim":
  46. %fishInfo = "15 15";
  47. case "TropicalAssets:eelAnim":
  48. %fishInfo = "7.5 3.75";
  49. }
  50. return %fishInfo;
  51. }
  52. //-----------------------------------------------------------------------------
  53. function buildAquarium(%scene)
  54. {
  55. // A pre-built aquarium of size 100x75, with blue water, some haze, and some nice rocks.
  56. // Triggers will be provide around the edges to let the developer know when objects in the
  57. // aquarium have reached the edges.
  58. // Background
  59. %background = new Sprite();
  60. %background.setBodyType( "static" );
  61. %background.setImage( "TropicalAssets:background" );
  62. %background.setSize( 100, 75 );
  63. %background.setCollisionSuppress();
  64. %background.setAwake( false );
  65. %background.setActive( false );
  66. %background.setSceneLayer(30);
  67. %scene.add( %background );
  68. // Far rocks
  69. %farRocks = new Sprite();
  70. %farRocks.setBodyType( "static" );
  71. %farRocks.setPosition( 0, -7.5 );
  72. %farRocks.setImage( "TropicalAssets:rocksfar" );
  73. %farRocks.setSize( 100, 75 );
  74. %farRocks.setCollisionSuppress();
  75. %farRocks.setAwake( false );
  76. %farRocks.setActive( false );
  77. %farRocks.setSceneLayer(20);
  78. %scene.add( %farRocks );
  79. // Near rocks
  80. %nearRocks = new Sprite();
  81. %nearRocks.setBodyType( "static" );
  82. %nearRocks.setPosition( 0, -8.5 );
  83. %nearRocks.setImage( "TropicalAssets:rocksnear" );
  84. %nearRocks.setSize( 100, 75 );
  85. %nearRocks.setCollisionSuppress();
  86. %nearRocks.setAwake( false );
  87. %nearRocks.setActive( false );
  88. %nearRocks.setSceneLayer(10);
  89. %scene.add( %nearRocks );
  90. addAquariumBoundaries( %scene, 100, 75 );
  91. }
  92. //-----------------------------------------------------------------------------
  93. function addAquariumBoundaries(%scene, %width, %height)
  94. {
  95. // add boundaries on all sides of the aquarium a bit outside of the border of the tank.
  96. // The triggers allow for onCollision to be sent to any fish or other object that touches the edges.
  97. // The triggers are far enough outside the tank so that objects will most likely be just out of view
  98. // before they are sent the onCollision callback. This way will they can adjust "off stage".
  99. // Calculate a width and height to use for the bounds.
  100. // They should be bigger than the aquarium itself.
  101. %wrapWidth = %width * 1.5;
  102. %wrapHeight = %height * 1.5;
  103. %scene.add( createOneAquariumBoundary( "left", -%wrapWidth/2 SPC 0, 5 SPC %wrapHeight) );
  104. %scene.add( createOneAquariumBoundary( "right", %wrapWidth/2 SPC 0, 5 SPC %wrapHeight) );
  105. %scene.add( createOneAquariumBoundary( "top", 0 SPC -%wrapHeight/2, %wrapWidth SPC 5 ) );
  106. %scene.add( createOneAquariumBoundary( "bottom", 0 SPC %wrapHeight/2, %wrapWidth SPC 5 ) );
  107. }
  108. //-----------------------------------------------------------------------------
  109. function createOneAquariumBoundary(%side, %position, %size)
  110. {
  111. %boundary = new SceneObject() { class = "AquariumBoundary"; };
  112. %boundary.setSize( %size );
  113. %boundary.side = %side;
  114. %boundary.setPosition( %position );
  115. %boundary.setSceneLayer( 1 );
  116. %boundary.createPolygonBoxCollisionShape( %size );
  117. // the objects that collide with us should handle any callbacks.
  118. // remember to set those scene objects to collide with scene group 15 (which is our group)!
  119. %boundary.setSceneGroup( 15 );
  120. %boundary.setCollisionCallback(false);
  121. %boundary.setBodyType( "static" );
  122. %boundary.setCollisionShapeIsSensor(0, true);
  123. return %boundary;
  124. }
  125. //-----------------------------------------------------------------------------
  126. function createAquariumEffects(%scene)
  127. {
  128. %obj = new Scroller();
  129. %obj.setBodyType( "static" );
  130. %obj.setImage( "TropicalAssets:wave" );
  131. %obj.setPosition( 0, 0 );
  132. %obj.setScrollX(2);
  133. %obj.setSize( 100, 75 );
  134. %obj.setRepeatX( 0.2 );
  135. %obj.setSceneLayer( 0 );
  136. %obj.setSceneGroup( 0 );
  137. %obj.setCollisionSuppress();
  138. %obj.setAwake( false );
  139. %obj.setActive( false );
  140. %scene.add( %obj );
  141. // Add the caustics particle.
  142. %caustics = new ParticlePlayer();
  143. %caustics.Particle = "TropicalAssets:Caustics";
  144. %scene.add( %caustics );
  145. }
  146. //-----------------------------------------------------------------------------