Files
EE2Clone/Assets/Scripts/Hybrid/RTSInputActions.cs
ldbetteridge 58da5d1d71 Initial import
2026-03-31 15:59:23 +01:00

141 lines
5.1 KiB
C#

using UnityEngine;
using UnityEngine.InputSystem;
namespace EE2Clone.Hybrid
{
/// <summary>
/// MonoBehaviour that reads from the Input System and exposes RTS-specific input state
/// for the hybrid camera, selection, and UI systems. This is the bridge between
/// Unity's Input System and our ECS command pipeline.
/// </summary>
public class RTSInputActions : MonoBehaviour
{
public static RTSInputActions Instance { get; private set; }
[Header("Input Action References")]
[SerializeField] private InputActionAsset inputActions;
// Camera
public Vector2 CameraPan { get; private set; }
public float CameraZoom { get; private set; }
public float CameraRotate { get; private set; }
// Selection
public bool LeftClickPressed { get; private set; }
public bool LeftClickReleased { get; private set; }
public bool LeftClickHeld { get; private set; }
public bool RightClickPressed { get; private set; }
public Vector2 MousePosition { get; private set; }
// Modifiers
public bool ShiftHeld { get; private set; }
public bool CtrlHeld { get; private set; }
// Actions
public bool AttackMovePressed { get; private set; }
public bool PatrolPressed { get; private set; }
public bool StopPressed { get; private set; }
public bool DeletePressed { get; private set; }
// Control groups (0-9)
public int ControlGroupPressed { get; private set; } = -1;
private InputAction _cameraPanAction;
private InputAction _cameraZoomAction;
private InputAction _cameraRotateAction;
private InputAction _leftClickAction;
private InputAction _rightClickAction;
private InputAction _mousePositionAction;
private InputAction _shiftAction;
private InputAction _ctrlAction;
private InputAction _attackMoveAction;
private InputAction _patrolAction;
private InputAction _stopAction;
private InputAction _deleteAction;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void OnEnable()
{
if (inputActions == null) return;
var rtsMap = inputActions.FindActionMap("RTS");
if (rtsMap == null)
{
Debug.LogError("RTS action map not found in Input Actions asset!");
return;
}
_cameraPanAction = rtsMap.FindAction("CameraPan");
_cameraZoomAction = rtsMap.FindAction("CameraZoom");
_cameraRotateAction = rtsMap.FindAction("CameraRotate");
_leftClickAction = rtsMap.FindAction("LeftClick");
_rightClickAction = rtsMap.FindAction("RightClick");
_mousePositionAction = rtsMap.FindAction("MousePosition");
_shiftAction = rtsMap.FindAction("Shift");
_ctrlAction = rtsMap.FindAction("Ctrl");
_attackMoveAction = rtsMap.FindAction("AttackMove");
_patrolAction = rtsMap.FindAction("Patrol");
_stopAction = rtsMap.FindAction("Stop");
_deleteAction = rtsMap.FindAction("Delete");
rtsMap.Enable();
}
private void OnDisable()
{
if (inputActions == null) return;
var rtsMap = inputActions.FindActionMap("RTS");
rtsMap?.Disable();
}
private void Update()
{
CameraPan = _cameraPanAction?.ReadValue<Vector2>() ?? Vector2.zero;
CameraZoom = _cameraZoomAction?.ReadValue<float>() ?? 0f;
CameraRotate = _cameraRotateAction?.ReadValue<float>() ?? 0f;
LeftClickPressed = _leftClickAction?.WasPressedThisFrame() ?? false;
LeftClickReleased = _leftClickAction?.WasReleasedThisFrame() ?? false;
LeftClickHeld = _leftClickAction?.IsPressed() ?? false;
RightClickPressed = _rightClickAction?.WasPressedThisFrame() ?? false;
MousePosition = _mousePositionAction?.ReadValue<Vector2>() ?? Vector2.zero;
ShiftHeld = _shiftAction?.IsPressed() ?? false;
CtrlHeld = _ctrlAction?.IsPressed() ?? false;
AttackMovePressed = _attackMoveAction?.WasPressedThisFrame() ?? false;
PatrolPressed = _patrolAction?.WasPressedThisFrame() ?? false;
StopPressed = _stopAction?.WasPressedThisFrame() ?? false;
DeletePressed = _deleteAction?.WasPressedThisFrame() ?? false;
// Control groups (keyboard 0-9)
ControlGroupPressed = -1;
for (int i = 0; i <= 9; i++)
{
if (Keyboard.current != null && Keyboard.current[Key.Digit0 + i].wasPressedThisFrame)
{
ControlGroupPressed = i;
break;
}
}
}
private void OnDestroy()
{
if (Instance == this)
Instance = null;
}
}
}