Brought over our player input code from GML into a PlayerInput.cs class.

This commit is contained in:
J. M. Ellis
2024-05-24 20:21:01 -07:00
parent b92de57435
commit 9ccb35f2ad
3 changed files with 126 additions and 20 deletions
+115
View File
@@ -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;
}
}
}
}