From db6f3e14254f9fd6bd6aa362391b7d529ed315ec Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Thu, 16 Jan 2020 17:02:38 -0500 Subject: [PATCH] Add Input class to group gamepad & keyboard inputs together. For motion directions (up/down & left/right), have them cancel each other out if the player attempts to go in opposite directions at once. Refactor Player & SneakGame to use the new Input class & remove direct access to Keyboard & GamePad. GitOrigin-RevId: 80fbed887408ae2b80c01273a49b150f38285dcb --- Shared/Input.cs | 51 +++++++++++++++++++++++++++++++++++++++++ Shared/Player.cs | 30 ++++++++---------------- Shared/Shared.projitems | 1 + Shared/SneakGame.cs | 15 +++++------- 4 files changed, 67 insertions(+), 30 deletions(-) create mode 100644 Shared/Input.cs diff --git a/Shared/Input.cs b/Shared/Input.cs new file mode 100644 index 0000000..83ecfca --- /dev/null +++ b/Shared/Input.cs @@ -0,0 +1,51 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace SemiColinGames { + struct Input { + public bool Jump; + public bool Attack; + public Vector2 Motion; + + public bool Exit; + public bool FullScreen; + public bool Debug; + + public Input(GamePadState gamePad, KeyboardState keyboard) { + // First we process normal buttons. + Jump = gamePad.IsButtonDown(Buttons.A) || keyboard.IsKeyDown(Keys.J); + Attack = gamePad.IsButtonDown(Buttons.X) || keyboard.IsKeyDown(Keys.K); + + // Then special debugging sorts of buttons. + Exit = gamePad.IsButtonDown(Buttons.Start) || keyboard.IsKeyDown(Keys.Escape); + FullScreen = gamePad.IsButtonDown(Buttons.Back) || keyboard.IsKeyDown(Keys.F12) || + keyboard.IsKeyDown(Keys.OemPlus); + Debug = gamePad.IsButtonDown(Buttons.LeftShoulder) || keyboard.IsKeyDown(Keys.OemMinus); + + // Then potential motion directions. If the player attempts to input opposite directions at + // once (up & down or left & right), those inputs cancel out, resulting in no motion. + Motion = new Vector2(); + Vector2 leftStick = gamePad.ThumbSticks.Left; + bool left = leftStick.X < -0.5 || gamePad.IsButtonDown(Buttons.DPadLeft) || + keyboard.IsKeyDown(Keys.A); + bool right = leftStick.X > 0.5 || gamePad.IsButtonDown(Buttons.DPadRight) || + keyboard.IsKeyDown(Keys.D); + bool up = leftStick.Y > 0.5 || gamePad.IsButtonDown(Buttons.DPadUp) || + keyboard.IsKeyDown(Keys.W); + bool down = leftStick.Y < -0.5 || gamePad.IsButtonDown(Buttons.DPadDown) || + keyboard.IsKeyDown(Keys.S); + if (left && !right) { + Motion.X = -1; + } + if (right && !left) { + Motion.X = 1; + } + if (up && !down) { + Motion.Y = 1; + } + if (down && !up) { + Motion.Y = -1; + } + } + } +} diff --git a/Shared/Player.cs b/Shared/Player.cs index da702c5..6700dce 100644 --- a/Shared/Player.cs +++ b/Shared/Player.cs @@ -35,12 +35,10 @@ namespace SemiColinGames { return new Rectangle(position.X - spriteWidth, position.Y - 7, spriteWidth * 2, 26); } - public void Update( - GameTime time, History gamePad, History keyboard, - List collisionTargets) { + public void Update(GameTime time, History input, List collisionTargets) { Point oldPosition = position; AirState oldAirState = airState; - UpdateFromInput(time, gamePad, keyboard); + HandleInput(time, input); Rectangle oldBbox = Bbox(oldPosition); Rectangle playerBbox = Bbox(position); @@ -97,11 +95,8 @@ namespace SemiColinGames { } } - void UpdateFromInput( - GameTime time, History gamePad, History 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) { + void HandleInput(GameTime time, History input) { + if (input[0].Jump && !input[1].Jump && airState == AirState.Ground) { pose = Pose.Jumping; airState = AirState.Jumping; jumpTime = 0.5; @@ -109,30 +104,23 @@ namespace SemiColinGames { return; } - if ((gamePad[0].IsButtonDown(Buttons.X) && gamePad[1].IsButtonUp(Buttons.X) || - keyboard[0].IsKeyDown(Keys.K) && keyboard[1].IsKeyUp(Keys.K)) - && swordSwingTime <= 0) { + if (input[0].Attack && !input[1].Attack && swordSwingTime <= 0) { pose = Pose.SwordSwing; swordSwingTime = 0.3; return; } - Vector2 leftStick = gamePad[0].ThumbSticks.Left; - if (gamePad[0].IsButtonDown(Buttons.DPadLeft) || leftStick.X < -0.5 || - keyboard[0].IsKeyDown(Keys.A)) { + if (input[0].Motion.X < 0) { facing = Facing.Left; pose = Pose.Walking; position.X -= (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds); - } else if (gamePad[0].IsButtonDown(Buttons.DPadRight) || leftStick.X > 0.5 || - keyboard[0].IsKeyDown(Keys.D)) { + } else if (input[0].Motion.X > 0) { facing = Facing.Right; pose = Pose.Walking; position.X += (int) (moveSpeed * time.ElapsedGameTime.TotalSeconds); - } else if (gamePad[0].IsButtonDown(Buttons.DPadDown) || leftStick.Y < -0.5 || - keyboard[0].IsKeyDown(Keys.S)) { + } else if (input[0].Motion.Y < 0) { pose = Pose.Crouching; - } else if (gamePad[0].IsButtonDown(Buttons.DPadUp) || leftStick.Y > 0.5 || - keyboard[0].IsKeyDown(Keys.W)) { + } else if (input[0].Motion.Y > 0) { pose = Pose.Stretching; } else { pose = Pose.Standing; diff --git a/Shared/Shared.projitems b/Shared/Shared.projitems index 2cece17..12c1794 100644 --- a/Shared/Shared.projitems +++ b/Shared/Shared.projitems @@ -10,6 +10,7 @@ + diff --git a/Shared/SneakGame.cs b/Shared/SneakGame.cs index ac7f2d6..3838132 100644 --- a/Shared/SneakGame.cs +++ b/Shared/SneakGame.cs @@ -17,6 +17,7 @@ namespace SemiColinGames { History keyboard = new History(2); History gamePad = new History(2); + History input = new History(2); FpsCounter fpsCounter = new FpsCounter(); Texture2D grasslandBg1; @@ -66,27 +67,23 @@ namespace SemiColinGames { protected override void Update(GameTime gameTime) { Debug.Clear(); - gamePad.Add(GamePad.GetState(PlayerIndex.One)); - keyboard.Add(Keyboard.GetState()); + input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState())); - if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) { + if (input[0].Exit) { Exit(); } - 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)) { + if (input[0].FullScreen && !input[1].FullScreen) { fullScreen = !fullScreen; display.SetFullScreen(fullScreen); } - if (gamePad[0].IsButtonDown(Buttons.LeftShoulder) && gamePad[1].IsButtonUp(Buttons.LeftShoulder) || - keyboard[0].IsKeyDown(Keys.OemMinus) && keyboard[1].IsKeyUp(Keys.OemMinus)) { + if (input[0].Debug && !input[1].Debug) { Debug.Enabled = !Debug.Enabled; } List collisionTargets = world.CollisionTargets(); - player.Update(gameTime, gamePad, keyboard, collisionTargets); + player.Update(gameTime, input, collisionTargets); camera.Update(gameTime, player.Position);