main.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 RotateToToy::create( %this )
  23. {
  24. // Initialize the toys settings.
  25. RotateToToy.rotateSpeed = 360;
  26. RotateToToy.trackMouse = true;
  27. // Add the custom controls.
  28. addNumericOption("Rotate Speed", 1, 720, 1, "setRotateSpeed", RotateToToy.rotateSpeed, false, "Sets the angular speed to use to rotate to the target angle.");
  29. addFlagOption("Track Mouse", "setTrackMouse", RotateToToy.trackMouse, false, "Whether to track the angle to the mouse or not." );
  30. // Reset the toy initially.
  31. RotateToToy.reset();
  32. }
  33. //-----------------------------------------------------------------------------
  34. function RotateToToy::destroy( %this )
  35. {
  36. }
  37. //-----------------------------------------------------------------------------
  38. function RotateToToy::reset( %this )
  39. {
  40. // Clear the scene.
  41. SandboxScene.clear();
  42. // Create background.
  43. %this.createBackground();
  44. // Create the target.
  45. %this.createTarget();
  46. }
  47. //-----------------------------------------------------------------------------
  48. function RotateToToy::createBackground( %this )
  49. {
  50. // Create the sprite.
  51. %object = new Sprite();
  52. // Set the sprite as "static" so it is not affected by gravity.
  53. %object.setBodyType( static );
  54. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  55. // Set the position.
  56. %object.Position = "0 0";
  57. // Set the size.
  58. %object.Size = "100 75";
  59. // Set to the furthest background layer.
  60. %object.SceneLayer = 31;
  61. // Set an image.
  62. %object.Image = "ToyAssets:highlightBackground";
  63. // Set the blend color.
  64. %object.BlendColor = SlateGray;
  65. // Add the sprite to the scene.
  66. SandboxScene.add( %object );
  67. }
  68. //-----------------------------------------------------------------------------
  69. function RotateToToy::createTarget( %this )
  70. {
  71. // Create the sprite.
  72. %object = new Sprite();
  73. // Set the target.
  74. RotateToToy.TargetObject = %object;
  75. // Set the static image.
  76. %object.Image = "ToyAssets:hollowArrow";
  77. // Set a useful size.
  78. %object.Size = 30;
  79. // Add to the scene.
  80. SandboxScene.add( %object );
  81. }
  82. //-----------------------------------------------------------------------------
  83. function RotateToToy::setRotateSpeed( %this, %value )
  84. {
  85. %this.rotateSpeed = %value;
  86. }
  87. //-----------------------------------------------------------------------------
  88. function RotateToToy::setTrackMouse( %this, %value )
  89. {
  90. %this.trackMouse = %value;
  91. }
  92. //-----------------------------------------------------------------------------
  93. function RotateToToy::onTouchDown(%this, %touchID, %worldPosition)
  94. {
  95. %this.rotateTargetObject( %worldPosition );
  96. }
  97. //-----------------------------------------------------------------------------
  98. function RotateToToy::onTouchDragged(%this, %touchID, %worldPosition)
  99. {
  100. // Finish if not tracking the mouse.
  101. if ( !RotateToToy.trackMouse )
  102. return;
  103. %this.rotateTargetObject( %worldPosition );
  104. }
  105. //-----------------------------------------------------------------------------
  106. function RotateToToy::rotateTargetObject(%this, %worldPosition)
  107. {
  108. // Finish if not tracking the mouse.
  109. if ( !RotateToToy.trackMouse )
  110. return;
  111. // Calculate the angle to the mouse.
  112. %origin = RotateToToy.TargetObject.getPosition();
  113. %angle = mAtan( Vector2Sub( %worldPosition, %origin ) );
  114. // The target object points up (the 90-degree point in polar coordinates).
  115. // We can simply subtract 90 from the calculate angle to make the
  116. // object point to the mouse.
  117. %angle -= 90;
  118. //Rotate to the touched angle.
  119. RotateToToy.TargetObject.RotateTo( %angle, RotateToToy.rotateSpeed );
  120. }