116 lines
3.2 KiB
C#
116 lines
3.2 KiB
C#
using UnityEngine;
|
|
using Unheimlich.Data.DetectiveBoard;
|
|
using Unheimlich.Managers;
|
|
|
|
namespace Unheimlich.Handlers.DetectiveBoard
|
|
{
|
|
/// <summary>
|
|
/// Unlocks a clue when a specific audio source plays or finishes playing.
|
|
/// Useful for triggering clues based on environmental sounds, dialogue, or hallucinations.
|
|
/// </summary>
|
|
[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<AudioSource>();
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Call this when the audio starts playing.
|
|
/// </summary>
|
|
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");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the unlock state.
|
|
/// </summary>
|
|
public void ResetUnlock()
|
|
{
|
|
_hasUnlocked = false;
|
|
_isMonitoring = false;
|
|
}
|
|
}
|
|
}
|