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

View File

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