268 lines
9.6 KiB
C#
268 lines
9.6 KiB
C#
using UnityEngine;
|
|
using OctoCore;
|
|
using OctoCore.Inputs;
|
|
using Unheimlich.Core;
|
|
|
|
namespace Unheimlich.Managers
|
|
{
|
|
/// <summary>
|
|
/// Unified input manager wrapping OctoCore inputs with Unheimlich-specific contexts.
|
|
/// Replaces the old PlayerInputManager while keeping context system (Normal, Dialogue, Listening).
|
|
/// </summary>
|
|
public class InputManager : Singleton<InputManager>
|
|
{
|
|
public enum InputContext
|
|
{
|
|
Normal, // All inputs enabled
|
|
Dialogue, // Only look and interact
|
|
Listening, // Only look and diapason
|
|
Inspection // Only look and interact (for item inspection)
|
|
}
|
|
|
|
[Header("Current State")]
|
|
[SerializeField] private InputContext _currentContext = InputContext.Normal;
|
|
|
|
// Public access to current context
|
|
public static InputContext CurrentContext => Instance ? Instance._currentContext : InputContext.Normal;
|
|
|
|
// Input values (cached for compatibility)
|
|
public static Vector2 MoveInput { get; private set; }
|
|
public static Vector2 LookInput { get; private set; }
|
|
|
|
// Legacy compatibility
|
|
public static bool IsInputEnabled => CurrentContext == InputContext.Normal;
|
|
public static bool IsLookingEnabled => CurrentContext != InputContext.Dialogue;
|
|
|
|
protected override void OnAwake()
|
|
{
|
|
// Try to subscribe, will retry in Start if needed
|
|
SubscribeAllInputs();
|
|
SetContext(InputContext.Normal);
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
Debug.Log("[InputManager] Start() called");
|
|
|
|
if (OctoCore.OctoCore.Instance == null)
|
|
{
|
|
Debug.LogError("[InputManager] OctoCore.Instance is NULL!");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("[InputManager] OctoCore.Instance exists");
|
|
|
|
if (OctoCore.OctoCore.Instance.Inputs == null)
|
|
{
|
|
Debug.LogError("[InputManager] OctoCore.Instance.Inputs is NULL!");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("[InputManager] OctoCore.Instance.Inputs exists");
|
|
|
|
// Ensure we're subscribed
|
|
SubscribeAllInputs();
|
|
Debug.Log("[InputManager] Initialized and subscribed to OctoCore inputs.");
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
UnsubscribeAllInputs();
|
|
}
|
|
|
|
// Subscribe to OctoCore inputs
|
|
private void SubscribeAllInputs()
|
|
{
|
|
if (OctoCore.OctoCore.Instance == null) return;
|
|
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Move, OnMove);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Look, OnLook);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Jump, OnJump);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Interact, OnInteract);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Diapason, OnDiapason);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Fire, OnFire);
|
|
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Exit, OnExit);
|
|
}
|
|
|
|
private void UnsubscribeAllInputs()
|
|
{
|
|
if (OctoCore.OctoCore.Instance == null) return;
|
|
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Move, OnMove);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Look, OnLook);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Jump, OnJump);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Interact, OnInteract);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Diapason, OnDiapason);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Fire, OnFire);
|
|
OctoCore.OctoCore.Instance.Inputs.UnsubscribeAction(OctoActions.Player_Exit, OnExit);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets input context and handles cursor/component state
|
|
/// </summary>
|
|
public static void SetContext(InputContext context)
|
|
{
|
|
if (Instance == null) return;
|
|
|
|
Instance._currentContext = context;
|
|
|
|
switch (context)
|
|
{
|
|
case InputContext.Normal:
|
|
// All inputs active
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
break;
|
|
|
|
case InputContext.Dialogue:
|
|
// Only look and interact (movement disabled)
|
|
Cursor.lockState = CursorLockMode.None;
|
|
Cursor.visible = true;
|
|
break;
|
|
|
|
case InputContext.Listening:
|
|
// Only look, diapason, fire, exit (movement disabled)
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
break;
|
|
|
|
case InputContext.Inspection:
|
|
// Only look and interact (for item inspection, movement disabled)
|
|
Cursor.lockState = CursorLockMode.Locked;
|
|
Cursor.visible = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Legacy compatibility
|
|
public static void ToggleInput(bool enable)
|
|
{
|
|
SetContext(enable ? InputContext.Normal : InputContext.Dialogue);
|
|
}
|
|
|
|
// Input callbacks - cache values and filter by context
|
|
private void OnMove(Vector2 value)
|
|
{
|
|
MoveInput = _currentContext == InputContext.Normal ? value : Vector2.zero;
|
|
}
|
|
|
|
private void OnLook(Vector2 value)
|
|
{
|
|
LookInput = value; // Look enabled in all contexts
|
|
}
|
|
|
|
private void OnJump()
|
|
{
|
|
if (_currentContext != InputContext.Normal) return;
|
|
// Broadcast jump event if needed
|
|
}
|
|
|
|
private void OnInteract()
|
|
{
|
|
if (_currentContext == InputContext.Listening) return;
|
|
// Broadcast interact event
|
|
}
|
|
|
|
private void OnDiapason()
|
|
{
|
|
if (_currentContext == InputContext.Dialogue) return;
|
|
// Broadcast diapason event
|
|
}
|
|
|
|
private void OnFire()
|
|
{
|
|
if (_currentContext != InputContext.Listening) return;
|
|
// Broadcast fire event
|
|
}
|
|
|
|
private void OnExit()
|
|
{
|
|
if (_currentContext != InputContext.Listening) return;
|
|
// Broadcast exit event
|
|
}
|
|
|
|
// Public accessors for backward compatibility
|
|
public static Vector2 GetMoveInput() => MoveInput;
|
|
public static Vector2 GetLookInput() => LookInput;
|
|
|
|
// Action accessors for direct button checks
|
|
public static UnityEngine.InputSystem.InputAction GetMoveAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Move" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static UnityEngine.InputSystem.InputAction GetDiapasonAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Diapason" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static UnityEngine.InputSystem.InputAction GetInteractAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Interact" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static UnityEngine.InputSystem.InputAction GetFireAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Fire" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static UnityEngine.InputSystem.InputAction GetExitAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Exit" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static UnityEngine.InputSystem.InputAction GetLookAction()
|
|
{
|
|
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
|
|
if (asset == null) return null;
|
|
|
|
foreach (var actionInfo in asset)
|
|
{
|
|
if (actionInfo.Name == "Look" && actionInfo.Reference != null)
|
|
return actionInfo.Reference.action;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|