Encounter / Assets / Scripts / AudioHandling / MusicManager.cs
MusicManager.cs
Raw
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// Handles primarily Music
/// </summary>
public class MusicManager : MonoBehaviour
{
    public static MusicManager Instance { get; private set; }   // Singleton pattern Instance call for Music
    
    [SerializeField] private List<SceneMusicCollectionSO> musicCol;      // The current collection of music

    private SceneMusicCollectionSO currentMusic;
    private static AudioSource audioSource;                     // Audio Source component linked to this manager
    private int indexRef;                                       // Reference to what scene index is currently active

    /// <summary>
    /// Retrieval system for the audio sources of the music manager
    /// </summary>
    /// <returns>The audio source attached to the music manager</returns>
    public AudioSource GetAudioSource() 
        => audioSource;
    /// <summary>
    /// Creates a singleton pattern of the 
    /// </summary>
    private void Awake()
    {
        Instance = this;

        audioSource = GetComponent<AudioSource>();
    }
    /// <summary>
    /// Collects necessary resources and starts up the title screen music since that is where the music manager is initialized
    /// </summary>
    private void Start()
    {
        currentMusic = musicCol[0];
        LoadMusic();
        indexRef = 0;
    }
    /// <summary>
    /// Simply awaits for the music to stop playing before starting up a random music clip again
    /// </summary>
    private void Update()
    {
        if (!audioSource.isPlaying)
            PlayMusic(currentMusic.musicList[Random.Range(0, currentMusic.musicList.Count)]);

        if (SceneManager.GetActiveScene().buildIndex != indexRef)
            DetermineNewSoundtrack();
    }
    /// <summary>
    /// Searches within the list of music bundles for the correct bundle with 
    /// the matching build index of the current scene before beginning to play the music
    /// </summary>
    public void DetermineNewSoundtrack(bool encounterCheck = false)
    {
        indexRef = SceneManager.GetActiveScene().buildIndex;
        bool alreadyPlaying = false;
        if (Globals.IsBossEncounter)
        {
            CallForBossMusic();
            return;
        }

        foreach (SceneMusicCollectionSO col in musicCol)
        {
            if (!encounterCheck)
            {
                if (col.BuildIndexId == indexRef)
                {
                    currentMusic = col;
                    break;
                }
            }
            else
            {
                if (col.LocationsTracksCanPlay.Contains(ClimateManager.Instance.GetLocation()))
                {
                    if (currentMusic == col)
                    {
                        alreadyPlaying = true;
                        break;
                    }

                    currentMusic = col;
                    break;
                }
            }
        }

        if (!alreadyPlaying)
            LoadMusic();
    }
    /// <summary>
    /// Loads music based on scene index from the audio collections from resources
    /// </summary>
    /// <param name="collection">The music collection is loaded into the music manager based off of resources loaded of the SO</param>
    public void LoadMusic()
        => PlayMusic(currentMusic.musicList[Random.Range(0, currentMusic.musicList.Count)]);
    /// <summary>
    /// Calls to play a clip of music
    /// </summary>
    /// <param name="clip"></param>
    private void PlayMusic(AudioClip clip)
    {
        audioSource.clip = clip;
        audioSource.Play();
    }

    public void CallForBossMusic()
    {
        currentMusic = musicCol[5];
        PlayMusic(currentMusic.musicList[0]);
    }
}