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.Input;
using System;
using System.Collections.Generic;
namespace Jumpy {
public class JumpyGame : Game {
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;
RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
int renderTargetIdx = 0;
SpriteBatch spriteBatch;
SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput();
bool fullScreen = false;
IDisplay display;
History<KeyboardState> keyboardHistory = new History<KeyboardState>(2);
History<GamePadState> gamePadHistory = new History<GamePadState>(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<Keys> keysDown = keyboardInput.NewKeysDown();
gamePadHistory.Add(GamePad.GetState(PlayerIndex.One));
keyboardHistory.Add(Keyboard.GetState());
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;
display.SetFullScreen(fullScreen);
}
if (gamePadState.Buttons.Start == ButtonState.Pressed ||
keysDown.Contains(Keys.Escape)) {
Exit();
}
player.Update(gameTime, gamePadState);
player.Update(gameTime, gamePadHistory);
base.Update(gameTime);
}

View File

@ -29,7 +29,8 @@ namespace Jumpy {
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) {
pose = Pose.Jumping;
airState = AirState.Jumping;