165 lines
5.5 KiB
C#
165 lines
5.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using Unheimlich.Gameplay.Player;
|
|
|
|
namespace Unheimlich.Managers
|
|
{
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
[RequireComponent(typeof(PlayerInput))]
|
|
public class PlayerInputManager : MonoBehaviour
|
|
{
|
|
/// <summary>
|
|
/// Defines different input contexts that control which player actions are enabled.
|
|
/// </summary>
|
|
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>();
|
|
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<PlayerMovement>();
|
|
_playerLook = GetComponent<PlayerLook>();
|
|
_headBob = GetComponentInChildren<CameraHeadBob>();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the input context, controlling which inputs and player components are active.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Enables or disables player movement components.
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static void ToggleInput(bool enable)
|
|
{
|
|
SetContext(enable ? InputContext.Normal : InputContext.Dialogue);
|
|
}
|
|
|
|
public void OnMove(InputAction.CallbackContext context)
|
|
{
|
|
MoveInput = context.ReadValue<Vector2>();
|
|
}
|
|
|
|
public void Look(InputAction.CallbackContext context)
|
|
{
|
|
LookInput = context.ReadValue<Vector2>();
|
|
}
|
|
|
|
public void OnJump(InputAction.CallbackContext context)
|
|
{
|
|
}
|
|
|
|
public void OnInteract(InputAction.CallbackContext context)
|
|
{
|
|
}
|
|
|
|
public void OnDiapason(InputAction.CallbackContext context)
|
|
{
|
|
}
|
|
}
|
|
}
|