main.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 AlphaBlendToy::create( %this )
  23. {
  24. // Configure the toy.
  25. AlphaBlendToy.SpriteCount = 0;
  26. AlphaBlendToy.SpriteSize = 10;
  27. // Add the custom controls.
  28. addNumericOption("SpriteSize", 1, 30, 1, "setSpriteSize", AlphaBlendToy.SpriteSize, true, "Sets the size of the sprite.");
  29. // Reset the toy initially.
  30. AlphaBlendToy.reset();
  31. }
  32. //-----------------------------------------------------------------------------
  33. function AlphaBlendToy::destroy( %this )
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. function AlphaBlendToy::reset( %this )
  38. {
  39. // Reset the sprite count.
  40. AlphaBlendToy.SpriteCount = 0;
  41. // Calculate the sprite configuration.
  42. AlphaBlendToy.SpriteCountX = mFloor(100.0 / AlphaBlendToy.SpriteSize);
  43. AlphaBlendToy.SpriteCountY = mFloor(75.0 / AlphaBlendToy.SpriteSize);
  44. AlphaBlendToy.SpriteCountTotal = AlphaBlendToy.SpriteCountX * AlphaBlendToy.SpriteCountY;
  45. AlphaBlendToy.SpriteAlphaStride = 1.0 / AlphaBlendToy.SpriteCountTotal;
  46. AlphaBlendToy.CursorX = 0;
  47. AlphaBlendToy.CursorY = 0;
  48. AlphaBlendToy.CursorAlpha = 1.0;
  49. // Clear the scene.
  50. SandboxScene.clear();
  51. // Create background.
  52. %this.createBackground();
  53. // Start the timer.
  54. AlphaBlendToy.startTimer( "createBlendedSprites", 50 );
  55. }
  56. //-----------------------------------------------------------------------------
  57. function AlphaBlendToy::createBackground( %this )
  58. {
  59. // Create the sprite.
  60. %object = new Scroller();
  61. // Set the sprite as "static" so it is not affected by gravity.
  62. %object.setBodyType( static );
  63. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  64. // Set the position.
  65. %object.Position = "0 0";
  66. // Set the size.
  67. %object.Size = "100 75";
  68. // Set to the furthest background layer.
  69. %object.SceneLayer = 31;
  70. // Set an image.
  71. %object.Image = "ToyAssets:checkered";
  72. // Repeat the image.
  73. %object.RepeatX = 4;
  74. %object.RepeatY = 3;
  75. // Scroll the image.
  76. %object.ScrollX = 4;
  77. %object.ScrollY = 3;
  78. // Set the blend color.
  79. %object.BlendColor = DimGray;
  80. // Add the sprite to the scene.
  81. SandboxScene.add( %object );
  82. }
  83. //-----------------------------------------------------------------------------
  84. function AlphaBlendToy::createBlendedSprites( %this )
  85. {
  86. // Finish if count reached.
  87. if ( AlphaBlendToy.SpriteCount >= AlphaBlendToy.SpriteCountTotal )
  88. {
  89. // Stop the timer.
  90. AlphaBlendToy.stopTimer();
  91. return;
  92. }
  93. // Create the sprite.
  94. %object = new Sprite();
  95. // Calculate sprite position.
  96. %halfSize = AlphaBlendToy.SpriteSize * 0.5;
  97. %positionX = -50 + (AlphaBlendToy.CursorX * AlphaBlendToy.SpriteSize) + %halfSize;
  98. %positionY = 37.5-(AlphaBlendToy.CursorY * AlphaBlendToy.SpriteSize) - %halfSize;
  99. // Set the position.
  100. %object.setPosition( %positionX, %positionY );
  101. // Set a useful size.
  102. %object.Size = AlphaBlendToy.SpriteSize;
  103. // Set the animation.
  104. %object.Animation = "ToyAssets:TD_Wizard_WalkSouth";
  105. // Set the transparency.
  106. %object.setBlendAlpha( AlphaBlendToy.CursorAlpha );
  107. // Add to the scene.
  108. SandboxScene.add( %object );
  109. // Update the sprite cursor position.
  110. AlphaBlendToy.CursorX++;
  111. if ( AlphaBlendToy.CursorX == AlphaBlendToy.SpriteCountX )
  112. {
  113. AlphaBlendToy.CursorX = 0;
  114. AlphaBlendToy.CursorY++;
  115. if ( AlphaBlendToy.CursorY == AlphaBlendToy.SpriteCountY )
  116. {
  117. // Stop the timer.
  118. AlphaBlendToy.stopTimer();
  119. return;
  120. }
  121. }
  122. // Update the sprite cursor alpha.
  123. AlphaBlendToy.CursorAlpha -= AlphaBlendToy.SpriteAlphaStride ;
  124. // Increase the sprite count.
  125. AlphaBlendToy.SpriteCount++;
  126. // Finish if count reached.
  127. if ( AlphaBlendToy.SpriteCount == AlphaBlendToy.SpriteCountTotal )
  128. {
  129. // Stop the timer.
  130. AlphaBlendToy.stopTimer();
  131. }
  132. }
  133. //-----------------------------------------------------------------------------
  134. function AlphaBlendToy::setSpriteSize( %this, %value )
  135. {
  136. %this.SpriteSize = %value;
  137. }