Fix: cleanup and minor perf improvements

This commit is contained in:
Ferlat Thibaud
2026-03-25 01:28:18 +01:00
parent 9fd251f6e9
commit e69bdb93f8
21 changed files with 130 additions and 136 deletions
@@ -14,10 +14,11 @@ namespace Unheimlich.Core.Input
{
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)
Normal, // All inputs enabled
Dialogue, // Only look and interact
Listening, // Only look and diapason
Inspection, // Only look and interact (for item inspection)
DetectiveBoard // Mouse cursor free, movement disabled
}
[Header("Current State")]
@@ -30,6 +31,14 @@ namespace Unheimlich.Core.Input
public static Vector2 MoveInput { get; private set; }
public static Vector2 LookInput { get; private set; }
// Cached InputAction references — populated once during initialization
private static UnityEngine.InputSystem.InputAction _cachedMoveAction;
private static UnityEngine.InputSystem.InputAction _cachedLookAction;
private static UnityEngine.InputSystem.InputAction _cachedInteractAction;
private static UnityEngine.InputSystem.InputAction _cachedDiapasonAction;
private static UnityEngine.InputSystem.InputAction _cachedFireAction;
private static UnityEngine.InputSystem.InputAction _cachedExitAction;
// Legacy compatibility
public static bool IsInputEnabled => CurrentContext == InputContext.Normal;
public static bool IsLookingEnabled => CurrentContext != InputContext.Dialogue;
@@ -38,6 +47,7 @@ namespace Unheimlich.Core.Input
{
// Try to subscribe, will retry in Start if needed
SubscribeAllInputs();
CacheAllActions();
SetContext(InputContext.Normal);
}
@@ -56,8 +66,9 @@ namespace Unheimlich.Core.Input
}
// Ensure we're subscribed
// Ensure we're subscribed and actions are cached
SubscribeAllInputs();
CacheAllActions();
}
private void OnDestroy()
@@ -79,6 +90,38 @@ namespace Unheimlich.Core.Input
OctoCore.OctoCore.Instance.Inputs.SubscribeAction(OctoActions.Player_Exit, OnExit);
}
/// <summary>
/// Caches all InputAction references from OctoCore once at startup to avoid
/// per-frame iteration over the Actions list.
/// </summary>
private void CacheAllActions()
{
var asset = OctoCore.OctoCore.Instance?.Inputs?.Actions;
if (asset == null) return;
_cachedMoveAction = null;
_cachedLookAction = null;
_cachedInteractAction = null;
_cachedDiapasonAction = null;
_cachedFireAction = null;
_cachedExitAction = null;
foreach (var actionInfo in asset)
{
if (actionInfo.Reference == null) continue;
switch (actionInfo.Name)
{
case "Move": _cachedMoveAction = actionInfo.Reference.action; break;
case "Look": _cachedLookAction = actionInfo.Reference.action; break;
case "Interact": _cachedInteractAction = actionInfo.Reference.action; break;
case "Diapason": _cachedDiapasonAction = actionInfo.Reference.action; break;
case "Fire": _cachedFireAction = actionInfo.Reference.action; break;
case "Exit": _cachedExitAction = actionInfo.Reference.action; break;
}
}
}
private void UnsubscribeAllInputs()
{
if (OctoCore.OctoCore.Instance == null) return;
@@ -126,6 +169,12 @@ namespace Unheimlich.Core.Input
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
break;
case InputContext.DetectiveBoard:
// Mouse cursor free for board interaction, movement disabled
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
break;
}
}
@@ -180,83 +229,12 @@ namespace Unheimlich.Core.Input
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;
}
// Action accessors — return pre-cached references, no per-frame allocation
public static UnityEngine.InputSystem.InputAction GetMoveAction() => _cachedMoveAction;
public static UnityEngine.InputSystem.InputAction GetLookAction() => _cachedLookAction;
public static UnityEngine.InputSystem.InputAction GetInteractAction() => _cachedInteractAction;
public static UnityEngine.InputSystem.InputAction GetDiapasonAction() => _cachedDiapasonAction;
public static UnityEngine.InputSystem.InputAction GetFireAction() => _cachedFireAction;
public static UnityEngine.InputSystem.InputAction GetExitAction() => _cachedExitAction;
}
}