2020-01-18 03:41:45 +00:00
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
|
|
namespace SemiColinGames {
|
2020-02-25 14:35:41 +00:00
|
|
|
readonly struct Input {
|
|
|
|
public readonly Vector2 Motion;
|
|
|
|
public readonly bool Jump;
|
|
|
|
public readonly bool Attack;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
2020-02-25 14:35:41 +00:00
|
|
|
public readonly bool Pause;
|
|
|
|
public readonly bool Debug;
|
|
|
|
public readonly bool Exit;
|
|
|
|
public readonly bool Restart;
|
|
|
|
public readonly bool FullScreen;
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
public Input(GamePadState gamePad, KeyboardState keyboard) {
|
2020-02-25 14:25:19 +00:00
|
|
|
Motion = new Vector2();
|
|
|
|
|
|
|
|
// We check for debugging buttons first. If any are pressed, we suppress normal input;
|
|
|
|
// other button-presses correspond to special debugging things.
|
|
|
|
Exit = keyboard.IsKeyDown(Keys.Escape);
|
|
|
|
Restart = keyboard.IsKeyDown(Keys.F5);
|
|
|
|
FullScreen = keyboard.IsKeyDown(Keys.F12);
|
|
|
|
if (gamePad.IsButtonDown(Buttons.LeftShoulder) &&
|
|
|
|
gamePad.IsButtonDown(Buttons.RightShoulder)) {
|
|
|
|
Exit |= gamePad.IsButtonDown(Buttons.Start);
|
|
|
|
Restart |= gamePad.IsButtonDown(Buttons.Back);
|
|
|
|
FullScreen |= gamePad.IsButtonDown(Buttons.Y);
|
|
|
|
}
|
|
|
|
if (Exit || Restart || FullScreen) {
|
|
|
|
Jump = false;
|
|
|
|
Attack = false;
|
|
|
|
Pause = false;
|
|
|
|
Debug = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we process normal buttons.
|
2020-01-18 03:41:45 +00:00
|
|
|
Jump = gamePad.IsButtonDown(Buttons.A) || gamePad.IsButtonDown(Buttons.B) ||
|
|
|
|
keyboard.IsKeyDown(Keys.J);
|
|
|
|
Attack = gamePad.IsButtonDown(Buttons.X) || gamePad.IsButtonDown(Buttons.Y) ||
|
|
|
|
keyboard.IsKeyDown(Keys.K);
|
|
|
|
|
2020-02-19 19:57:32 +00:00
|
|
|
Debug = gamePad.IsButtonDown(Buttons.Back) || keyboard.IsKeyDown(Keys.OemMinus);
|
2020-01-24 22:49:29 +00:00
|
|
|
Pause = gamePad.IsButtonDown(Buttons.Start) || keyboard.IsKeyDown(Keys.Pause);
|
2020-01-18 03:41:45 +00:00
|
|
|
|
|
|
|
// Then potential motion directions. If the player attempts to input opposite directions at
|
|
|
|
// once (up & down or left & right), those inputs cancel out, resulting in no motion.
|
|
|
|
Vector2 leftStick = gamePad.ThumbSticks.Left;
|
|
|
|
bool left = leftStick.X < -0.5 || gamePad.IsButtonDown(Buttons.DPadLeft) ||
|
|
|
|
keyboard.IsKeyDown(Keys.A);
|
|
|
|
bool right = leftStick.X > 0.5 || gamePad.IsButtonDown(Buttons.DPadRight) ||
|
|
|
|
keyboard.IsKeyDown(Keys.D);
|
|
|
|
bool up = leftStick.Y > 0.5 || gamePad.IsButtonDown(Buttons.DPadUp) ||
|
|
|
|
keyboard.IsKeyDown(Keys.W);
|
|
|
|
bool down = leftStick.Y < -0.5 || gamePad.IsButtonDown(Buttons.DPadDown) ||
|
|
|
|
keyboard.IsKeyDown(Keys.S);
|
|
|
|
if (left && !right) {
|
|
|
|
Motion.X = -1;
|
|
|
|
}
|
|
|
|
if (right && !left) {
|
|
|
|
Motion.X = 1;
|
|
|
|
}
|
|
|
|
if (up && !down) {
|
|
|
|
Motion.Y = 1;
|
|
|
|
}
|
|
|
|
if (down && !up) {
|
|
|
|
Motion.Y = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|