Browse Source

add keyboard controls for Player & toggling debug info

GitOrigin-RevId: 0ab5e52485
master
Colin McMillen 4 years ago
parent
commit
37e3a8baba
  1. 7
      Jumpy.Shared/JumpyGame.cs
  2. 28
      Jumpy.Shared/Player.cs

7
Jumpy.Shared/JumpyGame.cs

@ -83,18 +83,19 @@ namespace Jumpy {
}
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
keyboard[0].IsKeyDown(Keys.OemPlus) && keyboard[1].IsKeyUp(Keys.OemPlus) ||
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
fullScreen = !fullScreen;
display.SetFullScreen(fullScreen);
}
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) &&
gamePad[1].IsButtonUp(Buttons.LeftShoulder)) {
if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) ||
keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) {
Debug.Enabled = !Debug.Enabled;
}
List<Rectangle> collisionTargets = world.CollisionTargets();
player.Update(gameTime, gamePad, collisionTargets);
player.Update(gameTime, gamePad, keyboard, collisionTargets);
camera.Update(gameTime, player.Position);

28
Jumpy.Shared/Player.cs

@ -36,10 +36,11 @@ namespace Jumpy {
}
public void Update(
GameTime time, History<GamePadState> gamePad, List<Rectangle> collisionTargets) {
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard,
List<Rectangle> collisionTargets) {
Point oldPosition = position;
AirState oldAirState = airState;
UpdateFromGamePad(time, gamePad);
UpdateFromInput(time, gamePad, keyboard);
Rectangle oldBbox = Bbox(oldPosition);
Rectangle playerBbox = Bbox(position);
@ -102,8 +103,10 @@ namespace Jumpy {
// TODO: refactor input to have a virtual "which directions & buttons were being pressed"
// instead of complicated if-statements in this function.
// TODO: refactor to use a state-machine.
void UpdateFromGamePad(GameTime time, History<GamePadState> gamePad) {
if (gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) &&
void UpdateFromInput(
GameTime time, History<GamePadState> gamePad, History<KeyboardState> keyboard) {
if ((gamePad[0].IsButtonDown(Buttons.A) && gamePad[1].IsButtonUp(Buttons.A) ||
keyboard[0].IsKeyDown(Keys.J) && keyboard[1].IsKeyUp(Keys.J)) &&
airState == AirState.Ground) {
pose = Pose.Jumping;
airState = AirState.Jumping;
@ -112,7 +115,8 @@ namespace Jumpy {
return;
}
if (gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X)
if ((gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X) ||
keyboard[0].IsKeyDown(Keys.K) && keyboard[1].IsKeyUp(Keys.K))
&& swordSwingTime <= 0) {
pose = Pose.SwordSwing;
swordSwingTime = 0.3;
@ -120,17 +124,23 @@ namespace Jumpy {
}
Vector2 leftStick = gamePad[0].ThumbSticks.Left;
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5) {
// TODO: have keyboard directions cancel each other out if mutually-incompatible keys are
// held down?
if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5 ||
keyboard[0].IsKeyDown(Keys.A)) {
facing = Facing.Left;
pose = Pose.Walking;
position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5 ||
keyboard[0].IsKeyDown(Keys.D)) {
facing = Facing.Right;
pose = Pose.Walking;
position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds);
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5 ||
keyboard[0].IsKeyDown(Keys.S)) {
pose = Pose.Crouching;
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5) {
} else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5 ||
keyboard[0].IsKeyDown(Keys.W)) {
pose = Pose.Stretching;
} else {
pose = Pose.Standing;

Loading…
Cancel
Save