main.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. SandboxScene.add(%ground);
  100. // Create the grass.
  101. %grass = new Sprite();
  102. %grass.setBodyType("static");
  103. %grass.Image = "ToyAssets:grassForeground";
  104. %grass.setPosition(0, -8.5);
  105. %grass.setSize(PyramidToy.GroundWidth, 2);
  106. SandboxScene.add(%grass);
  107. }
  108. //-----------------------------------------------------------------------------
  109. function PyramidToy::createPyramid( %this )
  110. {
  111. // Fetch the block count.
  112. %blockCount = PyramidToy.BlockCount;
  113. // Sanity!
  114. if ( %blockCount < 2 )
  115. {
  116. echo( "Cannot have a pyramid block count less than two." );
  117. return;
  118. }
  119. // Set the block size.
  120. %blockSize = PyramidToy.BlockSize;
  121. // Calculate a block building position.
  122. %posx = %blockCount * -0.5 * %blockSize;
  123. %posy = -8.8 + (%blockSize * 0.5);
  124. // Build the stack of blocks.
  125. for( %stack = 0; %stack < %blockCount; %stack++ )
  126. {
  127. // Calculate the stack position.
  128. %stackIndexCount = %blockCount - (%stack*2);
  129. %stackX = %posX + ( %stack * %blockSize );
  130. %stackY = %posY + ( %stack * %blockSize );
  131. // Build the stack.
  132. for ( %stackIndex = 0; %stackIndex < %stackIndexCount; %stackIndex++ )
  133. {
  134. // Calculate the block position.
  135. %blockX = %stackX + (%stackIndex*%blockSize);
  136. %blockY = %stackY;
  137. // Create the sprite.
  138. %obj = new Sprite();
  139. %obj.setPosition( %blockX, %blockY );
  140. %obj.setSize( %blockSize );
  141. %obj.setImage( "ToyAssets:blocks" );
  142. %obj.setImageFrame( getRandom(0,55) );
  143. %obj.setDefaultFriction( 1.0 );
  144. %obj.createPolygonBoxCollisionShape( %blockSize, %blockSize );
  145. // Add to the scene.
  146. SandboxScene.add( %obj );
  147. }
  148. }
  149. }