using UnityEngine; using Unheimlich.Features.DetectiveBoard.Data; using Unheimlich.Features.DetectiveBoard; namespace Unheimlich.Features.DetectiveBoard { /// /// Unlocks a clue when a specific audio source plays or finishes playing. /// Useful for triggering clues based on environmental sounds, dialogue, or hallucinations. /// [RequireComponent(typeof(AudioSource))] public class ClueUnlockOnAudio : MonoBehaviour { public enum UnlockTrigger { OnPlay, OnFinish, OnTimeReached } [Header("Clue Configuration")] [Tooltip("The clue to unlock")] [SerializeField] private ClueData _clueToUnlock; [Header("Audio Trigger")] [Tooltip("When to unlock the clue")] [SerializeField] private UnlockTrigger _trigger = UnlockTrigger.OnPlay; [Tooltip("Time in seconds to wait before unlocking (only for OnTimeReached)")] [SerializeField] private float _unlockTime = 1f; private AudioSource _audioSource; private bool _hasUnlocked; private bool _isMonitoring; private float _currentTime; private void Awake() { _audioSource = GetComponent(); } private void OnEnable() { if (_audioSource != null && _audioSource.isPlaying) { StartMonitoring(); } } private void Update() { if (!_isMonitoring || _hasUnlocked) return; if (_audioSource == null || !_audioSource.isPlaying) { if (_trigger == UnlockTrigger.OnFinish && _isMonitoring) { UnlockClue(); } _isMonitoring = false; return; } _currentTime = _audioSource.time; if (_trigger == UnlockTrigger.OnTimeReached && _currentTime >= _unlockTime) { UnlockClue(); } } /// /// Call this when the audio starts playing. /// public void OnAudioPlay() { StartMonitoring(); if (_trigger == UnlockTrigger.OnPlay) { UnlockClue(); } } private void StartMonitoring() { _isMonitoring = true; _currentTime = 0f; } private void UnlockClue() { if (_hasUnlocked || _clueToUnlock == null) return; if (DetectiveBoardManager.Instance != null) { DetectiveBoardManager.Instance.UnlockClue(_clueToUnlock); _hasUnlocked = true; Debug.Log($"[ClueUnlockOnAudio] Unlocked clue: {_clueToUnlock.Title} via audio trigger"); } } /// /// Resets the unlock state. /// public void ResetUnlock() { _hasUnlocked = false; _isMonitoring = false; } } }