using UnityEngine;
using System.Collections;

public class EatingHandler : MonoBehaviour {
	private GameObject nearbyFood = null;
	private float lastAte = 0f;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		if (nearbyFood != null) {
			if(Input.GetButtonDown("Fire1"))
			{
				EatFood ();
			}
		}
	}

	void  EatFood()
	{
		if ((Time.time - lastAte) > 2f) {

			GetComponent<BirdAnimationManager> ().BirdIsEating ();
			GameObject.Destroy (nearbyFood.gameObject);
			BirdController.instance.Flaps = 3;
			nearbyFood = null;
			lastAte = Time.time;
		}
	}

	void OnTriggerEnter2D(Collider2D collider)
	{
		if (collider.gameObject.layer == 11) {
			nearbyFood = collider.gameObject.GetComponent<Food>().FoodOwner;
		}
	}

	void OnTriggerExit2D(Collider2D collider)
	{
		if (collider.gameObject.layer == 11) {
			if(collider.gameObject.GetComponent<Food>().FoodOwner == nearbyFood) {
				nearbyFood = null;
			}
		}
	}
}
