Browse Source

Add Input class to group gamepad & keyboard inputs together.

For motion directions (up/down & left/right), have them cancel each other out
if the player attempts to go in opposite directions at once.

Refactor Player & SneakGame to use the new Input class & remove direct access
to Keyboard & GamePad.

GitOrigin-RevId: 80fbed8874
master
Colin McMillen 4 years ago
parent
commit
db6f3e1425
  1. 51
      Shared/Input.cs
  2. 30
      Shared/Player.cs
  3. 1
      Shared/Shared.projitems
  4. 15
      Shared/SneakGame.cs

51
Shared/Input.cs

@ -0,0 +1,51 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
namespace SemiColinGames {
struct Input {
public bool Jump;
public bool Attack;
public Vector2 Motion;
public bool Exit;
public bool FullScreen;
public bool Debug;
public Input(GamePadState gamePad, KeyboardState keyboard) {
// First we process normal buttons.
Jump = gamePad.IsButtonDown(Buttons.A) || keyboard.IsKeyDown(Keys.J);
Attack = gamePad.IsButtonDown(Buttons.X) || keyboard.IsKeyDown(Keys.K);
// Then special debugging sorts of buttons.
Exit = gamePad.IsButtonDown(Buttons.Start) || keyboard.IsKeyDown(Keys.Escape);
FullScreen = gamePad.IsButtonDown(Buttons.Back) || keyboard.IsKeyDown(Keys.F12) ||
keyboard.IsKeyDown(Keys.OemPlus);
Debug = gamePad.IsButtonDown(Buttons.LeftShoulder) || keyboard.IsKeyDown(Keys.OemMinus);
// 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.
Motion = new Vector2();
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;
}
}
}
}

30
Shared/Player.cs

@ -35,12 +35,10 @@ namespace SemiColinGames {
return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26);
}
public void Update(
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard,
List<Rectangle> collisionTargets) {
public void Update(GameTime time, History<Input> input, List<Rectangle> collisionTargets) {
Point oldPosition = position;
AirState oldAirState = airState;
UpdateFromInput(time, gamePad, keyboard);
HandleInput(time, input);
Rectangle oldBbox = Bbox(oldPosition);
Rectangle playerBbox = Bbox(position);
@ -97,11 +95,8 @@ namespace SemiColinGames {
}
}
void UpdateFromInput(
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard) {
if ((gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) ||
keyboard[0].IsKeyDown(Keys.J) && keyboard[1].IsKeyUp(Keys.J)) &&
airState == AirState.Ground) {
void HandleInput(GameTime time, History<Input> input) {
if (input[0].Jump && !input[1].Jump && airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;
jumpTime = 0.5;
@ -109,30 +104,23 @@ namespace SemiColinGames {
return;
}
if ((gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X) ||
keyboard[0].IsKeyDown(Keys.K) && keyboard[1].IsKeyUp(Keys.K))
&& swordSwingTime <= 0) {
if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) {
pose = Pose.SwordSwing;
swordSwingTime = 0.3;
return;
}
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5 ||
keyboard[0].IsKeyDown(Keys.A)) {
if (input[0].Motion.X < 0) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5 ||
keyboard[0].IsKeyDown(Keys.D)) {
} else if (input[0].Motion.X > 0) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5 ||
keyboard[0].IsKeyDown(Keys.S)) {
} else if (input[0].Motion.Y < 0) {
pose = Pose.Crouching;
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5 ||
keyboard[0].IsKeyDown(Keys.W)) {
} else if (input[0].Motion.Y > 0) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;

1
Shared/Shared.projitems

@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Camera.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Input.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Debug.cs" />
<Compile Include="$(MSBuildThisFileDirectory)FpsCounter.cs" />
<Compile Include="$(MSBuildThisFileDirectory)History.cs" />

15
Shared/SneakGame.cs

@ -17,6 +17,7 @@ namespace SemiColinGames {
History<KeyboardState> keyboard = new History<KeyboardState>(2);
History<GamePadState> gamePad = new History<GamePadState>(2);
History<Input> input = new History<Input>(2);
FpsCounter fpsCounter = new FpsCounter();
Texture2D grasslandBg1;
@ -66,27 +67,23 @@ namespace SemiColinGames {
protected override void Update(GameTime gameTime) {
Debug.Clear();
gamePad.Add(GamePad.GetState(PlayerIndex.One));
keyboard.Add(Keyboard.GetState());
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
if (input[0].Exit) {
Exit();
}
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
if (input[0].FullScreen && !input[1].FullScreen) {
fullScreen = !fullScreen;
display.SetFullScreen(fullScreen);
}
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
if (input[0].Debug && !input[1].Debug) {
Debug.Enabled = !Debug.Enabled;
}
List<Rectangle> collisionTargets = world.CollisionTargets();
player.Update(gameTime, gamePad, keyboard, collisionTargets);
player.Update(gameTime, input, collisionTargets);
camera.Update(gameTime, player.Position);

Loading…
Cancel
Save