2025-03-07 23:32:42 -05:00
|
|
|
#if NO
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data.Common;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using Unity.VisualScripting;
|
2025-04-18 15:54:50 -04:00
|
|
|
using UnityEngine;
|
|
|
|
|
using Game;
|
|
|
|
|
using Music;
|
|
|
|
|
using Player;
|
2025-03-07 23:32:42 -05:00
|
|
|
using UnityEngine.SceneManagement;
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
namespace Music
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <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
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// The playlist containing the music tracks and layers.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Playlist musicTrack;
|
|
|
|
|
|
|
|
|
|
/// <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()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
if (!GameManager.music)
|
|
|
|
|
{
|
|
|
|
|
Destroy(gameObject);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the music layers and updates them based on the current scene.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Start()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
foreach (var layer in musicTrack.trackLayers)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
if (layer.enableTrigger != TrackLayer.EnableTrigger.Scene)
|
|
|
|
|
{
|
|
|
|
|
persistentLayers.Add(layer);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentScene = GetActiveSceneNotStatistics();
|
2025-04-18 15:54:50 -04:00
|
|
|
InitializeLayers();
|
2025-03-07 23:32:42 -05:00
|
|
|
UpdateLayers(musicTrack.trackLayers);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Checks for scene changes and updates music layers accordingly.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void Update()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
CheckForRestartability();
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
if (currentScene != GetActiveSceneNotStatistics())
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
currentScene = GetActiveSceneNotStatistics();
|
|
|
|
|
UpdateLayers(musicTrack.trackLayers);
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
if (persistentLayers.Count != 0) UpdateLayers(persistentLayers);
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Creates and initializes audio sources for each music layer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void InitializeLayers()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
foreach (TrackLayer layer in musicTrack.trackLayers)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
AudioSource layerSource = Instantiate(layerPrefab, transform).GetComponent<AudioSource>();
|
|
|
|
|
layerSource.gameObject.name = layer.layerName;
|
|
|
|
|
layerSource.clip = layer.layerTrack;
|
|
|
|
|
layerSource.volume = 0;
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
try
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master/" + layer.layerName)[0];
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
2025-04-18 15:54:50 -04:00
|
|
|
catch
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
layerSource.outputAudioMixerGroup = musicTrack.defaultMixer.FindMatchingGroups("Master")[0];
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
layerSource.Play();
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <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)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
DisableLayer(layer);
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
// 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.
|
|
|
|
|
// ...
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Restarts audio sources if they have stopped playing.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void CheckForRestartability()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
bool restart = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < transform.childCount; i++)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
|
|
|
|
if (child != null && !child.isPlaying)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
|
|
|
|
restart = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
if (!restart) return;
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
for (int i = 0; i < transform.childCount; i++)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
AudioSource child = transform.GetChild(i).GetComponent<AudioSource>();
|
|
|
|
|
if (child != null)
|
|
|
|
|
{
|
|
|
|
|
child.Stop();
|
|
|
|
|
child.Play();
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <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, true);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <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)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
foreach (AnimatorControllerParameter parameter in transform.Find(layer.layerName).GetComponent<Animator>().parameters)
|
|
|
|
|
{
|
|
|
|
|
transform.Find(layer.layerName).GetComponent<Animator>().SetBool(parameter.name, false);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the currently active scene, excluding the "Statistics Manager Scene".
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>The active scene.</returns>
|
|
|
|
|
public static Scene GetActiveSceneNotStatistics()
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
for (int sceneIndex = 0; sceneIndex < SceneManager.sceneCount; sceneIndex++)
|
2025-03-07 23:32:42 -05:00
|
|
|
{
|
2025-04-18 15:54:50 -04:00
|
|
|
if (SceneManager.GetSceneAt(sceneIndex).name != "Statistics Manager Scene")
|
|
|
|
|
{
|
|
|
|
|
return SceneManager.GetSceneAt(sceneIndex);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
2025-04-18 15:54:50 -04:00
|
|
|
return SceneManager.GetSceneByBuildIndex(0);
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Stops the music and destroys the TrackManager after a short delay.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Stop()
|
|
|
|
|
{
|
|
|
|
|
StartCoroutine(DestroyTrack());
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
|
2025-04-18 15:54:50 -04:00
|
|
|
/// <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);
|
|
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
}
|
2025-04-18 15:54:50 -04:00
|
|
|
}
|
2025-03-07 23:32:42 -05:00
|
|
|
#endif
|