using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MusicManager : MonoBehaviour
{
    public static MusicManager Instance { get; private set; }
    [SerializeField] AnimationCurve projectilesToIntensity;

    [SerializeField]
    AudioClip[] intensities;

    private int prevIntinsity = -1;

    private AudioSource[] sources;

    void Awake()
    {
        if (Instance != null)
        {
            Destroy(this);
            return;
        }
        Instance = this;
        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, true);
        }
        else if (intensity < prevIntinsity)
        {
            if (sources[prevIntinsity] != null)
                sources[prevIntinsity].Stop();
        }

        prevIntinsity = intensity;
    }
}