67 lines
2.2 KiB
C#
67 lines
2.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class BackgroundSoundsManager : MonoBehaviour
|
|
{
|
|
public float minSecondsToWaitForNextClip = 35f;
|
|
public float maxSecondsToWaitForNextClip = 60f;
|
|
[SerializeField] List<AudioSource> backgroundAudioSources;
|
|
[SerializeField] List<AudioClip> houseAudioClips;
|
|
public bool isUpdatingSounds;
|
|
float randomSecondsToWaitForNextSound;
|
|
|
|
private static BackgroundSoundsManager _instance;
|
|
public static BackgroundSoundsManager GetInstance() { return _instance; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (!_instance)
|
|
{
|
|
_instance = this;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
//Teleport top players position:
|
|
transform.position = PlayerManager.GetInstance().playerGameObj.transform.position;
|
|
}
|
|
|
|
public void PlayHouseSounds()
|
|
{
|
|
if (!isUpdatingSounds)
|
|
{
|
|
isUpdatingSounds = true;
|
|
StartCoroutine(BackgroundSoundUpdate(houseAudioClips));
|
|
}
|
|
}
|
|
|
|
public void StopBackgroundSounds()
|
|
{
|
|
isUpdatingSounds = false;
|
|
}
|
|
|
|
IEnumerator BackgroundSoundUpdate(List<AudioClip> audioClips)
|
|
{
|
|
yield return new WaitForSeconds(randomSecondsToWaitForNextSound);
|
|
while (isUpdatingSounds)
|
|
{
|
|
randomSecondsToWaitForNextSound = Random.Range(minSecondsToWaitForNextClip, maxSecondsToWaitForNextClip);
|
|
AudioSource randomAudioSource = backgroundAudioSources[Random.Range(0, backgroundAudioSources.Count)];
|
|
AudioClip randomClip = audioClips[Random.Range(0, audioClips.Count)];
|
|
|
|
//Play audio clip.
|
|
print("randomAudioSource: " + randomAudioSource.name + " Is playing Background sound: " + randomClip.name);
|
|
randomAudioSource.PlayOneShot(randomClip);
|
|
//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 Background sound");
|
|
yield return new WaitForSeconds(randomSecondsToWaitForNextSound);
|
|
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|