#pragma strict

// The start waypoint, this is initialized in Awake.
// This variable is static thus all instances of the waypoint script share it.
//public  DirectionalWaypoint start;

// The next waypoint, this variable needs to be assigned in the inspector.
// You can select all waypoints to see the full waypoint path.
public var next : DirectionalWaypoint;

// This is used to determine where the start waypoint is.
public var isStart : boolean = false;

// Returns where the AI should drive towards.
// position is the current position of the car.
function CalculateTargetPosition (position:Vector3, nDistance:float) : DirectionalWaypoint{
	// If we are getting close to the waypoint, we return the next waypoint.
	// This gives us better car behaviour when cars don't exactly hit the waypoint
	if (Vector3.Distance (transform.position, position) < nDistance) {
		return next;
	}
	// We are still far away from the next waypoint, just return the waypoints position
	else {
		return this;
	}
}

function CalculateDistance (position:Vector3) : float {
	// If we are getting close to the waypoint, we return the next waypoint.
	// This gives us better car behaviour when cars don't exactly hit the waypoint
	return Vector3.Distance (transform.position, position);
}

// This initializes the start and goal static variables.
// We have to do this inside Awake because the waypoints need 
// to be initialized before the AI scripts use it.
// All Awake function are always called before all Start functions.
function Awake () {
	if (!next)
		Debug.Log ("This waypoint is not connected, you need to set the next waypoint!", this);
}

// Draw the waypoint pickable gizmo
function OnDrawGizmos () {
	Gizmos.DrawIcon (transform.position, "waypoint.png");
	if (next) {
		Gizmos.color = Color.green;
		Gizmos.DrawLine (transform.position, next.transform.position);
	}
}

// Draw the waypoint lines only when you select one of the waypoints
function OnDrawGizmosSelected () {
	Gizmos.DrawIcon (transform.position, "waypoint.png");
	if (next) {
		Gizmos.color = Color.red;
		Gizmos.DrawLine (transform.position, next.transform.position);
	}
}