breakoutDemo.cs 6.5 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 LeapToy::createBreakoutLevel( %this )
  23. {
  24. // Create background.
  25. %this.createBackground();
  26. // Create the ground.
  27. %this.createGround();
  28. // Create the breakable bricks
  29. %this.createBricks();
  30. %this.createPaddle();
  31. %this.createBreakOutBall();
  32. // Se the gravity.
  33. SandboxScene.setGravity( 0, 0);
  34. // Set the manipulation mode.
  35. Sandbox.useManipulation( off );
  36. // Swap action maps
  37. GestureMap.pop();
  38. FingerMap.pop();
  39. BreakoutMap.push();
  40. // Create the help text scene
  41. %helpText = new SimSet();
  42. %helpText.add(new ScriptObject() { Text = "Roll your hand back and forth to control the paddle"; });
  43. %helpText.add(new ScriptObject() { Text = " "; });
  44. %helpText.add(new ScriptObject() { Text = "When a block is hit twice, it will be destroyed"; });
  45. %helpText.add(new ScriptObject() { Text = " "; });
  46. %helpText.add(new ScriptObject() { Text = "Press H to return to the demo."; });
  47. %this.createHelpTextScene(%helpText);
  48. %helpText.delete();
  49. }
  50. //-----------------------------------------------------------------------------
  51. function LeapToy::createBricks( %this )
  52. {
  53. // Fetch the block count.
  54. %brickCount = LeapToy.BrickRows;
  55. %brickColumsn = LeapToy.BrickColumns;
  56. // Sanity!
  57. if ( %brickCount < 1 )
  58. {
  59. echo( "Cannot have a brick count count less than one." );
  60. return;
  61. }
  62. // Set the block size.
  63. %brickSize = LeapToy.BrickSize;
  64. // Calculate a block building position.
  65. %posx = %brickCount * %brickSize._0 * -1;
  66. %posy = 3 + (%brickSize._1 * 0.5) + 3;
  67. // Build the stack of blocks.
  68. for( %stack = 0; %stack < %brickCount; %stack++ )
  69. {
  70. // Calculate the stack position.
  71. %stackIndexCount = %brickCount;
  72. %stackX = %posX + ( %stack * %brickSize._0 );
  73. %stackY = %posY + ( %stack * %brickSize._1 );
  74. // Build the stack.
  75. for ( %stackIndex = 0; %stackIndex < LeapToy.BrickColumns; %stackIndex++ )
  76. {
  77. // Calculate the block position.
  78. %brickX = (%stackIndex*%brickSize._0)+%posx;
  79. %brickY = %stackY;
  80. %brickFrames = "0 2 4 6 8 10";
  81. %randomNumber = getRandom(0, 5);
  82. %frame = getWord(%brickFrames, %randomNumber);
  83. // Create the sprite.
  84. %obj = new Sprite()
  85. {
  86. class = "Brick";
  87. };
  88. %obj.setPosition( %brickX, %brickY );
  89. %obj.setSize( %brickSize );
  90. %obj.setBodyType("Kinematic");
  91. %obj.setImage( "LeapToy:objectsBricks", %frame );
  92. %obj.setDefaultFriction( 1.0 );
  93. %obj.createPolygonBoxCollisionShape( %brickSize );
  94. %obj.CollisionCallback = true;
  95. %takesDamage = %this.TakesDamageBehavior.createInstance();
  96. %takesDamage.initialize(20, 100, 10, 0, "", "LeapToy:blockFadeParticle", %frame);
  97. %obj.addBehavior(%takesDamage);
  98. %swapImage = %this.SwapImageBehavior.createInstance();
  99. %swapImage.initialize("LeapToy:objectsBricks", %frame+1);
  100. %obj.addBehavior(%swapImage);
  101. // Add to the scene.
  102. SandboxScene.add( %obj );
  103. }
  104. }
  105. }
  106. //-----------------------------------------------------------------------------
  107. function LeapToy::createPaddle(%this)
  108. {
  109. // Create the player paddle
  110. %obj = new Sprite();
  111. %obj.setPosition( 0, -8);
  112. %obj.setSize( 5, 2.5 );
  113. %obj.setImage( "LeapToy:playerUfo");
  114. %obj.setDefaultFriction( 0 );
  115. %obj.createPolygonBoxCollisionShape( 4.8, 2.3);
  116. %obj.CollisionCallback = true;
  117. %obj.setFixedAngle( true );
  118. %this.paddle = %obj;
  119. SandboxScene.add(%obj);
  120. }
  121. //-----------------------------------------------------------------------------
  122. function LeapToy::createBreakoutBall(%this)
  123. {
  124. // Create the ball.
  125. %ball = new Sprite()
  126. {
  127. class = "Ball";
  128. };
  129. %ball.Position = "5 5";
  130. %ball.Size = 2;
  131. %ball.Image = "LeapToy:widgetBall";
  132. %ball.setDefaultDensity( 1 );
  133. %ball.setDefaultRestitution( 1.0 );
  134. %ball.setDefaultFriction(0);
  135. %ball.createCircleCollisionShape( 0.8 );
  136. %ball.CollisionCallback = true;
  137. %ball.setLinearVelocity(-5, 20);
  138. %ball.setPosition(-5, -5);
  139. %dealsDamage = %this.DealsDamageBehavior.createInstance();
  140. %dealsDamage.initialize(10, false, "");
  141. %ball.addBehavior(%dealsDamage);
  142. %this.breakoutBall = %ball;
  143. // Add to the scene.
  144. SandboxScene.add( %ball );
  145. }
  146. //-----------------------------------------------------------------------------
  147. function Ball::onCollision(%this, %object, %collisionDetails)
  148. {
  149. %xVelocity = %this.getLinearVelocity()._0;
  150. %yVelocity = %this.getLinearVelocity()._1;
  151. %newXVelocity = mClamp(%xVelocity, (LeapToy.maxBallSpeed*-1), LeapToy.maxBallSpeed);
  152. %newYVelocity = mClamp(%yVelocity, (LeapToy.maxBallSpeed*-1), LeapToy.maxBallSpeed);
  153. %this.setLinearVelocity(%newXVelocity, %newYVelocity);
  154. }
  155. //-----------------------------------------------------------------------------
  156. function LeapToy::movePaddle(%this, %speed)
  157. {
  158. %this.paddle.setLinearVelocity(%speed, 0);
  159. }