Irgendjemand musste es ja machen...
This commit is contained in:
@ -3,14 +3,17 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Serialization;
|
||||
using Utility;
|
||||
|
||||
public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
{
|
||||
[SerializeField]
|
||||
private List<AudioClip> _ambientMusic;
|
||||
[FormerlySerializedAs("_showDownMusic")] [SerializeField]
|
||||
private AudioClip _winningMusic;
|
||||
[SerializeField]
|
||||
private AudioClip _showDownMusic;
|
||||
private AudioClip _losingMusic;
|
||||
|
||||
[SerializeField, ShowOnly]
|
||||
private float _musicTimer = 5f;
|
||||
@ -18,9 +21,16 @@ public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
private AudioSource _audioSource;
|
||||
private AudioClip _currentlyPlaying;
|
||||
private float _defaultVolume;
|
||||
private bool _showDownTime = false;
|
||||
private ShowDown _showDown = ShowDown.NotYet;
|
||||
private bool _doneFading = false;
|
||||
private bool _showDownIsPlaying = false;
|
||||
|
||||
private enum ShowDown
|
||||
{
|
||||
NotYet,
|
||||
AlmostFinished,
|
||||
TimesRunningOut
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
@ -34,10 +44,10 @@ public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
{
|
||||
CheckForFinalMusic();
|
||||
|
||||
if (!_showDownTime)
|
||||
if (_showDown == ShowDown.NotYet)
|
||||
{
|
||||
_musicTimer -= Time.deltaTime;
|
||||
|
||||
|
||||
if (_musicTimer <= 0)
|
||||
{
|
||||
_musicTimer = _musicCheckInterval;
|
||||
@ -47,18 +57,6 @@ public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (_doneFading)
|
||||
{
|
||||
if (!_showDownIsPlaying)
|
||||
PlayShowDownMusic();
|
||||
}
|
||||
else
|
||||
{
|
||||
FadeOutMusic();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FadeOutMusic()
|
||||
@ -70,10 +68,64 @@ public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
|
||||
private void CheckForFinalMusic()
|
||||
{
|
||||
float timeLeft = (float)GameManager.Instance.ExpectedRemainingGameDuration;
|
||||
_showDownTime = timeLeft < 0.9 * _showDownMusic.length;
|
||||
ShowDown oldShowDown = _showDown;
|
||||
|
||||
if (TimeManager.Instance.RealLifeSecondsUntilDeadline < 100)
|
||||
{
|
||||
_showDown = ShowDown.TimesRunningOut;
|
||||
}
|
||||
else if (GameManager.Instance.GameProgress > 0.85)
|
||||
{
|
||||
_showDown = ShowDown.AlmostFinished;
|
||||
}
|
||||
else
|
||||
{
|
||||
_showDown = ShowDown.NotYet;
|
||||
}
|
||||
|
||||
if (oldShowDown != _showDown)
|
||||
{
|
||||
switch (_showDown)
|
||||
{
|
||||
case ShowDown.NotYet:
|
||||
FadeToClip(_ambientMusic.GetRandomElement());
|
||||
break;
|
||||
case ShowDown.AlmostFinished:
|
||||
FadeToClip(_winningMusic);
|
||||
break;
|
||||
case ShowDown.TimesRunningOut:
|
||||
FadeToClip(_losingMusic);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void FadeToClip(AudioClip audioClip)
|
||||
{
|
||||
StartCoroutine(FadeToClipRoutine(audioClip));
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
private float _fadeDuration = 1.0f;
|
||||
|
||||
private IEnumerator FadeToClipRoutine(AudioClip audioClip)
|
||||
{
|
||||
float currentTime = 0;
|
||||
float start = _audioSource.volume;
|
||||
while (currentTime < _fadeDuration)
|
||||
{
|
||||
currentTime += Time.deltaTime;
|
||||
_audioSource.volume = Mathf.Lerp(start, 0.0f, currentTime / _fadeDuration);
|
||||
yield return null;
|
||||
}
|
||||
|
||||
_audioSource.Stop();
|
||||
_audioSource.volume = _defaultVolume;
|
||||
_audioSource.clip = audioClip;
|
||||
_audioSource.loop = true;
|
||||
_audioSource.Play();
|
||||
}
|
||||
|
||||
private void PlayNewMusic()
|
||||
{
|
||||
if (_currentlyPlaying == null)
|
||||
@ -90,14 +142,4 @@ public class MusicManager : MonoBehaviourSingleton<MusicManager>
|
||||
_audioSource.Play();
|
||||
}
|
||||
}
|
||||
|
||||
private void PlayShowDownMusic()
|
||||
{
|
||||
_showDownIsPlaying = true;
|
||||
_audioSource.Stop();
|
||||
_audioSource.volume = _defaultVolume;
|
||||
_audioSource.clip = _showDownMusic;
|
||||
_audioSource.loop = true;
|
||||
_audioSource.Play();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user