main.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 CompoundObjectsToy::create( %this )
  23. {
  24. // Configure the toy.
  25. CompoundObjectsToy.BlockSize = 1.5;
  26. CompoundObjectsToy.BlockCount = 15;
  27. CompoundObjectsToy.GroundWidth = 40;
  28. // Set the camera.
  29. SandboxWindow.setCameraSize( 40, 30 );
  30. // Se the gravity.
  31. SandboxScene.setGravity( 0, -9.8 );
  32. // Reset the toy.
  33. CompoundObjectsToy.reset();
  34. }
  35. //-----------------------------------------------------------------------------
  36. function CompoundObjectsToy::destroy( %this )
  37. {
  38. }
  39. //-----------------------------------------------------------------------------
  40. function CompoundObjectsToy::reset( %this )
  41. {
  42. // Clear the scene.
  43. SandboxScene.clear();
  44. // Create a background.
  45. %this.createBackground();
  46. // Create the ground.
  47. %this.createGround();
  48. }
  49. //-----------------------------------------------------------------------------
  50. function CompoundObjectsToy::createBackground( %this )
  51. {
  52. // Create the sprite.
  53. %object = new Sprite();
  54. // Set the sprite as "static" so it is not affected by gravity.
  55. %object.setBodyType( static );
  56. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  57. // Set the size.
  58. %object.Size = CompoundObjectsToy.GroundWidth SPC (CompoundObjectsToy.GroundWidth * 0.75);
  59. // Set the position.
  60. %object.setPositionY( (%object.getSizeY() * 0.5) - 15 );
  61. // Set to the furthest background layer.
  62. %object.SceneLayer = 31;
  63. // Set an image.
  64. %object.Image = "ToyAssets:jungleSky";
  65. // Add the sprite to the scene.
  66. SandboxScene.add( %object );
  67. }
  68. //-----------------------------------------------------------------------------
  69. function CompoundObjectsToy::createGround( %this )
  70. {
  71. // Create the ground
  72. %ground = SandboxScene.create( Scroller );
  73. %ground.BodyType = static;
  74. %ground.Image = "ToyAssets:dirtGround";
  75. %ground.SceneGroup = 10;
  76. %ground.setPosition(0, -12);
  77. %ground.setSize(CompoundObjectsToy.GroundWidth, 6);
  78. %ground.setRepeatX(CompoundObjectsToy.GroundWidth / 60);
  79. %ground.createEdgeCollisionShape(CompoundObjectsToy.GroundWidth/-2, 3, CompoundObjectsToy.GroundWidth/2, 3);
  80. %ground.createEdgeCollisionShape(CompoundObjectsToy.GroundWidth/-2, 3, CompoundObjectsToy.GroundWidth/-2, 40 );
  81. %ground.createEdgeCollisionShape(CompoundObjectsToy.GroundWidth/2, 3, CompoundObjectsToy.GroundWidth/2, 40 );
  82. // Create the grass.
  83. %grass = SandboxScene.create( Sprite );
  84. %grass.BodyType = static;
  85. %grass.Image = "ToyAssets:grassForeground";
  86. %grass.SetPosition(0, -8.5);
  87. %grass.setSize(CompoundObjectsToy.GroundWidth, 2);
  88. }
  89. //-----------------------------------------------------------------------------
  90. function CompoundObjectsToy::createCompoundObject( %this, %worldPosition )
  91. {
  92. // Configure the compound object.
  93. %radius = 2;
  94. %angleStride = 15;
  95. %blockSize = 1;
  96. // Create the composite.
  97. %composite = SandboxScene.create( CompositeSprite );
  98. // Turn-off batch culling to save memory as this is a small composite.
  99. %composite.BatchCulling = false;
  100. // Turn-off batch layout as these sprites will be positioned explicitly.
  101. %composite.BatchLayout = "off";
  102. // Render everything together, don't sort the sprites with the rest of the scene layer.
  103. %composite.BatchIsolated = true;
  104. // Set the position.
  105. %composite.Position = %worldPosition;
  106. // Set the scene layer (behind the grass).
  107. %composite.SceneLayer = 1;
  108. // Create compound ring.
  109. for( %angle = 0; %angle < 360; %angle += %angleStride )
  110. {
  111. %spriteX = mCos( %angle ) * %radius;
  112. %spriteY = mSin( %angle ) * %radius;
  113. %composite.addSprite();
  114. %composite.setSpriteLocalPosition( %spriteX, %spriteY );
  115. %composite.setSpriteSize( %blockSize );
  116. %composite.setSpriteAngle( %angle );
  117. %composite.setSpriteImage( "ToyAssets:Blocks" );
  118. %composite.setSpriteImageFrame( getRandom(0,55) );
  119. }
  120. // Add center sprite.
  121. %composite.addSprite();
  122. %composite.setSpriteSize( %radius * 2 );
  123. %composite.setSpriteAnimation( "ToyAssets:TD_Barbarian_WalkSouth" );
  124. // Set the collision shape defaults.
  125. %composite.setDefaultFriction( 0.25 );
  126. %composite.setDefaultRestitution( 0.75 );
  127. // Create a collision shape.
  128. %composite.createCircleCollisionShape( %radius + (%blockSize * 0.5 ) );
  129. }
  130. //-----------------------------------------------------------------------------
  131. function CompoundObjectsToy::onTouchDown(%this, %touchID, %worldPosition)
  132. {
  133. %this.createCompoundObject( %worldPosition );
  134. }