main.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 PyramidToy::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. // Configure the toy.
  30. PyramidToy.BlockSize = 1.5;
  31. PyramidToy.BlockCount = 15;
  32. PyramidToy.GroundWidth = 140;
  33. // Set the camera.
  34. SandboxWindow.setCameraSize( 40, 30 );
  35. // Se the gravity.
  36. SandboxScene.setGravity( 0, -9.8 );
  37. // Add configuration option.
  38. addNumericOption( "Block Count", 2, 30, 1, "setBlockCount", PyramidToy.BlockCount, true, "Sets the number of blocks used to create the pyramid." );
  39. addNumericOption( "Block Size", 1, 4, 0.5, "setBlockSize", PyramidToy.BlockSize, true, "Sets the size of the blocks used to create the pyramid." );
  40. // Reset the toy.
  41. PyramidToy.reset();
  42. }
  43. //-----------------------------------------------------------------------------
  44. function PyramidToy::destroy( %this )
  45. {
  46. }
  47. //-----------------------------------------------------------------------------
  48. function PyramidToy::reset( %this )
  49. {
  50. // Clear the scene.
  51. SandboxScene.clear();
  52. // Create a background.
  53. %this.createBackground();
  54. // Create the pyramid.
  55. %this.createPyramid();
  56. // Create the ground.
  57. %this.createGround();
  58. }
  59. //-----------------------------------------------------------------------------
  60. function PyramidToy::setBlockCount(%this, %value)
  61. {
  62. %this.BlockCount = %value;
  63. }
  64. //-----------------------------------------------------------------------------
  65. function PyramidToy::setBlockSize(%this, %value)
  66. {
  67. %this.BlockSize = %value;
  68. }
  69. //-----------------------------------------------------------------------------
  70. function PyramidToy::createBackground( %this )
  71. {
  72. // Create the sprite.
  73. %object = new Sprite();
  74. // Set the sprite as "static" so it is not affected by gravity.
  75. %object.setBodyType( static );
  76. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  77. // Set the size.
  78. %object.Size = PyramidToy.GroundWidth SPC (PyramidToy.GroundWidth * 0.75);
  79. // Set the position.
  80. %object.setPositionY( (%object.getSizeY() * 0.5) - 15 );
  81. // Set to the furthest background layer.
  82. %object.SceneLayer = 31;
  83. // Set an image.
  84. %object.Image = "ToyAssets:jungleSky";
  85. // Add the sprite to the scene.
  86. SandboxScene.add( %object );
  87. }
  88. //-----------------------------------------------------------------------------
  89. function PyramidToy::createGround( %this )
  90. {
  91. // Create the ground
  92. %ground = new Scroller();
  93. %ground.setBodyType("static");
  94. %ground.Image = "ToyAssets:dirtGround";
  95. %ground.setPosition(0, -12);
  96. %ground.setSize(PyramidToy.GroundWidth, 6);
  97. %ground.setRepeatX(PyramidToy.GroundWidth / 60);
  98. %ground.createEdgeCollisionShape(PyramidToy.GroundWidth/-2, 3, PyramidToy.GroundWidth/2, 3);
  99. //%ground.SceneLayer = 10;
  100. SandboxScene.add(%ground);
  101. // Create the grass.
  102. %grass = new Sprite();
  103. %grass.setBodyType("static");
  104. %grass.Image = "ToyAssets:grassForeground";
  105. %grass.setPosition(0, -8.5);
  106. %grass.setSize(PyramidToy.GroundWidth, 2);
  107. %grass.SceneLayer = 10;
  108. SandboxScene.add(%grass);
  109. }
  110. //-----------------------------------------------------------------------------
  111. function PyramidToy::createPyramid( %this )
  112. {
  113. // Fetch the block count.
  114. %blockCount = PyramidToy.BlockCount;
  115. // Sanity!
  116. if ( %blockCount < 2 )
  117. {
  118. echo( "Cannot have a pyramid block count less than two." );
  119. return;
  120. }
  121. // Set the block size.
  122. %blockSize = PyramidToy.BlockSize;
  123. // Calculate a block building position.
  124. %posx = %blockCount * -0.5 * %blockSize;
  125. %posy = -8.8 + (%blockSize * 0.5);
  126. // Build the stack of blocks.
  127. for( %stack = 0; %stack < %blockCount; %stack++ )
  128. {
  129. // Calculate the stack position.
  130. %stackIndexCount = %blockCount - (%stack*2);
  131. %stackX = %posX + ( %stack * %blockSize );
  132. %stackY = %posY + ( %stack * %blockSize );
  133. // Build the stack.
  134. for ( %stackIndex = 0; %stackIndex < %stackIndexCount; %stackIndex++ )
  135. {
  136. // Calculate the block position.
  137. %blockX = %stackX + (%stackIndex*%blockSize);
  138. %blockY = %stackY;
  139. // Create the sprite.
  140. %obj = new Sprite();
  141. %obj.setPosition( %blockX, %blockY );
  142. %obj.setSize( %blockSize );
  143. %obj.setImage( "ToyAssets:blocks" );
  144. %obj.setImageFrame( getRandom(0,55) );
  145. %obj.setDefaultFriction( 1.0 );
  146. %obj.createPolygonBoxCollisionShape( %blockSize, %blockSize );
  147. %obj.SceneLayer = 15;
  148. // Add to the scene.
  149. SandboxScene.add( %obj );
  150. }
  151. }
  152. %map = new LightObject();
  153. %map.setSize("15 15");
  154. %map.SceneLayer = 17;
  155. %map.setBlendColor(0.5,0.5,1.0,1.0);
  156. SandboxScene.add(%map);
  157. }