89 lines
3.0 KiB
C#
89 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class HallucinationsSoundsManager : MonoBehaviour
|
|
{
|
|
public float minSecondsToWaitForNextClip = 35f;
|
|
public float maxSecondsToWaitForNextClip = 60f;
|
|
[SerializeField] List<AudioSource> hallucinationAudioSources;
|
|
[SerializeField] List<AudioClip> hallucinationAudioClips;
|
|
public bool isUpdatingSounds;
|
|
float randomSecondsToWaitForNextSound;
|
|
|
|
private static HallucinationsSoundsManager _instance;
|
|
public static HallucinationsSoundsManager GetInstance() { return _instance; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
////Teleport top players position:
|
|
//transform.position = PlayerManager.GetInstance().playerGameObj.transform.position;
|
|
|
|
//Sanity.GetInstance().OnSane += StopHallucinationSounds;
|
|
//Sanity.GetInstance().OnMinorHallucinations += PlayHallucinationSounds;
|
|
StartCoroutine(LateStart());
|
|
}
|
|
|
|
//void OnEnable()
|
|
//{
|
|
// Sanity.GetInstance().OnSane += StopHallucinationSounds;
|
|
// Sanity.GetInstance().OnMinorHallucinations += PlayHallucinationSounds;
|
|
//}
|
|
|
|
IEnumerator LateStart()
|
|
{
|
|
yield return new WaitForSeconds(0.5f);
|
|
//Teleport top players position:
|
|
transform.position = PlayerManager.GetInstance().playerGameObj.transform.position;
|
|
|
|
Sanity.GetInstance().OnSane += StopHallucinationSounds;
|
|
Sanity.GetInstance().OnMinorHallucinations += PlayHallucinationSounds;
|
|
}
|
|
|
|
void OnDisable()
|
|
{
|
|
Sanity.GetInstance().OnSane -= StopHallucinationSounds;
|
|
Sanity.GetInstance().OnMinorHallucinations -= PlayHallucinationSounds;
|
|
}
|
|
|
|
public void PlayHallucinationSounds()
|
|
{
|
|
isUpdatingSounds = true;
|
|
StartCoroutine(HallucinationSoundUpdate());
|
|
}
|
|
|
|
public void StopHallucinationSounds()
|
|
{
|
|
isUpdatingSounds = false;
|
|
}
|
|
|
|
IEnumerator HallucinationSoundUpdate()
|
|
{
|
|
while (isUpdatingSounds)
|
|
{
|
|
randomSecondsToWaitForNextSound = Random.Range(minSecondsToWaitForNextClip, maxSecondsToWaitForNextClip);
|
|
AudioSource randomAudioSource = hallucinationAudioSources[Random.Range(0, hallucinationAudioSources.Count)];
|
|
AudioClip randomClip = hallucinationAudioClips[Random.Range(0, hallucinationAudioClips.Count)];
|
|
|
|
//Play audio clip.
|
|
print("randomAudioSource: " + randomAudioSource.name + " Is playing Hallucination sound: " + randomClip.name);
|
|
randomAudioSource.PlayOneShot(randomClip, 1); //volume
|
|
//Wait for audioSource to stop playing the clip.
|
|
yield return new WaitForSeconds(randomClip.length);
|
|
//Wait for random seconds to play the next clip.
|
|
print("Is waiting for next Hallucination sound");
|
|
yield return new WaitForSeconds(randomSecondsToWaitForNextSound);
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|