main.cs 5.5 KB

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