GameVsJam/3d Prototyp/Assets/Scripts/MusicManager.cs

151 lines
4.0 KiB
C#
Raw Normal View History

2024-04-07 14:22:27 +02:00
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
2024-04-09 01:17:44 +02:00
using UnityEngine.Serialization;
2024-04-07 14:22:27 +02:00
using Utility;
public class MusicManager : MonoBehaviourSingleton<MusicManager>
{
[SerializeField]
private List<AudioClip> _ambientMusic;
2024-04-09 01:17:44 +02:00
[FormerlySerializedAs("_showDownMusic")] [SerializeField]
private AudioClip _winningMusic;
2024-04-07 14:22:27 +02:00
[SerializeField]
2024-04-09 01:17:44 +02:00
private AudioClip _losingMusic;
2024-04-07 14:22:27 +02:00
[SerializeField, ShowOnly]
private float _musicTimer = 5f;
private float _musicCheckInterval = 30f;
private AudioSource _audioSource;
private AudioClip _currentlyPlaying;
private float _defaultVolume;
2024-04-09 01:17:44 +02:00
private ShowDown _showDown = ShowDown.NotYet;
2024-04-07 14:22:27 +02:00
private bool _doneFading = false;
private bool _showDownIsPlaying = false;
2024-04-09 01:17:44 +02:00
private enum ShowDown
{
NotYet,
AlmostFinished,
TimesRunningOut
}
2024-04-07 14:22:27 +02:00
// Start is called before the first frame update
void Start()
{
_audioSource = GetComponent<AudioSource>();
_defaultVolume = _audioSource.volume;
}
// Update is called once per frame
void Update()
{
CheckForFinalMusic();
2024-04-09 01:17:44 +02:00
if (_showDown == ShowDown.NotYet)
2024-04-07 14:22:27 +02:00
{
_musicTimer -= Time.deltaTime;
2024-04-09 01:17:44 +02:00
2024-04-07 14:22:27 +02:00
if (_musicTimer <= 0)
{
_musicTimer = _musicCheckInterval;
if (!_audioSource.isPlaying)
{
PlayNewMusic();
}
}
}
}
private void FadeOutMusic()
{
_audioSource.volume -= _audioSource.volume * Time.deltaTime;
_audioSource.volume = _audioSource.volume <= 0.05f ? 0.0f : _audioSource.volume;
_doneFading = _audioSource.volume <= 0.05f;
}
private void CheckForFinalMusic()
{
2024-04-09 01:17:44 +02:00
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;
}
2024-04-09 02:47:54 +02:00
if (!GameManager.Instance.IsStarted)
{
_showDown = ShowDown.NotYet;
}
2024-04-09 01:17:44 +02:00
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));
2024-04-07 14:22:27 +02:00
}
2024-04-09 01:17:44 +02:00
[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();
}
2024-04-07 14:22:27 +02:00
private void PlayNewMusic()
{
if (_currentlyPlaying == null)
{
_audioSource.clip = _ambientMusic.GetRandomElement();
_audioSource.Play();
}
else
{
_ambientMusic.Remove(_currentlyPlaying);
AudioClip newMusic = _ambientMusic.GetRandomElement();
_ambientMusic.Add(_currentlyPlaying);
_audioSource.clip = newMusic;
_audioSource.Play();
}
}
}