From 7dc1f166abc8f7f622267baf15b2e00d31b9c45d Mon Sep 17 00:00:00 2001 From: "J. M. Ellis" Date: Fri, 24 May 2024 16:44:27 -0700 Subject: [PATCH] Adding player input class. --- PlayerInput.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 PlayerInput.cs diff --git a/PlayerInput.cs b/PlayerInput.cs new file mode 100644 index 0000000..a8dbe5f --- /dev/null +++ b/PlayerInput.cs @@ -0,0 +1,41 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace LunaLightXMG +{ + internal class PlayerInput + { + // Field to store the current keyboard state + private KeyboardState currentKeyboardState; + + // Method to update the input state + public void Update() + { + currentKeyboardState = Keyboard.GetState(); + } + + // Method to check if a key is currently held down + public bool IsKeyDown(Keys key) + { + return currentKeyboardState.IsKeyDown(key); + } + + // Method to check WASD movement keys + public Vector2 MoveWithWASD(Vector2 currentPosition, float speed) + { + Vector2 movement = Vector2.Zero; + + if (IsKeyDown(Keys.W)) + movement.Y -= speed; + if (IsKeyDown(Keys.A)) + movement.X -= speed; + if (IsKeyDown(Keys.S)) + movement.Y += speed; + if (IsKeyDown(Keys.D)) + movement.X += speed; + + return currentPosition + movement; + } + } + +}