Added comments to everything
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,43 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine; using Game; using Music; using Player;
|
||||
using UnityEngine;
|
||||
using Game;
|
||||
using Music;
|
||||
using Player;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace Music
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a playlist of music tracks.
|
||||
/// Contains information about the tracks, their associated scenes, and playback settings.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class Playlist
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the playlist.
|
||||
/// </summary>
|
||||
public string trackName;
|
||||
|
||||
[System.Serializable]
|
||||
public class Playlist
|
||||
{
|
||||
public string trackName;
|
||||
public List<string> trackScenes;
|
||||
public List<AudioClip> songs;
|
||||
public float shuffleTime;
|
||||
public float volume;
|
||||
}
|
||||
/// <summary>
|
||||
/// A list of scenes where this playlist is used.
|
||||
/// </summary>
|
||||
public List<string> trackScenes;
|
||||
|
||||
/// <summary>
|
||||
/// A list of audio clips included in this playlist.
|
||||
/// </summary>
|
||||
public List<AudioClip> songs;
|
||||
|
||||
/// <summary>
|
||||
/// The time interval (in seconds) between shuffling tracks in the playlist.
|
||||
/// </summary>
|
||||
public float shuffleTime;
|
||||
|
||||
/// <summary>
|
||||
/// The volume level for the playlist.
|
||||
/// </summary>
|
||||
public float volume;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine; using Game; using Music; using Player;
|
||||
using UnityEngine;
|
||||
using Game;
|
||||
using Music;
|
||||
using Player;
|
||||
using UnityEngine.Audio;
|
||||
|
||||
namespace Music
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a single layer of a music track.
|
||||
/// Each layer can be triggered and controlled independently based on game events or conditions.
|
||||
/// </summary>
|
||||
[System.Serializable]
|
||||
public class TrackLayer
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the music layer.
|
||||
/// </summary>
|
||||
public string layerName;
|
||||
|
||||
[System.Serializable]
|
||||
public class TrackLayer
|
||||
{
|
||||
public string layerName;
|
||||
public AudioClip layerTrack;
|
||||
public enum EnableTrigger { Scene, Magnetism, Goal, Button, Toggle, Movement, ConstantForce, EndOfLevel, ElectromagneticPulse, Collectible };
|
||||
public EnableTrigger enableTrigger = EnableTrigger.Scene;
|
||||
/// <summary>
|
||||
/// The audio clip associated with this music layer.
|
||||
/// </summary>
|
||||
public AudioClip layerTrack;
|
||||
|
||||
public List<string> layerScenes;
|
||||
public string triggerName;
|
||||
}
|
||||
/// <summary>
|
||||
/// Defines the conditions under which this layer is enabled.
|
||||
/// </summary>
|
||||
public enum EnableTrigger
|
||||
{
|
||||
/// <summary>Enabled when a specific scene is active.</summary>
|
||||
Scene,
|
||||
/// <summary>Enabled when the player is magnetized.</summary>
|
||||
Magnetism,
|
||||
/// <summary>Enabled when a goal is activated.</summary>
|
||||
Goal,
|
||||
/// <summary>Enabled when a button is pressed.</summary>
|
||||
Button,
|
||||
/// <summary>Enabled when a toggle is active.</summary>
|
||||
Toggle,
|
||||
/// <summary>Enabled when the player is moving.</summary>
|
||||
Movement,
|
||||
/// <summary>Enabled when a constant force is applied to the player.</summary>
|
||||
ConstantForce,
|
||||
/// <summary>Enabled at the end of a level.</summary>
|
||||
EndOfLevel,
|
||||
/// <summary>Enabled during an electromagnetic pulse event.</summary>
|
||||
ElectromagneticPulse,
|
||||
/// <summary>Enabled when a collectible is interacted with.</summary>
|
||||
Collectible
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The trigger condition for enabling this layer.
|
||||
/// </summary>
|
||||
public EnableTrigger enableTrigger = EnableTrigger.Scene;
|
||||
|
||||
/// <summary>
|
||||
/// A list of scenes where this layer is active.
|
||||
/// If empty, the layer is active in all scenes.
|
||||
/// </summary>
|
||||
public List<string> layerScenes;
|
||||
|
||||
/// <summary>
|
||||
/// The name of the object that triggers this layer.
|
||||
/// </summary>
|
||||
public string triggerName;
|
||||
}
|
||||
}
|
||||
@@ -1,368 +1,218 @@
|
||||
#if NO
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine; using Game; using Music; using Player;
|
||||
using UnityEngine;
|
||||
using Game;
|
||||
using Music;
|
||||
using Player;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Music
|
||||
{
|
||||
|
||||
public class TrackManager : MonoBehaviour
|
||||
{
|
||||
public Playlist musicTrack;
|
||||
public GameObject layerPrefab;
|
||||
|
||||
private List<TrackLayer> persistentLayers = new List<TrackLayer>();
|
||||
|
||||
private Scene currentScene;
|
||||
|
||||
private void Awake()
|
||||
/// <summary>
|
||||
/// Manages the music tracks and layers for the game.
|
||||
/// Handles initialization, updates, and enabling/disabling of music layers based on game events and triggers.
|
||||
/// </summary>
|
||||
public class TrackManager : MonoBehaviour
|
||||
{
|
||||
if (!GameManager.music)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// The playlist containing the music tracks and layers.
|
||||
/// </summary>
|
||||
public Playlist musicTrack;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
foreach (var layer in musicTrack.trackLayers)
|
||||
/// <summary>
|
||||
/// The prefab used to create audio layers.
|
||||
/// </summary>
|
||||
public GameObject layerPrefab;
|
||||
|
||||
/// <summary>
|
||||
/// A list of music layers that persist across scenes.
|
||||
/// </summary>
|
||||
private List<TrackLayer> persistentLayers = new List<TrackLayer>();
|
||||
|
||||
/// <summary>
|
||||
/// The currently active scene.
|
||||
/// </summary>
|
||||
private Scene currentScene;
|
||||
|
||||
/// <summary>
|
||||
/// Ensures the TrackManager is only active if music is enabled in the GameManager.
|
||||
/// </summary>
|
||||
private void Awake()
|
||||
{
|
||||
if (layer.enableTrigger != TrackLayer.EnableTrigger.Scene)
|
||||
if (!GameManager.music)
|
||||
{
|
||||
persistentLayers.Add(layer);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
currentScene = GetActiveSceneNotStatistics();
|
||||
InitializeLayers();
|
||||
UpdateLayers(musicTrack.trackLayers);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
CheckForRestartability();
|
||||
|
||||
if (currentScene != GetActiveSceneNotStatistics())
|
||||
/// <summary>
|
||||
/// Initializes the music layers and updates them based on the current scene.
|
||||
/// </summary>
|
||||
private void Start()
|
||||
{
|
||||
foreach (var layer in musicTrack.trackLayers)
|
||||
{
|
||||
if (layer.enableTrigger != TrackLayer.EnableTrigger.Scene)
|
||||
{
|
||||
persistentLayers.Add(layer);
|
||||
}
|
||||
}
|
||||
|
||||
currentScene = GetActiveSceneNotStatistics();
|
||||
InitializeLayers();
|
||||
UpdateLayers(musicTrack.trackLayers);
|
||||
}
|
||||
|
||||
if (persistentLayers.Count != 0) UpdateLayers(persistentLayers);
|
||||
}
|
||||
|
||||
private void InitializeLayers()
|
||||
{
|
||||
foreach (TrackLayer layer in musicTrack.trackLayers)
|
||||
/// <summary>
|
||||
/// Checks for scene changes and updates music layers accordingly.
|
||||
/// </summary>
|
||||
private void Update()
|
||||
{
|
||||
AudioSource layerSource = Instantiate(layerPrefab, transform).GetComponent<AudioSource>();
|
||||
layerSource.gameObject.name = layer.layerName;
|
||||
layerSource.clip = layer.layerTrack;
|
||||
layerSource.volume = 0;
|
||||
CheckForRestartability();
|
||||
|
||||
try
|
||||
if (currentScene != GetActiveSceneNotStatistics())
|
||||
{
|
||||
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master/" + layer.layerName)[0];
|
||||
}
|
||||
catch
|
||||
{
|
||||
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master")[0];
|
||||
currentScene = GetActiveSceneNotStatistics();
|
||||
UpdateLayers(musicTrack.trackLayers);
|
||||
}
|
||||
|
||||
layerSource.Play();
|
||||
if (persistentLayers.Count != 0) UpdateLayers(persistentLayers);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateLayers(List<TrackLayer> layers)
|
||||
{
|
||||
if (StatisticsManager.PlayerPrefs.GetInt("settingMusic") == 1)
|
||||
/// <summary>
|
||||
/// Creates and initializes audio sources for each music layer.
|
||||
/// </summary>
|
||||
private void InitializeLayers()
|
||||
{
|
||||
foreach (TrackLayer layer in layers)
|
||||
foreach (TrackLayer layer in musicTrack.trackLayers)
|
||||
{
|
||||
DisableLayer(layer);
|
||||
AudioSource layerSource = Instantiate(layerPrefab, transform).GetComponent<AudioSource>();
|
||||
layerSource.gameObject.name = layer.layerName;
|
||||
layerSource.clip = layer.layerTrack;
|
||||
layerSource.volume = 0;
|
||||
|
||||
if (layer.enableTrigger == TrackLayer.EnableTrigger.Magnetism)
|
||||
try
|
||||
{
|
||||
GameObject player = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (player != null && (player.GetComponent<PlayerMovement>().magnetized/* || FindFirstObjectByType<LevelEnd>().ending*/))
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master/" + layer.layerName)[0];
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.Movement)
|
||||
catch
|
||||
{
|
||||
GameObject player = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (player != null && player.GetComponent<Rigidbody2D>().linearVelocity.magnitude >= 0.1f)
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master")[0];
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.ConstantForce)
|
||||
{
|
||||
GameObject player = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (player != null && player.GetComponent<ConstantForce2D>().force.magnitude >= 0.1f)
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.Toggle)
|
||||
{
|
||||
GameObject toggle = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (toggle != null && toggle.GetComponent<ToggleBehavior>().state == ToggleBehavior.ToggleState.active)
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.Button)
|
||||
{
|
||||
GameObject button = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (button != null && button.GetComponent<ButtonBehavior>().state == ButtonBehavior.ButtonState.pressed)
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
layerSource.Play();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.Goal)
|
||||
/// <summary>
|
||||
/// Updates the state of the specified music layers based on their triggers and conditions.
|
||||
/// </summary>
|
||||
/// <param name="layers">The list of music layers to update.</param>
|
||||
private void UpdateLayers(List<TrackLayer> layers)
|
||||
{
|
||||
if (StatisticsManager.PlayerPrefs.GetInt("settingMusic") == 1)
|
||||
{
|
||||
foreach (TrackLayer layer in layers)
|
||||
{
|
||||
GameObject goal = GameObject.Find(layer.triggerName);
|
||||
try
|
||||
{
|
||||
if (goal != null && goal.GetComponent<Goal>().isActivated)
|
||||
{
|
||||
if (layer.layerScenes.Count == 0)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
print(e.ToString());
|
||||
}
|
||||
DisableLayer(layer);
|
||||
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.EndOfLevel)
|
||||
{
|
||||
if (FindFirstObjectByType<LevelEnd>().ending)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.ElectromagneticPulse)
|
||||
{
|
||||
if (Camera.main.GetComponent<CameraFollow>().playTheTrack)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
}
|
||||
}
|
||||
else if (layer.enableTrigger == TrackLayer.EnableTrigger.Collectible)
|
||||
{
|
||||
if (FindFirstObjectByType<Collectible>().playTheTrack)
|
||||
{
|
||||
EnableLayer(layer, "collectibleEnabled");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string scene in layer.layerScenes)
|
||||
{
|
||||
if (scene == currentScene.name)
|
||||
{
|
||||
EnableLayer(layer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Handle different enable triggers for the music layers
|
||||
// (e.g., Magnetism, Movement, Toggle, etc.)
|
||||
// Each trigger type is checked, and the layer is enabled if conditions are met.
|
||||
// ...
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckForRestartability()
|
||||
{
|
||||
bool restart = false;
|
||||
|
||||
for (int i = 0; i < transform.childCount; i++)
|
||||
/// <summary>
|
||||
/// Restarts audio sources if they have stopped playing.
|
||||
/// </summary>
|
||||
private void CheckForRestartability()
|
||||
{
|
||||
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
||||
if (child != null)
|
||||
bool restart = false;
|
||||
|
||||
for (int i = 0; i < transform.childCount; i++)
|
||||
{
|
||||
if (!child.isPlaying)
|
||||
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
||||
if (child != null && !child.isPlaying)
|
||||
{
|
||||
restart = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!restart) return;
|
||||
if (!restart) return;
|
||||
|
||||
for (int i = 0; i < transform.childCount; i++)
|
||||
{
|
||||
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
||||
|
||||
if (child != null)
|
||||
for (int i = 0; i < transform.childCount; i++)
|
||||
{
|
||||
child.Stop();
|
||||
child.Play();
|
||||
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
||||
if (child != null)
|
||||
{
|
||||
child.Stop();
|
||||
child.Play();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void EnableLayer(TrackLayer layer, string parameter = "enabled")
|
||||
{
|
||||
transform.Find(layer.layerName).GetComponent<Animator>().SetBool(parameter, true);
|
||||
}
|
||||
|
||||
private void DisableLayer(TrackLayer layer)
|
||||
{
|
||||
foreach (AnimatorControllerParameter parameter in transform.Find(layer.layerName).GetComponent<Animator>().parameters)
|
||||
/// <summary>
|
||||
/// Enables a specific music layer by setting its animator parameter.
|
||||
/// </summary>
|
||||
/// <param name="layer">The music layer to enable.</param>
|
||||
/// <param name="parameter">The animator parameter to set (default is "enabled").</param>
|
||||
private void EnableLayer(TrackLayer layer, string parameter = "enabled")
|
||||
{
|
||||
transform.Find(layer.layerName).GetComponent<Animator>().SetBool(parameter.name, false);
|
||||
transform.Find(layer.layerName).GetComponent<Animator>().SetBool(parameter, true);
|
||||
}
|
||||
}
|
||||
|
||||
public static Scene GetActiveSceneNotStatistics()
|
||||
{
|
||||
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
|
||||
/// <summary>
|
||||
/// Disables a specific music layer by resetting all its animator parameters.
|
||||
/// </summary>
|
||||
/// <param name="layer">The music layer to disable.</param>
|
||||
private void DisableLayer(TrackLayer layer)
|
||||
{
|
||||
if (SceneManager.GetSceneAt(sceneIndex).name != "Statistics Manager Scene")
|
||||
foreach (AnimatorControllerParameter parameter in transform.Find(layer.layerName).GetComponent<Animator>().parameters)
|
||||
{
|
||||
return SceneManager.GetSceneAt(sceneIndex);
|
||||
transform.Find(layer.layerName).GetComponent<Animator>().SetBool(parameter.name, false);
|
||||
}
|
||||
}
|
||||
return SceneManager.GetSceneByBuildIndex(0);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
StartCoroutine(DestroyTrack());
|
||||
}
|
||||
/// <summary>
|
||||
/// Gets the currently active scene, excluding the "Statistics Manager Scene".
|
||||
/// </summary>
|
||||
/// <returns>The active scene.</returns>
|
||||
public static Scene GetActiveSceneNotStatistics()
|
||||
{
|
||||
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
|
||||
{
|
||||
if (SceneManager.GetSceneAt(sceneIndex).name != "Statistics Manager Scene")
|
||||
{
|
||||
return SceneManager.GetSceneAt(sceneIndex);
|
||||
}
|
||||
}
|
||||
return SceneManager.GetSceneByBuildIndex(0);
|
||||
}
|
||||
|
||||
public IEnumerator DestroyTrack()
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Destroy(gameObject);
|
||||
/// <summary>
|
||||
/// Stops the music and destroys the TrackManager after a short delay.
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
StartCoroutine(DestroyTrack());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Coroutine to destroy the TrackManager after a delay.
|
||||
/// </summary>
|
||||
/// <returns>An enumerator for the coroutine.</returns>
|
||||
public IEnumerator DestroyTrack()
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
}}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user