use History for handling gamepad & keyboard state

GitOrigin-RevId: 87aa91b212eedb5e3f64cc50de70098ce5f81596
This commit is contained in:
Colin McMillen 2019-12-10 10:56:51 -05:00
parent 826e308cfe
commit 307efa5f5b
2 changed files with 17 additions and 12 deletions

View File

@ -2,22 +2,27 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace Jumpy { namespace Jumpy {
public class JumpyGame : Game { public class JumpyGame : Game {
GraphicsDeviceManager graphics; GraphicsDeviceManager graphics;
// TODO: use a History<RenderTarget2D> but implement functions that let us re-use the
// RenderTargets instead of re-creating them every frame?
const int numRenderTargets = 1; const int numRenderTargets = 1;
RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets]; RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
int renderTargetIdx = 0; int renderTargetIdx = 0;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
SpriteFont font; SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput();
bool fullScreen = false; bool fullScreen = false;
IDisplay display; IDisplay display;
History<KeyboardState> keyboardHistory = new History<KeyboardState>(2);
History<GamePadState> gamePadHistory = new History<GamePadState>(2);
FpsCounter fpsCounter = new FpsCounter(); FpsCounter fpsCounter = new FpsCounter();
Player player; Player player;
@ -56,21 +61,20 @@ namespace Jumpy {
// Updates the game world. // Updates the game world.
protected override void Update(GameTime gameTime) { protected override void Update(GameTime gameTime) {
keyboardInput.Update(); gamePadHistory.Add(GamePad.GetState(PlayerIndex.One));
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One); keyboardHistory.Add(Keyboard.GetState());
List<Keys> keysDown = keyboardInput.NewKeysDown();
if (keysDown.Contains(Keys.F12)) { if (gamePadHistory[0].Buttons.Start == ButtonState.Pressed ||
keyboardHistory[0].IsKeyDown(Keys.Escape)) {
Exit();
}
if (keyboardHistory[0].IsKeyDown(Keys.F12) && keyboardHistory[1].IsKeyUp(Keys.F12)) {
fullScreen = !fullScreen; fullScreen = !fullScreen;
display.SetFullScreen(fullScreen); display.SetFullScreen(fullScreen);
} }
if (gamePadState.Buttons.Start == ButtonState.Pressed || player.Update(gameTime, gamePadHistory);
keysDown.Contains(Keys.Escape)) {
Exit();
}
player.Update(gameTime, gamePadState);
base.Update(gameTime); base.Update(gameTime);
} }

View File

@ -29,7 +29,8 @@ namespace Jumpy {
this.texture = texture; this.texture = texture;
} }
public void Update(GameTime time, GamePadState gamePad) { public void Update(GameTime time, History<GamePadState> gamePadHistory) {
GamePadState gamePad = gamePadHistory[0];
if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) { if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) {
pose = Pose.Jumping; pose = Pose.Jumping;
airState = AirState.Jumping; airState = AirState.Jumping;