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

[CreateAssetMenu(menuName = "Sounds/Sound Effect")]
public class SoundEffect : ScriptableObject
{
    private static ObjectPool<AudioSource> m_SourcesPool =
        new ObjectPool<AudioSource>
        (
            delegate (AudioSource source)
            {
                return !source.isPlaying;
            },
            delegate ()
            {
                return new GameObject("AudioSource", typeof(AudioSource)).GetComponent<AudioSource>();
            }
        );

    [SerializeField] private AudioClip[] m_Clips;

    [Space]
    [SerializeField] float m_MinPitch;
    [SerializeField] float m_MaxPitch;

    [Space]
    [SerializeField] float m_MinVolume;
    [SerializeField] float m_MaxVolume;

    public void Play(AudioSource source)
    {
        source.volume = Random.Range(m_MinVolume, m_MaxVolume);
        source.pitch = Random.Range(m_MinPitch, m_MaxPitch);

        source.clip = m_Clips[Random.Range(0, m_Clips.Length)];

        source.Play();
    }

    public void Play(Vector3 position)
    {
        AudioSource source = m_SourcesPool.Get();
        source.transform.position = position;
        Play(source);
    }
}
