ShipInput.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //---------------------------------------------------------------------------------
  2. // Ported to the Atomic Game Engine
  3. // Originally written for XNA by Michael Hoffman
  4. // Find the full tutorial at: http://gamedev.tutsplus.com/series/vector-shooter-xna/
  5. //----------------------------------------------------------------------------------
  6. using System;
  7. using System.IO;
  8. using AtomicEngine;
  9. namespace AtomicBlaster
  10. {
  11. static class ShipInput
  12. {
  13. private static bool isAimingWithMouse = false;
  14. static IntVector2 lastTouchPos = IntVector2.Zero;
  15. public static void Update()
  16. {
  17. isAimingWithMouse = true;
  18. }
  19. public static Vector2 GetMovementDirection()
  20. {
  21. var input = AtomicNET.GetSubsystem<Input>();
  22. Vector2 direction = new Vector2(0, 0);
  23. #if ATOMIC_DESKTOP
  24. if (input.GetKeyDown((int) SDL.SDL_Keycode.SDLK_a))
  25. direction.X -= 1;
  26. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_d))
  27. direction.X += 1;
  28. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_s))
  29. direction.Y -= 1;
  30. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_w))
  31. direction.Y += 1;
  32. #endif
  33. #if ATOMIC_MOBILE
  34. var touchPos = lastTouchPos;
  35. if (input.NumTouches == 1)
  36. {
  37. touchPos = input.GetTouchPosition(0);
  38. lastTouchPos = touchPos;
  39. }
  40. if (touchPos != IntVector2.Zero)
  41. {
  42. direction = new Vector2((float)touchPos.X, (float)touchPos.Y);
  43. }
  44. Vector2 shipPos = PlayerShip.Instance.Position;
  45. shipPos.Y = GameRoot.ScreenBounds.Height - shipPos.Y;
  46. direction -= shipPos;
  47. direction.Y = -direction.Y;
  48. if (touchPos == IntVector2.Zero || direction.Length < 4.0f)
  49. direction = Vector2.Zero;
  50. #endif
  51. #if ATOMIC_IOS
  52. uint numJoySticks = 0;
  53. #else
  54. uint numJoySticks = input.GetNumJoysticks();
  55. #endif
  56. if (numJoySticks > 0)
  57. {
  58. var state = input.GetJoystickByIndex(0);
  59. float x = state.GetAxisPosition(0);
  60. float y = state.GetAxisPosition(1);
  61. if (x < -0.15f)
  62. direction.X = x;
  63. if (x > 0.15f)
  64. direction.X = x;
  65. if (y < -0.15f)
  66. direction.Y = -y;
  67. if (y > 0.15f)
  68. direction.Y = -y;
  69. }
  70. // Clamp the length of the vector to a maximum of 1.
  71. if (direction.LengthSquared > 1)
  72. direction.Normalize();
  73. return direction;
  74. }
  75. public static Vector2 GetAimDirection()
  76. {
  77. return GetMouseAimDirection();
  78. }
  79. private static Vector2 GetMouseAimDirection()
  80. {
  81. var input = AtomicNET.GetSubsystem<Input>();
  82. Vector2 direction = new Vector2();
  83. #if ATOMIC_DESKTOP
  84. direction = new Vector2((float)input.GetMousePosition().X, (float)input.GetMousePosition().Y);
  85. #endif
  86. #if ATOMIC_IOS
  87. uint numJoySticks = 0;
  88. #else
  89. uint numJoySticks = input.GetNumJoysticks();
  90. #endif
  91. if (numJoySticks > 0)
  92. {
  93. Vector2 dir = new Vector2(0, 0);
  94. var state = input.GetJoystickByIndex(0);
  95. float x = state.GetAxisPosition(0);
  96. float y = state.GetAxisPosition(1);
  97. if (x < -0.15f)
  98. dir.X = x;
  99. if (x > 0.15f)
  100. dir.X = x;
  101. if (y < -0.15f)
  102. dir.Y = -y;
  103. if (y > 0.15f)
  104. dir.Y = -y;
  105. // Clamp the length of the vector to a maximum of 1.
  106. if (dir.LengthSquared > 1)
  107. dir.Normalize();
  108. return dir;
  109. }
  110. #if !ATOMIC_MOBILE
  111. direction = new Vector2((float)input.GetMousePosition().X, (float)input.GetMousePosition().Y);
  112. #else
  113. {
  114. direction = PlayerShip.Instance.Velocity;
  115. return Vector2.Normalize(direction);
  116. }
  117. #endif
  118. Vector2 shipPos = PlayerShip.Instance.Position;
  119. shipPos.Y = GameRoot.ScreenBounds.Height - shipPos.Y;
  120. direction -= shipPos;
  121. direction.Y = -direction.Y;
  122. if (direction == Vector2.Zero)
  123. return Vector2.Zero;
  124. else
  125. return Vector2.Normalize(direction);
  126. }
  127. public static bool WasBombButtonPressed()
  128. {
  129. var input = AtomicNET.GetSubsystem<Input>();
  130. return input.GetKeyPress((int)SDL.SDL_Keycode.SDLK_SPACE);
  131. }
  132. }
  133. }