main.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 MultiWindowToy::create( %this )
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pull );
  26. // Set the manipulation mode.
  27. Sandbox.useManipulation( pull );
  28. // Configure the toy.
  29. MultiWindowToy.MaxGems = 10;
  30. MultiWindowToy.MountSubWindow = true;
  31. MultiWindowToy.SubWindowBackground = true;
  32. // Add configuration option.
  33. addNumericOption("Max Balls", 1, 100, 10, "setMaxGems", MultiWindowToy.MaxGems, true, "Sets the number of gems created.");
  34. addFlagOption("Mount Sub-Window", "setMountSubWindow", MultiWindowToy.MountSubWindow, true, "Whether mount the sub-window to a random gem or not." );
  35. addFlagOption("Sub-Window Background", "setSubWindowBackground", MultiWindowToy.SubWindowBackground, true, "Whether render the sub-window background or not." );
  36. // Create a scene window.
  37. new SceneWindow(SandboxWindow2)
  38. {
  39. Profile = SandboxWindowProfile;
  40. Position = "64 64";
  41. Extent = "320 240";
  42. };
  43. // Set it to the scene.
  44. SandboxWindow2.setScene( SandboxScene );
  45. // Add it as a child window.
  46. SandboxWindow.add( SandboxWindow2 );
  47. // Add window to the toy so it is destroyed.
  48. MultiWindowToy.add( SandboxWindow2 );
  49. // Reset the toy.
  50. MultiWindowToy.reset();
  51. }
  52. //-----------------------------------------------------------------------------
  53. function MultiWindowToy::destroy( %this )
  54. {
  55. }
  56. //-----------------------------------------------------------------------------
  57. function MultiWindowToy::reset( %this )
  58. {
  59. // Clear the scene.
  60. SandboxScene.clear();
  61. // Create background.
  62. %this.createBackground();
  63. // Create gems.
  64. %this.createGems();
  65. // Reset the camera position if not mounted.
  66. if ( !MultiWindowToy.MountSubWindow )
  67. {
  68. SandboxWindow2.setCameraPosition( "0 0" );
  69. SandboxWindow2.setCameraAngle( 0 );
  70. }
  71. // Set the scene layers to render.
  72. if ( MultiWindowToy.SubWindowBackground )
  73. {
  74. // Render the layers #20 & #31 i.e. the gems and backgound.
  75. SandboxWindow2.setRenderLayers( "20 31" );
  76. }
  77. else
  78. {
  79. // Render the layer #20 only i.e. the gems.
  80. SandboxWindow2.setRenderLayers( "20" );
  81. }
  82. }
  83. //-----------------------------------------------------------------------------
  84. function MultiWindowToy::createBackground( %this )
  85. {
  86. // Create the sprite.
  87. %object = new Sprite();
  88. // Set the sprite as "static" so it is not affected by gravity.
  89. %object.BodyType = static;
  90. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  91. // Set the position.
  92. %object.Position = "0 0";
  93. // Set the size.
  94. %object.Size = "400 300";
  95. // Set to the furthest background layer.
  96. %object.SceneLayer = 31;
  97. // Set the scroller to use an animation!
  98. %object.Image = "ToyAssets:jungleSky";
  99. // Create border collisions.
  100. %object.createEdgeCollisionShape( -50, -37.5, -50, 37.5 );
  101. %object.createEdgeCollisionShape( 50, -37.5, 50, 37.5 );
  102. %object.createEdgeCollisionShape( -50, 37.5, 50, 37.5 );
  103. %object.createEdgeCollisionShape( -50, -34.5, 50, -34.5 );
  104. // Add the sprite to the scene.
  105. SandboxScene.add( %object );
  106. }
  107. //-----------------------------------------------------------------------------
  108. function MultiWindowToy::createGems( %this )
  109. {
  110. // Create balls.
  111. for( %n = 0; %n < MultiWindowToy.MaxGems; %n++ )
  112. {
  113. // Create the sprite.
  114. %object = new Sprite();
  115. // Set the position.
  116. %object.Position = getRandom(-40,40) SPC getRandom(-30,30);
  117. // If the size is to be square then we can simply pass a single value.
  118. // This applies to any 'Vector2' engine type.
  119. %object.Size = 5;
  120. // Set the layer.
  121. %object.SceneLayer = 20;
  122. // Create a circle collision shape.
  123. %object.setDefaultRestitution( 1 );
  124. %object.setDefaultFriction( 0.2 );
  125. %object.createCircleCollisionShape( 2 );
  126. // Set velocities.
  127. %object.SetLinearVelocity( getRandom(-40,40) SPC getRandom(-30,30) );
  128. %object.SetAngularVelocity( getRandom(-15,15) );
  129. // Add the sprite to the scene.
  130. SandboxScene.add( %object );
  131. // Mount to sub-window to the first object if selected.
  132. if ( MultiWindowToy.MountSubWindow == true && %n == 0 )
  133. {
  134. // Set the sprite to use an image.
  135. %object.Image = "ToyAssets:CrossHair2";
  136. %object.BlendColor = LimeGreen;
  137. // Mount to the object.
  138. SandboxWindow2.mount( %object, "0 0", 0, true, false );
  139. }
  140. else
  141. {
  142. // Set the sprite to use an image.
  143. %object.Image = "ToyAssets:Gems";
  144. %object.Frame = getRandom(0,63);
  145. }
  146. }
  147. }
  148. //-----------------------------------------------------------------------------
  149. function MultiWindowToy::setMaxGems(%this, %value)
  150. {
  151. %this.MaxGems = %value;
  152. }
  153. //-----------------------------------------------------------------------------
  154. function MultiWindowToy::setMountSubWindow(%this, %value)
  155. {
  156. %this.MountSubWindow = %value;
  157. }
  158. //-----------------------------------------------------------------------------
  159. function MultiWindowToy::setSubWindowBackground(%this, %value)
  160. {
  161. %this.SubWindowBackground = %value;
  162. }