ShipInput.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. public static void Update()
  15. {
  16. isAimingWithMouse = true;
  17. }
  18. public static Vector2 GetMovementDirection()
  19. {
  20. var input = AtomicNET.GetSubsystem<Input>();
  21. Vector2 direction = new Vector2(0, 0);
  22. if (input.GetKeyDown((int) SDL.SDL_Keycode.SDLK_a))
  23. direction.X -= 1;
  24. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_d))
  25. direction.X += 1;
  26. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_s))
  27. direction.Y -= 1;
  28. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_w))
  29. direction.Y += 1;
  30. uint numJoySticks = input.GetNumJoysticks();
  31. if (numJoySticks > 0)
  32. {
  33. var state = input.GetJoystickByIndex(0);
  34. float x = state.GetAxisPosition(0);
  35. float y = state.GetAxisPosition(1);
  36. if (x < -0.15f)
  37. direction.X = x;
  38. if (x > 0.15f)
  39. direction.X = x;
  40. if (y < -0.15f)
  41. direction.Y = -y;
  42. if (y > 0.15f)
  43. direction.Y = -y;
  44. }
  45. // Clamp the length of the vector to a maximum of 1.
  46. if (direction.LengthSquared > 1)
  47. direction.Normalize();
  48. return direction;
  49. }
  50. public static Vector2 GetAimDirection()
  51. {
  52. return GetMouseAimDirection();
  53. }
  54. private static Vector2 GetMouseAimDirection()
  55. {
  56. var input = AtomicNET.GetSubsystem<Input>();
  57. uint numJoySticks = input.GetNumJoysticks();
  58. if (numJoySticks > 0)
  59. {
  60. Vector2 dir = new Vector2(0, 0);
  61. var state = input.GetJoystickByIndex(0);
  62. float x = state.GetAxisPosition(0);
  63. float y = state.GetAxisPosition(1);
  64. if (x < -0.15f)
  65. dir.X = x;
  66. if (x > 0.15f)
  67. dir.X = x;
  68. if (y < -0.15f)
  69. dir.Y = -y;
  70. if (y > 0.15f)
  71. dir.Y = -y;
  72. // Clamp the length of the vector to a maximum of 1.
  73. if (dir.LengthSquared > 1)
  74. dir.Normalize();
  75. return dir;
  76. }
  77. Vector2 direction = new Vector2((float)input.GetMousePosition().X, (float)input.GetMousePosition().Y);
  78. Vector2 shipPos = PlayerShip.Instance.Position;
  79. shipPos.Y = GameRoot.ScreenBounds.Height - shipPos.Y;
  80. direction -= shipPos;
  81. direction.Y = -direction.Y;
  82. if (direction == Vector2.Zero)
  83. return Vector2.Zero;
  84. else
  85. return Vector2.Normalize(direction);
  86. }
  87. public static bool WasBombButtonPressed()
  88. {
  89. var input = AtomicNET.GetSubsystem<Input>();
  90. return input.GetKeyPress((int)SDL.SDL_Keycode.SDLK_SPACE);
  91. }
  92. }
  93. }