ShipInput.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using AtomicEngine;
  3. namespace AtomicBlaster
  4. {
  5. static class ShipInput
  6. {
  7. private static bool isAimingWithMouse = false;
  8. public static void Update()
  9. {
  10. isAimingWithMouse = true;
  11. }
  12. public static Vector2 GetMovementDirection()
  13. {
  14. var input = AtomicNET.GetSubsystem<Input>();
  15. Vector2 direction = new Vector2(0, 0);
  16. if (input.GetKeyDown((int) SDL.SDL_Keycode.SDLK_a))
  17. direction.X -= 1;
  18. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_d))
  19. direction.X += 1;
  20. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_s))
  21. direction.Y -= 1;
  22. if (input.GetKeyDown((int)SDL.SDL_Keycode.SDLK_w))
  23. direction.Y += 1;
  24. // Clamp the length of the vector to a maximum of 1.
  25. if (direction.LengthSquared > 1)
  26. direction.Normalize();
  27. return direction;
  28. }
  29. public static Vector2 GetAimDirection()
  30. {
  31. return GetMouseAimDirection();
  32. }
  33. private static Vector2 GetMouseAimDirection()
  34. {
  35. var input = AtomicNET.GetSubsystem<Input>();
  36. Vector2 direction = new Vector2((float)input.GetMousePosition().X, (float)input.GetMousePosition().Y);
  37. Vector2 shipPos = PlayerShip.Instance.Position;
  38. shipPos.Y = GameRoot.ScreenBounds.Height - shipPos.Y;
  39. direction -= shipPos;
  40. direction.Y = -direction.Y;
  41. if (direction == Vector2.Zero)
  42. return Vector2.Zero;
  43. else
  44. return Vector2.Normalize(direction);
  45. }
  46. public static bool WasBombButtonPressed()
  47. {
  48. var input = AtomicNET.GetSubsystem<Input>();
  49. return input.GetKeyPress((int)SDL.SDL_Keycode.SDLK_SPACE);
  50. }
  51. }
  52. }