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

public class RandomSoundPlayer : MonoBehaviour
{
	public AudioSource[] sounds;
	private float duration;

	public void PlaySound()
	{
		System.Random rnd = new System.Random();
        int sound = rnd.Next(sounds.Length); //TODO: how to get count of items?
        duration = sounds[sound].clip.length;
        StartCoroutine(PlayAndTrigger(sounds[sound]));
	}

    IEnumerator PlayAndTrigger(AudioSource sound)
    {
    	sound.Play();
    	yield return new WaitForSeconds(duration);
    	//Done with sound
    	EventManager.TriggerMobRandomSoundComplete();
    	transform.parent.GetComponent<MobBase>().DonePlayingRandomSound();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
