using UnityEngine;
using UnityEngine.InputSystem;
using Unheimlich.Gameplay.Player;
namespace Unheimlich.Managers
{
///
/// Centralizes Input System handling for the player.
/// Manages input state, action references, and global input enable/disable functionality.
/// Supports different input contexts (Normal, Dialogue, Listening) to control which inputs are active.
///
[RequireComponent(typeof(PlayerInput))]
public class PlayerInputManager : MonoBehaviour
{
///
/// Defines different input contexts that control which player actions are enabled.
///
public enum InputContext
{
Normal, // All inputs enabled (movement, look, jump, interact, diapason)
Dialogue, // Only look and interact enabled (for dialogues/UI)
Listening // Only look enabled, movement disabled (for diapason camera pan)
}
public static PlayerInputManager Instance { get; private set; }
[Header("Input Values (Read only)")]
public Vector2 MoveInput;
public Vector2 LookInput;
public InputActionReference JumpAction;
public InputActionReference InteractAction;
public InputActionReference DiapasonAction;
public static bool IsInputEnabled { get; private set; } = true;
public static bool IsLookingEnabled { get; private set; } = true;
public static InputContext CurrentContext { get; private set; } = InputContext.Normal;
private InputActionMap _playerActionMap;
private InputAction _lookAction;
private InputAction _interactAction;
private InputAction _diapasonAction;
private PlayerMovement _playerMovement;
private PlayerLook _playerLook;
private CameraHeadBob _headBob;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(this);
return;
}
Instance = this;
IsInputEnabled = true;
PlayerInput playerInput = GetComponent();
playerInput.actions.Enable();
_playerActionMap = playerInput.actions.FindActionMap("Player");
_lookAction = _playerActionMap.FindAction("Look");
_interactAction = _playerActionMap.FindAction("Interact");
_diapasonAction = _playerActionMap.FindAction("Diapason");
JumpAction.action.Enable();
InteractAction.action.Enable();
if (DiapasonAction != null)
DiapasonAction.action.Enable();
_playerMovement = GetComponent();
_playerLook = GetComponent();
_headBob = GetComponentInChildren();
}
///
/// Sets the input context, controlling which inputs and player components are active.
///
public static void SetContext(InputContext context)
{
if (Instance == null) return;
CurrentContext = context;
switch (context)
{
case InputContext.Normal:
Instance._playerActionMap.Enable();
Instance.SetComponentsEnabled(true, true, true);
IsInputEnabled = true;
IsLookingEnabled = true;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
break;
case InputContext.Dialogue:
Instance._playerActionMap.Disable();
Instance._lookAction.Enable();
Instance._interactAction.Enable();
if (Instance._diapasonAction != null)
Instance._diapasonAction.Enable();
Instance.SetComponentsEnabled(false, false, false);
IsInputEnabled = false;
IsLookingEnabled = false;
break;
case InputContext.Listening:
Instance._playerActionMap.Disable();
Instance._lookAction.Enable();
if (Instance._diapasonAction != null)
Instance._diapasonAction.Enable();
Instance.SetComponentsEnabled(false, false, false);
IsInputEnabled = false;
IsLookingEnabled = false;
break;
}
}
///
/// Enables or disables player movement components.
///
private void SetComponentsEnabled(bool movement, bool look, bool headBob)
{
if (_playerMovement != null)
_playerMovement.enabled = movement;
if (_playerLook != null)
_playerLook.enabled = look;
if (_headBob != null)
_headBob.enabled = headBob;
}
///
/// Globally toggles player input on/off. When disabled, only look and interact actions remain active.
/// Used during dialogues, inspections, or UI interactions.
/// LEGACY: Use SetContext(InputContext.Dialogue) instead.
///
public static void ToggleInput(bool enable)
{
SetContext(enable ? InputContext.Normal : InputContext.Dialogue);
}
public void OnMove(InputAction.CallbackContext context)
{
MoveInput = context.ReadValue();
}
public void Look(InputAction.CallbackContext context)
{
LookInput = context.ReadValue();
}
public void OnJump(InputAction.CallbackContext context)
{
}
public void OnInteract(InputAction.CallbackContext context)
{
}
public void OnDiapason(InputAction.CallbackContext context)
{
}
}
}