using UnityEngine;
using System.Collections;

public class PoacherKiller : Entity {

	protected float angle;

	public float speed;


	public void OnPoacher(Poacher poacher) {
		if (speed > Data.rhinoMaxSpeed / 2.0f) {
			//Vector2 velocity = getVelocity ();
			Vector2 loc = getLoc ();
			Vector2 target = poacher.getLoc ();
			Vector2 dir = (target - loc);
			//float ourAngle = Mathf.Atan2 (velocity.y, velocity.x);
			float ourAngle = angle;
			float theirAngle = Mathf.Atan2 (dir.y, dir.x);
			if (Mathf.Abs (Mathf.DeltaAngle (ourAngle * Mathf.Rad2Deg, theirAngle * Mathf.Rad2Deg)) < Mathf.PI / 2.0f * Mathf.Rad2Deg) {
				poacher.destroy();
			}
			else {
				addPoacherHit ();
			}
		}
		else {
			addPoacherHit ();
		}
	}

	public virtual void addPoacherHit() {
	}

	public void fixOrientation() {
		// calculate the y-position ahead of us
		Ray ray = new Ray(transform.position + new Vector3(Mathf.Cos (angle) * 5.0f, 50.0f, Mathf.Sin(angle) * 5.0f), new Vector3(0, -1, 0));
		RaycastHit hitInfo;
		if (Physics.Raycast (ray, out hitInfo, float.MaxValue, 1 << 9)) {
			Vector3 p = hitInfo.point;
			p.y += transform.localScale.y / 2.0f;
			Vector3 diff = p - transform.position;
			Quaternion q = Quaternion.LookRotation(diff, Vector3.up);
			transform.rotation = q;
		}
	}
}
