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.

84 lines
3.0 KiB

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