129 lines
3.5 KiB
C#
129 lines
3.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class MusicManager : MonoBehaviour
|
|
{
|
|
[SerializeField] private float MusicVolume;
|
|
[SerializeField] private float TransitionDelay;
|
|
[SerializeField] private float HeroicMusicEndBuffer;
|
|
[SerializeField] private GameObject LoopTrack;
|
|
[SerializeField] private GameObject HeroicTrack;
|
|
[SerializeField] private MusicLoopController LoopController;
|
|
[SerializeField] private GameObject[] SceneTracks;
|
|
|
|
private bool HeroicMusicAvailable = true;
|
|
private bool GameMusicStopped = false;
|
|
|
|
// TODO: Supprimer cette ligne et lancer la musique héroique depuis un autre Manager
|
|
public bool DebugCallHeroicMusic = false;
|
|
// TODO: Supprimer cette ligne et arrêter la musique du jeu depuis un autre Manager
|
|
public bool DebugCallStopMusic = false;
|
|
|
|
private void OnEnable()
|
|
{
|
|
StartGameMusic();
|
|
}
|
|
|
|
// TODO: Supprimer le FixedUpdate et lancer la musique héroique et l'arrêt de la musique depuis un autre Manager
|
|
private void FixedUpdate()
|
|
{
|
|
if(DebugCallHeroicMusic == true)
|
|
{
|
|
PlayHeroicMusic();
|
|
}
|
|
|
|
if(DebugCallStopMusic == true && GameMusicStopped == false)
|
|
{
|
|
GameMusicStopped = true;
|
|
StopGameMusic();
|
|
}
|
|
}
|
|
|
|
private void StartGameMusic()
|
|
{
|
|
ClearAllMusic();
|
|
ApplyVolume();
|
|
|
|
LoopTrack.SetActive(true);
|
|
}
|
|
|
|
public void PlayHeroicMusic()
|
|
{
|
|
if(HeroicMusicAvailable == true)
|
|
{
|
|
HeroicMusicAvailable = false;
|
|
LoopController.FadeOut(TransitionDelay);
|
|
HeroicTrack.SetActive(true);
|
|
StartCoroutine(ContinueGameMusic());
|
|
}
|
|
}
|
|
|
|
public void StopGameMusic()
|
|
{
|
|
LoopController.FadeOut(TransitionDelay * 2.0f);
|
|
StartCoroutine(FadeOutMusic(TransitionDelay * 2.0f));
|
|
}
|
|
|
|
public void SetVolume(float volume)
|
|
{
|
|
if (volume > 1.0f)
|
|
{
|
|
MusicVolume = 1.0f;
|
|
}
|
|
else if (volume >= 0.0f)
|
|
{
|
|
MusicVolume = volume;
|
|
}
|
|
}
|
|
|
|
private void ClearAllMusic()
|
|
{
|
|
// Désactiver tous les morceaux
|
|
LoopTrack.SetActive(false);
|
|
HeroicTrack.SetActive(false);
|
|
}
|
|
|
|
private void ApplyVolume()
|
|
{
|
|
if (SceneTracks.Length > 0)
|
|
{
|
|
foreach (GameObject track in SceneTracks)
|
|
{
|
|
if(track.TryGetComponent(out AudioSource trackAudio))
|
|
{
|
|
trackAudio.volume = MusicVolume;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private IEnumerator ContinueGameMusic()
|
|
{
|
|
AudioSource heroicAudio = HeroicTrack.GetComponent<AudioSource>();
|
|
yield return new WaitForSeconds(heroicAudio.clip.length - HeroicMusicEndBuffer);
|
|
LoopController.ResumePlaying();
|
|
yield return new WaitForSeconds(HeroicMusicEndBuffer);
|
|
HeroicTrack.SetActive(false);
|
|
HeroicMusicAvailable = true;
|
|
}
|
|
|
|
private IEnumerator FadeOutMusic(float fadeTime)
|
|
{
|
|
AudioSource heroicAudio = HeroicTrack.GetComponent<AudioSource>();
|
|
float startVolume = heroicAudio.volume;
|
|
while (heroicAudio.volume > 0)
|
|
{
|
|
heroicAudio.volume -= startVolume * Time.deltaTime / fadeTime;
|
|
|
|
yield return null;
|
|
}
|
|
heroicAudio.Stop();
|
|
heroicAudio.volume = startVolume;
|
|
LoopTrack.SetActive(false);
|
|
HeroicTrack.SetActive(false);
|
|
HeroicMusicAvailable = true;
|
|
this.enabled = false;
|
|
}
|
|
}
|