Browse Source

use History for handling gamepad & keyboard state

GitOrigin-RevId: 87aa91b212
master
Colin McMillen 4 years ago
parent
commit
307efa5f5b
  1. 26
      Jumpy.Shared/JumpyGame.cs
  2. 3
      Jumpy.Shared/Player.cs

26
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<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)) {
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);
}

3
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<GamePadState> gamePadHistory) {
GamePadState gamePad = gamePadHistory[0];
if (gamePad.Buttons.A == ButtonState.Pressed && airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;

Loading…
Cancel
Save