main.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 MoveToToy::create( %this )
  23. {
  24. // Activate the package.
  25. activatePackage( MoveToToyPackage );
  26. // Initialize the toys settings.
  27. MoveToToy.moveSpeed = 50;
  28. MoveToToy.trackMouse = true;
  29. // Add the custom controls.
  30. addNumericOption("Move Speed", 1, 150, 1, "setMoveSpeed", MoveToToy.moveSpeed, true, "Sets the linear speed to use when moving to the target position.");
  31. addFlagOption("Track Mouse", "setTrackMouse", MoveToToy.trackMouse, false, "Whether to track the position of the mouse or not." );
  32. // Reset the toy initially.
  33. MoveToToy.reset();
  34. }
  35. //-----------------------------------------------------------------------------
  36. function MoveToToy::destroy( %this )
  37. {
  38. // Deactivate the package.
  39. deactivatePackage( MoveToToyPackage );
  40. }
  41. //-----------------------------------------------------------------------------
  42. function MoveToToy::reset( %this )
  43. {
  44. // Clear the scene.
  45. SandboxScene.clear();
  46. // Create background.
  47. %this.createBackground();
  48. // Create target.
  49. %this.createTarget();
  50. // Create sight.
  51. %this.createSight();
  52. }
  53. //-----------------------------------------------------------------------------
  54. function MoveToToy::createBackground( %this )
  55. {
  56. // Create the sprite.
  57. %object = new Sprite();
  58. // Set the sprite as "static" so it is not affected by gravity.
  59. %object.setBodyType( static );
  60. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  61. // Set the position.
  62. %object.Position = "0 0";
  63. // Set the size.
  64. %object.Size = "100 75";
  65. // Set to the furthest background layer.
  66. %object.SceneLayer = 31;
  67. // Set an image.
  68. %object.Image = "ToyAssets:highlightBackground";
  69. // Set the blend color.
  70. %object.BlendColor = SlateGray;
  71. // Add the sprite to the scene.
  72. SandboxScene.add( %object );
  73. }
  74. //-----------------------------------------------------------------------------
  75. function MoveToToy::createSight( %this )
  76. {
  77. // Create the sprite.
  78. %object = new Sprite();
  79. // Set the sight object.
  80. MoveToToy.SightObject = %object;
  81. // Set the static image.
  82. %object.Image = "ToyAssets:Crosshair2";
  83. // Set the blend color.
  84. %object.BlendColor = Lime;
  85. // Set the transparency.
  86. %object.setBlendAlpha( 0.5 );
  87. // Set a useful size.
  88. %object.Size = 40;
  89. // Set the sprite rotating to make it more interesting.
  90. %object.AngularVelocity = -90;
  91. // Add to the scene.
  92. SandboxScene.add( %object );
  93. }
  94. //-----------------------------------------------------------------------------
  95. function MoveToToy::createTarget( %this )
  96. {
  97. // Create the sprite.
  98. %object = new Sprite();
  99. // Set the target object.
  100. MoveToToy.TargetObject = %object;
  101. // Set the static image.
  102. %object.Image = "ToyAssets:Crosshair3";
  103. // Set the blend color.
  104. %object.BlendColor = DarkOrange;
  105. // Set a useful size.
  106. %object.Size = 20;
  107. // Set the sprite rotating to make it more interesting.
  108. %object.AngularVelocity = 60;
  109. // Add to the scene.
  110. SandboxScene.add( %object );
  111. }
  112. //-----------------------------------------------------------------------------
  113. function MoveToToy::setMoveSpeed( %this, %value )
  114. {
  115. %this.moveSpeed = %value;
  116. }
  117. //-----------------------------------------------------------------------------
  118. function MoveToToy::setTrackMouse( %this, %value )
  119. {
  120. %this.trackMouse = %value;
  121. }
  122. //-----------------------------------------------------------------------------
  123. package MoveToToyPackage
  124. {
  125. function SandboxWindow::onTouchDown(%this, %touchID, %worldPosition)
  126. {
  127. // Call parent.
  128. Parent::onTouchDown(%this, %touchID, %worldPosition );
  129. // Set the target to the touched position.
  130. MoveToToy.TargetObject.Position = %worldPosition;
  131. // Move the sight to the touched position.
  132. MoveToToy.SightObject.MoveTo( %worldPosition, MoveToToy.moveSpeed );
  133. }
  134. //-----------------------------------------------------------------------------
  135. function SandboxWindow::onTouchMoved(%this, %touchID, %worldPosition)
  136. {
  137. // Call parent.
  138. Parent::onTouchMoved(%this, %touchID, %worldPosition );
  139. // Finish if not tracking the mouse.
  140. if ( !MoveToToy.trackMouse )
  141. return;
  142. // Set the target to the touched position.
  143. MoveToToy.TargetObject.Position = %worldPosition;
  144. // Move the sight to the touched position.
  145. MoveToToy.SightObject.MoveTo( %worldPosition, MoveToToy.moveSpeed );
  146. }
  147. };