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

public class AudioController : MonoBehaviour
{
	private AudioSource musicSource;
	private List<AudioSource> effectSource;

	void Awake()
	{
		musicSource = gameObject.AddComponent<AudioSource>();
		musicSource.loop = true;
		effectSource = new List<AudioSource>();
	}

	public void PlayMusic(AudioClip music)
	{
		musicSource.Stop();
		musicSource.clip = music;
		musicSource.Play();
	}

	public void PlaySoundEffect(AudioClip effect)
	{
		AudioSource newEffect = gameObject.AddComponent<AudioSource>();
		newEffect.clip = effect;
		newEffect.Play();
		effectSource.Add(newEffect);
	}

	void Update()
	{
		int offset = 0;
		for(int idx = 0; idx < effectSource.Count; idx++)
		{
			if(!effectSource[idx-offset].isPlaying)
			{
				AudioSource currentClip = effectSource[idx-offset];
				effectSource.Remove(currentClip);
				Destroy(currentClip);
				offset++;
			}
		}
	}
}
