Files
Ipseity-Demo/Assets/_Content/Scripts/Features/DetectiveBoard/Runtime/ClueUnlockTrigger.cs
T

99 lines
3.1 KiB
C#

using UnityEngine;
using Unheimlich.Core.Events;
using Unheimlich.Features.DetectiveBoard.Data;
namespace Unheimlich.Features.DetectiveBoard
{
/// <summary>
/// Unlocks a clue on the detective board.
/// Can be driven by an optional GameEventTrigger on the same GameObject,
/// or called directly via UnlockClue() from any external script or UnityEvent.
/// </summary>
public class ClueUnlockTrigger : MonoBehaviour
{
[Header("Clue Configuration")]
[Tooltip("The clue data to unlock when triggered")]
[SerializeField] private ClueData _clueToUnlock;
[Header("Auto Trigger")]
[Tooltip("Unlock automatically on Start()")]
[SerializeField] private bool _unlockOnStart;
[Tooltip("Delay in seconds before unlocking (only with Unlock On Start)")]
[SerializeField] private float _unlockDelay;
[Tooltip("Prevent unlocking more than once")]
[SerializeField] private bool _unlockOnlyOnce = true;
[Header("Debug")]
[SerializeField] private bool _debugMode;
private GameEventTrigger _eventTrigger;
private bool _hasUnlocked;
/// <summary>Raised after the clue has been successfully unlocked.</summary>
public event System.Action OnClueUnlocked;
private void Awake()
{
// Optional composition with GameEventTrigger
_eventTrigger = GetComponent<GameEventTrigger>();
if (_eventTrigger != null)
_eventTrigger.OnTriggered += UnlockClue;
}
private void Start()
{
if (_unlockOnStart)
{
if (_unlockDelay > 0f)
Invoke(nameof(UnlockClue), _unlockDelay);
else
UnlockClue();
}
}
private void OnDestroy()
{
if (_eventTrigger != null)
_eventTrigger.OnTriggered -= UnlockClue;
}
/// <summary>
/// Unlocks the associated clue. Can be called directly from external scripts or UnityEvents.
/// </summary>
public void UnlockClue()
{
if (_unlockOnlyOnce && _hasUnlocked)
return;
if (_clueToUnlock == null)
{
Debug.LogError("[ClueUnlockTrigger] No clue data assigned!", this);
return;
}
if (DetectiveBoardManager.Instance == null)
{
Debug.LogError("[ClueUnlockTrigger] DetectiveBoardManager not found!", this);
return;
}
DetectiveBoardManager.Instance.UnlockClue(_clueToUnlock);
_hasUnlocked = true;
if (_debugMode)
Debug.Log($"[ClueUnlockTrigger] Unlocked clue: {_clueToUnlock.Title}", this);
OnClueUnlocked?.Invoke();
}
/// <summary>Resets the unlock state so the clue can be unlocked again.</summary>
public void ResetUnlock()
{
_hasUnlocked = false;
CancelInvoke(nameof(UnlockClue));
}
}
}