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

[RequireComponent (typeof (AudioSource))]
public class SoundEffects : MonoBehaviour {
	
	public static SoundEffects instance;

    public AudioClip pickUp, endGame, playerSpawn, click;
    //public AudioClip evocation;
	
	private AudioSource audioSource;
	private List<AudioSource> audioSources;
	
	void Awake () {
		if (instance != null)
			Destroy (instance.gameObject);
		instance = this;
		audioSource = GetComponent<AudioSource> ();
		audioSources = new List<AudioSource> ();
		
		DontDestroyOnLoad(this.gameObject);
	}
	
	public void PlaySound(AudioClip clip){
		if (audioSource.isPlaying) {
			AudioSource audioS = gameObject.AddComponent<AudioSource>();
			audioS.clip = clip;
			audioS.Play ();
			audioSources.Add(audioS);
			Destroy (audioS, clip.length);
		} else {
			audioSource.clip = clip;
			audioSource.Play ();
		}
	}
	
	public void StopAllSounds(){
		foreach(AudioSource audio in audioSources){
			if(audio != null)
				audio.Stop();
		}
		audioSources.Clear ();
		audioSource.Stop ();
	}
	
}
