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

public class feastOnSheep : MonoBehaviour
{

	public float distanceToSheep;
	public bool readyToDie;
	public GameObject Manager;
	public float MeatAmount;
	public Transform splatter;
	public Transform blood;
	public Transform splattersound;

	// Use this for initialization
	void Start ()
	{
		readyToDie = false;
	}
	
	// Update is called once per frame
	void Update ()
	{
		List<GameObject> swarm = new List<GameObject> (GameObject.FindGameObjectsWithTag (tag));
		swarm.Remove (gameObject);
		GameObject nearestSheep = Utils.findNearestNeighbor (gameObject, swarm);
		readyToDie = Utils.getDistance (gameObject, nearestSheep) > distanceToSheep;
		gameObject.GetComponent<CircleCollider2D> ().enabled = readyToDie;
	}

	void OnTriggerEnter2D (Collider2D col)
	{
		if (col.gameObject.tag.Equals ("Wolf") && col.gameObject.layer != 8) {
			Manager.GetComponent<HungryWolfes> ().Hunger += MeatAmount;
			Manager.GetComponent<HungryWolfes> ().SheepsKilled += 1;
			Manager.GetComponent<HungryWolfes> ().SheepCounter += 1;
			Instantiate (splatter, transform.position + new Vector3(0,0,1), Quaternion.identity);
			Instantiate (blood, transform.position+ new Vector3(0,0,1), Quaternion.identity);
			Instantiate (splattersound, transform.position+ new Vector3(0,0,1), Quaternion.identity);
			GameObject.Destroy (gameObject);
		}
	}
}
