using UnityEngine;
using System.Collections;

public class Goat : Movable {

	Vector2 direction;

	float nextActionTime;
	public bool returningToCenter = false;
	public bool hungry = false;
	public bool baby = false;

	public static Vector2 goatCenter;

	protected override void Start () {
		base.Start();

		var color = Color.white * (0.75f + Random.value * 0.25f);
		color.a = 1f;
		sr.color = color;
	}


	void Update () {
		if (!hungry) {
			if (returningToCenter) {
				var dirToCenter = goatCenter - rb.position;
				Move(dirToCenter);
			} else {
				nextActionTime -= Time.deltaTime;
				if (nextActionTime <= 0) {
					nextActionTime = 0.5f + Random.value * 2f;
					if (baby)
						nextActionTime *= 0.5f;
					if (Random.value < 0.5f)
						direction = Vector2.zero;
					else
						direction = (new Vector2(Random.value - 0.5f, Random.value - 0.5f)).normalized;
				}

				Move(direction);
			}
		}

		otherSpeed = baby ? 1.5f : 1f;

		anim.SetBool("Baby", baby);
		anim.SetBool("Hungry", hungry);
	}

	public override void OnEnterArea (Area area) {
		// goat returned
		if (area.name == "goat-center") {
			returningToCenter = false;
		}
	}

	public override void OnExitArea (Area area) {
		// goat escaped
		if (area.name == "goats") {
			returningToCenter = true;
		}
	}
}
