using UnityEngine;
using System.Collections;

public class TreeController : MonoBehaviour
{
		private float previousSpawn;
		private const float SPAWN_INTERVAL = 10.0f;

		public AudioClip chirp;
		public AudioClip creek;
	
		private Player getPlayer ()
		{
				return gameObject.GetComponent ("Player") as Player;
		}
	
		void Update ()
		{
				if ((Time.time - previousSpawn) < SPAWN_INTERVAL)
						return;
				previousSpawn = Time.time;

/*
				var colliders = Physics.OverlapSphere (transform.position, 50.0f);
				var pl = getPlayer ();
		
				foreach (var c in colliders) {
						Debug.Log ("collided with " + c.gameObject);
						if (c.gameObject.tag == "Tree") {
								Debug.Log ("found tree");
								if (UnityEngine.Random.value > .2f) {
										Debug.Log ("playing");
										AudioSource.PlayClipAtPoint (pl.CurrentState == Player.States.Scared ? creek : chirp,
				                             c.gameObject.transform.position);
								}
						}
				}
				*/

				if (UnityEngine.Random.value < .5f)
						return;
			
				var pl = getPlayer ();
				if (pl.Scared)
						gameObject.audio.PlayOneShot (creek);
				else
						gameObject.audio.PlayOneShot (chirp);
		}
}
