using UnityEngine;
using System.Collections;

public class SoundManager : MonoBehaviour
{

    public static SoundManager instance = null;
    public float minPitch = 0.9f;
    public float maxPitch = 1.1f;

    //private AudioSource audioSource;

    // Use this for initialization
    void Start()
    {
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
            return;
        }

        //audioSource = GetComponent<AudioSource>();
    }

    public void PlaySound(AudioClip audioClip, bool variedPitch)
    {
        PlaySound(audioClip, variedPitch, 1f);
    }

    public void PlaySound(AudioClip audioClip, bool variedPitch, float volume)
    {
        PlaySound(audioClip, variedPitch ? Random.Range(minPitch, maxPitch) : 1f, volume);
    }

    public void PlaySound(AudioClip audioClip, float pitch, float volume)
    {
        AudioSource audioSource = gameObject.AddComponent<AudioSource>(); //Dynamically stack sounds. Inefficient? 
        audioSource.clip = audioClip;
        audioSource.volume = volume;
        audioSource.pitch = pitch;
        audioSource.Play();
        Destroy(audioSource, audioClip.length);
    }
}
