A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.6 KiB

  1. using Microsoft.Xna.Framework;
  2. using Microsoft.Xna.Framework.Input;
  3. namespace SemiColinGames {
  4. readonly struct Input {
  5. public readonly Vector2 Motion;
  6. public readonly bool Jump;
  7. public readonly bool Attack;
  8. public readonly bool Pause;
  9. public readonly bool Debug;
  10. public readonly bool Exit;
  11. public readonly bool Restart;
  12. public readonly bool FullScreen;
  13. public Input(GamePadState gamePad, KeyboardState keyboard) {
  14. Motion = new Vector2();
  15. // We check for debugging buttons first. If any are pressed, we suppress normal input;
  16. // other button-presses correspond to special debugging things.
  17. Exit = keyboard.IsKeyDown(Keys.Escape);
  18. Restart = keyboard.IsKeyDown(Keys.F5);
  19. FullScreen = keyboard.IsKeyDown(Keys.F12);
  20. if (gamePad.IsButtonDown(Buttons.LeftShoulder) &&
  21. gamePad.IsButtonDown(Buttons.RightShoulder)) {
  22. Exit |= gamePad.IsButtonDown(Buttons.Start);
  23. Restart |= gamePad.IsButtonDown(Buttons.Back);
  24. FullScreen |= gamePad.IsButtonDown(Buttons.Y);
  25. }
  26. if (Exit || Restart || FullScreen) {
  27. Jump = false;
  28. Attack = false;
  29. Pause = false;
  30. Debug = false;
  31. return;
  32. }
  33. // Then we process normal buttons.
  34. Jump = gamePad.IsButtonDown(Buttons.A) || gamePad.IsButtonDown(Buttons.B) ||
  35. keyboard.IsKeyDown(Keys.J);
  36. Attack = gamePad.IsButtonDown(Buttons.X) || gamePad.IsButtonDown(Buttons.Y) ||
  37. keyboard.IsKeyDown(Keys.K);
  38. Debug = gamePad.IsButtonDown(Buttons.Back) || keyboard.IsKeyDown(Keys.OemMinus);
  39. Pause = gamePad.IsButtonDown(Buttons.Start) || keyboard.IsKeyDown(Keys.P);
  40. // Then potential motion directions. If the player attempts to input opposite directions at
  41. // once (up & down or left & right), those inputs cancel out, resulting in no motion.
  42. Vector2 leftStick = gamePad.ThumbSticks.Left;
  43. bool left = leftStick.X < -0.5 || gamePad.IsButtonDown(Buttons.DPadLeft) ||
  44. keyboard.IsKeyDown(Keys.A);
  45. bool right = leftStick.X > 0.5 || gamePad.IsButtonDown(Buttons.DPadRight) ||
  46. keyboard.IsKeyDown(Keys.D);
  47. bool up = leftStick.Y > 0.5 || gamePad.IsButtonDown(Buttons.DPadUp) ||
  48. keyboard.IsKeyDown(Keys.W);
  49. bool down = leftStick.Y < -0.5 || gamePad.IsButtonDown(Buttons.DPadDown) ||
  50. keyboard.IsKeyDown(Keys.S);
  51. if (left && !right) {
  52. Motion.X = -1;
  53. }
  54. if (right && !left) {
  55. Motion.X = 1;
  56. }
  57. if (up && !down) {
  58. Motion.Y = 1;
  59. }
  60. if (down && !up) {
  61. Motion.Y = -1;
  62. }
  63. }
  64. }
  65. }