From 307efa5f5b94e4b13ea298875e660954bbf6ca39 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 10 Dec 2019 10:56:51 -0500 Subject: [PATCH] use History for handling gamepad & keyboard state GitOrigin-RevId: 87aa91b212eedb5e3f64cc50de70098ce5f81596 --- Jumpy.Shared/JumpyGame.cs | 26 +++++++++++++++----------- Jumpy.Shared/Player.cs | 3 ++- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Jumpy.Shared/JumpyGame.cs b/Jumpy.Shared/JumpyGame.cs index 8fb0958..333121d 100644 --- a/Jumpy.Shared/JumpyGame.cs +++ b/Jumpy.Shared/JumpyGame.cs @@ -2,22 +2,27 @@ using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; +using System; using System.Collections.Generic; namespace Jumpy { public class JumpyGame : Game { GraphicsDeviceManager graphics; + // TODO: use a History but implement functions that let us re-use the + // RenderTargets instead of re-creating them every frame? const int numRenderTargets = 1; RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets]; int renderTargetIdx = 0; SpriteBatch spriteBatch; SpriteFont font; - KeyboardInput keyboardInput = new KeyboardInput(); bool fullScreen = false; IDisplay display; + History keyboardHistory = new History(2); + History gamePadHistory = new History(2); + FpsCounter fpsCounter = new FpsCounter(); Player player; @@ -56,21 +61,20 @@ namespace Jumpy { // Updates the game world. protected override void Update(GameTime gameTime) { - keyboardInput.Update(); - GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); - List keysDown = keyboardInput.NewKeysDown(); + gamePadHistory.Add(GamePad.GetState(PlayerIndex.One)); + keyboardHistory.Add(Keyboard.GetState()); - if (keysDown.Contains(Keys.F12)) { - fullScreen = !fullScreen; - display.SetFullScreen(fullScreen); + if (gamePadHistory[0].Buttons.Start == ButtonState.Pressed || + keyboardHistory[0].IsKeyDown(Keys.Escape)) { + Exit(); } - if (gamePadState.Buttons.Start == ButtonState.Pressed || - keysDown.Contains(Keys.Escape)) { - Exit(); + if (keyboardHistory[0].IsKeyDown(Keys.F12) && keyboardHistory[1].IsKeyUp(Keys.F12)) { + fullScreen = !fullScreen; + display.SetFullScreen(fullScreen); } - player.Update(gameTime, gamePadState); + player.Update(gameTime, gamePadHistory); base.Update(gameTime); } diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index dd653ef..55e7a4b 100644 --- a/Jumpy.Shared/Player.cs +++ b/Jumpy.Shared/Player.cs @@ -29,7 +29,8 @@ namespace Jumpy { this.texture = texture; } - public void Update(GameTime time, GamePadState gamePad) { + public void Update(GameTime time, History gamePadHistory) { + GamePadState gamePad = gamePadHistory[0]; if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) { pose = Pose.Jumping; airState = AirState.Jumping;