Add a option for randomized music

This commit is contained in:
2025-09-05 13:52:36 -07:00
parent 5eee7e69f1
commit 7f092dc977
4 changed files with 585 additions and 7 deletions

View File

@@ -6,28 +6,35 @@ public class GameMusicHandler : MonoBehaviour
public AudioClip[] audioClips;
private AudioSource audioSource;
private int lastIndex = -1;
private int currentIndex = 0;
private bool isPaused = false;
void Awake()
void Start()
{
audioSource = GetComponent<AudioSource>();
PlayRandomClip();
PlayNextClip();
}
void Update()
{
if (!audioSource.isPlaying && !isPaused) PlayRandomClip();
if (!audioSource.isPlaying && !isPaused) PlayNextClip();
}
void PlayRandomClip()
void PlayNextClip()
{
if (audioClips.Length == 0) return;
int index;
do
if (BazookaManager.Instance.GetSettingRandomMusic())
{
index = Random.Range(0, audioClips.Length);
} while (audioClips.Length > 1 && index == lastIndex);
do index = Random.Range(0, audioClips.Length);
while (audioClips.Length > 1 && index == lastIndex);
}
else
{
index = currentIndex;
currentIndex = (currentIndex + 1) % audioClips.Length;
}
lastIndex = index;
audioSource.clip = audioClips[index];