3DTD/Assets/Scripts/Manager/MusicManager.cs

40 lines
1.1 KiB
C#
Raw Normal View History

2024-04-21 06:59:36 +02:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MusicManager : MonoBehaviour
{
2024-04-21 10:38:33 +02:00
public static MusicManager Instance { get; private set; }
2024-04-21 06:59:36 +02:00
[SerializeField] AnimationCurve projectilesToIntensity;
[SerializeField]
AudioClip[] intensities;
private int prevIntinsity = -1;
private AudioSource[] sources;
void Awake()
{
sources = new AudioSource[intensities.Length];
}
private void Update()
{
int projectileCount = GameObject.FindObjectsByType<Projectile>(FindObjectsSortMode.None).Length;
int intensity = (int) projectilesToIntensity.Evaluate(projectileCount) - 1;
if (intensity > prevIntinsity)
{
sources[intensity] = AudioManager.PlaySound(intensities[intensity], Vector3.zero, false, false, true, true);
}
else if (intensity < prevIntinsity)
{
if (sources[prevIntinsity] != null)
sources[prevIntinsity].Stop();
}
prevIntinsity = intensity;
}
}