Added comments to everything

This commit is contained in:
djkellerman
2025-04-18 15:54:50 -04:00
parent a0305ea0e9
commit 213bb2d14b
39 changed files with 3166 additions and 1796 deletions

View File

@@ -1,120 +1,165 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using Game; using Music; using Player;
using UnityEngine.Assertions.Must;
using UnityEngine.Playables;
using UnityEngine;
using Game;
using Music;
using Player;
using UnityEngine.SceneManagement;
namespace Music
{
public class MusicManager : MonoBehaviour
{
public static MusicManager Instance;
public List<Playlist> playlists;
private Dictionary<string, Playlist> sceneToPlaylist = new Dictionary<string, Playlist>();
public GameObject songPrefab;
private void Awake() // Creates only one MusicManager instance at a time
/// <summary>
/// Manages the music playlists for the game.
/// Handles the initialization, playback, and shuffling of music tracks based on the active scene.
/// </summary>
public class MusicManager : MonoBehaviour
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(this.gameObject);
}
/// <summary>
/// A singleton instance of the <see cref="MusicManager"/> class.
/// Ensures only one instance of the MusicManager exists at a time.
/// </summary>
public static MusicManager Instance;
foreach (Playlist playlist in playlists)
/// <summary>
/// A list of playlists available in the game.
/// </summary>
public List<Playlist> playlists;
/// <summary>
/// A dictionary mapping scene names to their corresponding playlists.
/// </summary>
private Dictionary<string, Playlist> sceneToPlaylist = new Dictionary<string, Playlist>();
/// <summary>
/// The prefab used to create audio sources for playing songs.
/// </summary>
public GameObject songPrefab;
/// <summary>
/// Ensures only one instance of the MusicManager exists and initializes the scene-to-playlist mapping.
/// </summary>
private void Awake()
{
foreach (string scene in playlist.trackScenes)
if (Instance == null)
{
sceneToPlaylist.Add(scene, playlist);
Instance = this;
}
}
}
public void StartPlaylist() // Starts music playlist for each scene
{
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
StopAllCoroutines();
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
try
{
StartCoroutine(PlayPlaylist(sceneToPlaylist[GetActiveSceneNotTitleScreen()]));
}
catch (System.Exception)
{
print("No playlist found for this scene: " + GetActiveSceneNotTitleScreen());
}
}
public void StartPlaylist(string scene) // Sets music for Title Screen
{
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
StopAllCoroutines();
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
StartCoroutine(PlayPlaylist(sceneToPlaylist[scene]));
}
private IEnumerator PlayPlaylist(Playlist playlist)
{
while (true)
{
// Shuffles the playlist
List<AudioClip> randomized = new List<AudioClip>(playlist.songs);
for (int i = 0; i < randomized.Count; i++)
else
{
AudioClip temp = randomized[i];
int randomIndex = Random.Range(i, randomized.Count);
randomized[i] = randomized[randomIndex];
randomized[randomIndex] = temp;
Destroy(this.gameObject);
}
// Starts the music in the playlist
foreach (AudioClip song in randomized)
foreach (Playlist playlist in playlists)
{
AudioSource songInstance = Instantiate(songPrefab, transform).GetComponent<AudioSource>();
songInstance.clip = song;
songInstance.volume = playlist.volume;
songInstance.Play();
if (playlist.shuffleTime > 0f)
foreach (string scene in playlist.trackScenes)
{
yield return new WaitForSeconds(playlist.shuffleTime);
float time = 0f;
while (time < 5f)
sceneToPlaylist.Add(scene, playlist);
}
}
}
/// <summary>
/// Starts the music playlist for the current active scene.
/// </summary>
public void StartPlaylist()
{
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
StopAllCoroutines();
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
try
{
StartCoroutine(PlayPlaylist(sceneToPlaylist[GetActiveSceneNotTitleScreen()]));
}
catch (System.Exception)
{
print("No playlist found for this scene: " + GetActiveSceneNotTitleScreen());
}
}
/// <summary>
/// Starts the music playlist for a specific scene.
/// </summary>
/// <param name="scene">The name of the scene for which to start the playlist.</param>
public void StartPlaylist(string scene)
{
if (GetActiveSceneNotTitleScreen() == "Player Select") return;
StopAllCoroutines();
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
StartCoroutine(PlayPlaylist(sceneToPlaylist[scene]));
}
/// <summary>
/// Plays the specified playlist by shuffling and playing its songs in sequence.
/// </summary>
/// <param name="playlist">The playlist to play.</param>
/// <returns>An enumerator for the coroutine.</returns>
private IEnumerator PlayPlaylist(Playlist playlist)
{
while (true)
{
// Shuffle the playlist
List<AudioClip> randomized = new List<AudioClip>(playlist.songs);
for (int i = 0; i < randomized.Count; i++)
{
AudioClip temp = randomized[i];
int randomIndex = Random.Range(i, randomized.Count);
randomized[i] = randomized[randomIndex];
randomized[randomIndex] = temp;
}
// Play each song in the shuffled playlist
foreach (AudioClip song in randomized)
{
AudioSource songInstance = Instantiate(songPrefab, transform).GetComponent<AudioSource>();
songInstance.clip = song;
songInstance.volume = playlist.volume;
songInstance.Play();
if (playlist.shuffleTime > 0f)
{
songInstance.volume = playlist.volume * (1 - time / 5f);
time += Time.deltaTime;
yield return null;
yield return new WaitForSeconds(playlist.shuffleTime);
float time = 0f;
while (time < 5f)
{
songInstance.volume = playlist.volume * (1 - time / 5f);
time += Time.deltaTime;
yield return null;
}
}
else
{
yield return new WaitForSeconds(song.length);
}
}
else
{
yield return new WaitForSeconds(song.length);
}
Destroy(songInstance.gameObject);
}
}
}
public static string GetActiveSceneNotTitleScreen() // Finds the scene name besides Title Screen
{
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
{
if (SceneManager.GetSceneAt(sceneIndex).name != "Title Screen")
{
return SceneManager.GetSceneAt(sceneIndex).name;
Destroy(songInstance.gameObject);
}
}
}
return "Title Screen";
/// <summary>
/// Gets the name of the currently active scene, excluding the "Title Screen".
/// </summary>
/// <returns>The name of the active scene, or "Title Screen" if no other scene is active.</returns>
public static string GetActiveSceneNotTitleScreen()
{
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
{
if (SceneManager.GetSceneAt(sceneIndex).name != "Title Screen")
{
return SceneManager.GetSceneAt(sceneIndex).name;
}
}
return "Title Screen";
}
}
}
}