ShipInput.cs 2.2 KB

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