Smooth 3C & simplify hallucination trigger

This commit is contained in:
Ferlat Thibaud
2026-03-22 18:03:21 +01:00
parent 0d968f75ab
commit 1589a7a543
24 changed files with 1318 additions and 621 deletions
@@ -1,115 +1,40 @@
using UnityEngine;
using Unheimlich.Core.Audio;
using Unheimlich.Features.DetectiveBoard.Data;
using Unheimlich.Features.DetectiveBoard;
namespace Unheimlich.Features.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.
/// Unlocks a clue when a specific AudioSource plays, finishes, or reaches a timestamp.
/// Call OnAudioPlay() from whatever drives the AudioSource to start monitoring.
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class ClueUnlockOnAudio : MonoBehaviour
public class ClueUnlockOnAudio : AudioTriggerBase
{
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()
protected override void Execute()
{
_audioSource = GetComponent<AudioSource>();
}
private void OnEnable()
{
if (_audioSource != null && _audioSource.isPlaying)
if (_clueToUnlock == null)
{
StartMonitoring();
}
}
private void Update()
{
if (!_isMonitoring || _hasUnlocked)
return;
if (_audioSource == null || !_audioSource.isPlaying)
{
if (_trigger == UnlockTrigger.OnFinish && _isMonitoring)
{
UnlockClue();
}
_isMonitoring = false;
Debug.LogError("[ClueUnlockOnAudio] No ClueData assigned!", this);
return;
}
_currentTime = _audioSource.time;
if (_trigger == UnlockTrigger.OnTimeReached && _currentTime >= _unlockTime)
if (DetectiveBoardManager.Instance == null)
{
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)
Debug.LogError("[ClueUnlockOnAudio] DetectiveBoardManager not found!", this);
return;
if (DetectiveBoardManager.Instance != null)
{
DetectiveBoardManager.Instance.UnlockClue(_clueToUnlock);
_hasUnlocked = true;
Debug.Log($"[ClueUnlockOnAudio] Unlocked clue: {_clueToUnlock.Title} via audio trigger");
}
DetectiveBoardManager.Instance.UnlockClue(_clueToUnlock);
if (_debugMode)
Debug.Log($"[ClueUnlockOnAudio] Unlocked clue '{_clueToUnlock.Title}' on '{_fireCondition}'.", this);
}
/// <summary>
/// Resets the unlock state.
/// </summary>
public void ResetUnlock()
{
_hasUnlocked = false;
_isMonitoring = false;
}
/// <summary>Resets the unlock state so the clue can be unlocked again.</summary>
public void ResetUnlock() => ResetFired();
}
}