using UnityEngine; using Unheimlich.Core.Events; using Unheimlich.Features.DetectiveBoard.Data; namespace Unheimlich.Features.DetectiveBoard { /// /// 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. /// 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; /// Raised after the clue has been successfully unlocked. public event System.Action OnClueUnlocked; private void Awake() { // Optional composition with GameEventTrigger _eventTrigger = GetComponent(); 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; } /// /// Unlocks the associated clue. Can be called directly from external scripts or UnityEvents. /// 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(); } /// Resets the unlock state so the clue can be unlocked again. public void ResetUnlock() { _hasUnlocked = false; CancelInvoke(nameof(UnlockClue)); } } }