From 9ccb35f2ad2d792254b527df42be8d49596c5511 Mon Sep 17 00:00:00 2001 From: "J. M. Ellis" Date: Fri, 24 May 2024 20:21:01 -0700 Subject: [PATCH] Brought over our player input code from GML into a PlayerInput.cs class. --- Game1.cs | 9 +++- Player.cs | 22 ++-------- PlayerInput.cs | 115 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 20 deletions(-) create mode 100644 PlayerInput.cs diff --git a/Game1.cs b/Game1.cs index 77a8ec9..c23dbe7 100644 --- a/Game1.cs +++ b/Game1.cs @@ -23,7 +23,8 @@ namespace LunaLightXMG // Test player input and movement with abstracted player class private Player player; - + // Player input + private PlayerInput playerInput; // Native retro scale private int retroWidth = 320; private int retroHeight = 180; @@ -40,6 +41,7 @@ namespace LunaLightXMG // Test player input and movement with abstracted player class player = new Player(); + playerInput = new PlayerInput(); } private void OnClientSizeChanged(object sender, EventArgs eventArgs) @@ -59,6 +61,8 @@ namespace LunaLightXMG graphics.PreferredBackBufferHeight = 1080; graphics.ApplyChanges(); + playerInput.HasControl = true; // Do we need this here? + base.Initialize(); CalculateRenderDestination(); } @@ -96,7 +100,8 @@ namespace LunaLightXMG Exit(); // Test player input and movement with abstracted player class - player.Update(gameTime); + playerInput.Update(); + player.Update(gameTime, playerInput); base.Update(gameTime); } diff --git a/Player.cs b/Player.cs index 4535225..7d18c4b 100644 --- a/Player.cs +++ b/Player.cs @@ -1,7 +1,6 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; namespace LunaLightXMG { @@ -26,10 +25,6 @@ namespace LunaLightXMG // Player position public Vector2 Position; - // Placeholder for key input states - private bool keyLeft; - private bool keyRight; - // Constructor to initialize variables public Player() { @@ -54,24 +49,23 @@ namespace LunaLightXMG } // Update method to handle movement logic - public void Update(GameTime gameTime) + public void Update(GameTime gameTime, PlayerInput input) { - HandleInput(); - Movement(); + Movement(input); // Update player position based on speed Position.X += hsp; Position.Y += vsp; } - private void Movement() + private void Movement(PlayerInput input) { // Horizontal movement with acceleration + wall jump delay wallJumpDelay = Math.Max(wallJumpDelay - 1, 0); // counts down every frame if (wallJumpDelay == 0) // no left-right control until delay is 0, prevents rapid wall climb { - float moveH = (keyRight ? 1 : 0) - (keyLeft ? 1 : 0); + float moveH = input.KeyRight - input.KeyLeft; hsp += moveH * hspAcc; if (moveH == 0) @@ -115,14 +109,6 @@ namespace LunaLightXMG return start; } - // Placeholder for key input handling (to be connected to actual input handling logic) - private void HandleInput() - { - KeyboardState keyboardState = Keyboard.GetState(); - keyLeft = keyboardState.IsKeyDown(Keys.A); - keyRight = keyboardState.IsKeyDown(Keys.D); - } - public void Draw(SpriteBatch spriteBatch, Texture2D texture) { spriteBatch.Draw(texture, Position, Color.White); diff --git a/PlayerInput.cs b/PlayerInput.cs new file mode 100644 index 0000000..f853ab9 --- /dev/null +++ b/PlayerInput.cs @@ -0,0 +1,115 @@ +using System; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace LunaLightXMG +{ + public class PlayerInput + { + public bool HasControl { get; set; } + public bool Controller { get; set; } + public bool CanDash { get; set; } + public float KeyUp { get; private set; } + public float KeyDown { get; private set; } + public float KeyRight { get; private set; } + public float KeyLeft { get; private set; } + public bool KeyJump { get; private set; } + public bool KeyDash { get; private set; } + public bool KeyAttack { get; private set; } + public bool KeyWallClimb { get; private set; } + + public void Update() + { + if (HasControl) + { + if (Controller) + { + // If using controller, switch to keyboard by pressing any key + if (Keyboard.GetState().GetPressedKeys().Length > 0) + { + // Insert your logic to create the objMouseJoyStick instance + Controller = false; + } + + // Zero input each step + KeyUp = 0; + KeyDown = 0; + KeyRight = 0; + KeyLeft = 0; + KeyJump = false; + KeyDash = false; + KeyAttack = false; + KeyWallClimb = false; + + // Controller input (assuming Xbox controller) + GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); + if (gamepadState.IsConnected) + { + float axisLH = gamepadState.ThumbSticks.Left.X; + float axisLV = gamepadState.ThumbSticks.Left.Y; + + if (Math.Abs(axisLH) > 0.5f || Math.Abs(axisLV) > 0.5f) + { + KeyDown = Math.Max(axisLV, 0); + KeyUp = Math.Abs(Math.Min(axisLV, 0)); + KeyLeft = Math.Abs(Math.Min(axisLH, 0)) > 0.5f ? Math.Abs(Math.Min(axisLH, 0)) : 0; + KeyRight = Math.Max(axisLH, 0) > 0.5f ? Math.Max(axisLH, 0) : 0; + } + + KeyJump = gamepadState.Buttons.A == ButtonState.Pressed; + KeyWallClimb = gamepadState.Buttons.LeftShoulder == ButtonState.Pressed; + KeyDash = gamepadState.Buttons.RightShoulder == ButtonState.Pressed; + } + } + else + { + // If using keyboard, switch to controller by pressing the start button on the gamepad + GamePadState gamepadState = GamePad.GetState(PlayerIndex.One); + if (gamepadState.IsConnected && gamepadState.Buttons.Start == ButtonState.Pressed) + { + // Insert your logic to destroy the objMouseJoyStick instance + Controller = true; + } + + // Keyboard input + KeyboardState keyboardState = Keyboard.GetState(); + + KeyLeft = keyboardState.IsKeyDown(Keys.A) ? 1 : 0; + KeyRight = keyboardState.IsKeyDown(Keys.D) ? 1 : 0; + KeyUp = keyboardState.IsKeyDown(Keys.W) ? 1 : 0; + KeyDown = keyboardState.IsKeyDown(Keys.S) ? 1 : 0; + + // Prevent superposition + if (KeyLeft != 0 && KeyRight != 0) + { + KeyLeft = 0; + KeyRight = 0; + } + if (KeyUp != 0 && KeyDown != 0) + { + KeyUp = 0; + KeyDown = 0; + } + + KeyJump = keyboardState.IsKeyDown(Keys.Space); + KeyDash = Mouse.GetState().LeftButton == ButtonState.Pressed; + //KeyAttack = Mouse.GetState().RightButton == ButtonState.Pressed; + KeyWallClimb = keyboardState.IsKeyDown(Keys.LeftShift); + } + } + else + { + // Zero input if player has no control + KeyUp = 0; + KeyDown = 0; + KeyRight = 0; + KeyLeft = 0; + KeyJump = false; + KeyDash = false; + KeyAttack = false; + KeyWallClimb = false; + } + } + } + +}