refactor uses of gamepad / keyboard history

GitOrigin-RevId: ee4d881cb0aad41a0cdf8ac10aa9489b652384f4
This commit is contained in:
Colin McMillen 2019-12-10 11:12:06 -05:00
parent 307efa5f5b
commit 7fe248aecc
2 changed files with 16 additions and 17 deletions

View File

@ -20,8 +20,8 @@ namespace Jumpy {
bool fullScreen = false;
IDisplay display;
History<KeyboardState> keyboardHistory = new History<KeyboardState>(2);
History<GamePadState> gamePadHistory = new History<GamePadState>(2);
History<KeyboardState> keyboard = new History<KeyboardState>(2);
History<GamePadState> gamePad = new History<GamePadState>(2);
FpsCounter fpsCounter = new FpsCounter();
Player player;
@ -61,20 +61,20 @@ namespace Jumpy {
// Updates the game world.
protected override void Update(GameTime gameTime) {
gamePadHistory.Add(GamePad.GetState(PlayerIndex.One));
keyboardHistory.Add(Keyboard.GetState());
gamePad.Add(GamePad.GetState(PlayerIndex.One));
keyboard.Add(Keyboard.GetState());
if (gamePadHistory[0].Buttons.Start == ButtonState.Pressed ||
keyboardHistory[0].IsKeyDown(Keys.Escape)) {
if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
Exit();
}
if (keyboardHistory[0].IsKeyDown(Keys.F12) && keyboardHistory[1].IsKeyUp(Keys.F12)) {
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
fullScreen = !fullScreen;
display.SetFullScreen(fullScreen);
}
player.Update(gameTime, gamePadHistory);
player.Update(gameTime, gamePad);
base.Update(gameTime);
}

View File

@ -29,9 +29,8 @@ namespace Jumpy {
this.texture = texture;
}
public void Update(GameTime time, History<GamePadState> gamePadHistory) {
GamePadState gamePad = gamePadHistory[0];
if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) {
public void Update(GameTime time, History<GamePadState> gamePad) {
if (gamePad[0].IsButtonDown(Buttons.A) && airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;
jumpTime = 0.5;
@ -39,23 +38,23 @@ namespace Jumpy {
return;
}
// TODO: only swing the sword if button newly pressed (no turbosword)
if (gamePad.Buttons.X == ButtonState.Pressed && swordSwingTime <= 0) {
if (gamePad[0].IsButtonDown(Buttons.X) && swordSwingTime <= 0) {
pose = Pose.SwordSwing;
swordSwingTime = 0.3;
return;
}
Vector2 leftStick = gamePad.ThumbSticks.Left;
if (gamePad.DPad.Left == ButtonState.Pressed || leftStick.X < -0.5) {
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= (int) (moveSpeed * (float) time.ElapsedGameTime.TotalSeconds);
} else if (gamePad.DPad.Right == ButtonState.Pressed || leftStick.X > 0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += (int) (moveSpeed * (float) time.ElapsedGameTime.TotalSeconds);
} else if (gamePad.DPad.Down == ButtonState.Pressed || leftStick.Y < -0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
pose = Pose.Crouching;
} else if (gamePad.DPad.Up == ButtonState.Pressed || leftStick.Y > 0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;