using UnityEngine;
using System.Collections;

/** AI controller specifically made for the spider robot.
 * The spider robot (or mine-bot) which is got from the Unity Example Project
 * can have this script attached to be able to pathfind around with animations working properly.\n
 * This script should be attached to a parent GameObject however since the original bot has Z+ as up.
 * This component requires Z+ to be forward and Y+ to be up.\n
 * 
 * It overrides the AIPath class, see that class's documentation for more information on most variables.\n
 * Animation is handled by this component. The Animation component refered to in #anim should have animations named "awake" and "forward".
 * The forward animation will have it's speed modified by the velocity and scaled by #animationSpeed to adjust it to look good.
 * The awake animation will only be sampled at the end frame and will not play.\n
 * When the end of path is reached, if the #endOfPathEffect is not null, it will be instantiated at the current position. However a check will be
 * done so that it won't spawn effects too close to the previous spawn-point.
 * \shadowimage{mine-bot.png}
 */
[RequireComponent(typeof(Seeker))]
public class MineBotAI : AIPath {
	
	public GameObject Bot;
	
	public float RadarRange;
	
	public float distancesearch;
	
	public float distancedeath;
	
	public float distance;
		
	/** Animation component.
	 * Should hold animations "awake" and "forward"
	 */
	public Animation anim;
	
	/** Minimum velocity for moving */
	public float sleepVelocity = 0.4F;
	
	/** Speed relative to velocity with which to play animations */
	public float animationSpeed = 0.2F;
	
	/** Effect which will be instantiated when end of path is reached.
	 * \see OnTargetReached */
	public GameObject endOfPathEffect;
	
	public new void Start () {
		
		RadarRange=12.0f;
		distance=15.0f;
		distancesearch=10.0f;
		distancedeath=1.2f;
		
		//Prioritize the walking animation
		anim["forward"].layer = 10;
		
		//Play all animations
		anim.Play ("awake");
		anim.Play ("forward");
		
		//Setup awake animations properties
		anim["awake"].wrapMode = WrapMode.Clamp;
		anim["awake"].speed = 0;
		anim["awake"].normalizedTime = 1F;
		
		//Call Start in base script (AIPath)
		base.Start ();
	}
	
	/** Point for the last spawn of #endOfPathEffect */
	protected Vector3 lastTarget;
	
	/**
	 * Called when the end of path has been reached.
	 * An effect (#endOfPathEffect) is spawned when this function is called
	 * However, since paths are recalculated quite often, we only spawn the effect
	 * when the current position is some distance away from the previous spawn-point
	*/
	public override void OnTargetReached () {
		
		if (endOfPathEffect != null && Vector3.Distance (tr.position, lastTarget) > 1) {
			GameObject.Instantiate (endOfPathEffect,tr.position,tr.rotation);
			lastTarget = tr.position;
		}
	}
	
	public override Vector3 GetFeetPosition ()
	{
		return tr.position;
	}
	
	protected new void Update () {
		
		if(distance<RadarRange)
		{
		Bot.layer=13;
		}
		else
		{
		Bot.layer=12;	
		}	
		
		if(distance<distancedeath)
		{
			PacManGame.state="loose";
			Application.LoadLevel("Menu");	
		}
		
		if(distance<distancesearch)
		{
			target=player;	
		}
		else
		{
			target=alternativetarget;
		}
		
		distance=Mathf.Sqrt(Mathf.Pow(transform.position.x-player.position.x,2)+Mathf.Pow(transform.position.z-player.position.z,2));
		
		//Get velocity in world-space
		Vector3 velocity;
		if (canMove) {
		
			//Calculate desired velocity
			Vector3 dir = CalculateVelocity (GetFeetPosition());
			
			//Rotate towards targetDirection (filled in by CalculateVelocity)
			if (targetDirection != Vector3.zero) {
				RotateTowards (targetDirection);
			}
			
			if (dir.sqrMagnitude > sleepVelocity*sleepVelocity) {
				//If the velocity is large enough, move
			} else {
				//Otherwise, just stand still (this ensures gravity is applied)
				dir = Vector3.zero;
			}
			
			if (navController != null)
				navController.SimpleMove (GetFeetPosition(), dir);
			else if (controller != null)
				controller.SimpleMove (dir);
			else
				Debug.LogWarning ("No NavmeshController or CharacterController attached to GameObject");
			
			velocity = controller.velocity;
		} else {
			velocity = Vector3.zero;
		}
		
		
		//Animation
		
		//Calculate the velocity relative to this transform's orientation
		Vector3 relVelocity = tr.InverseTransformDirection (velocity);
		
		if (velocity.sqrMagnitude <= sleepVelocity*sleepVelocity) {
			//Fade out walking animation
			anim.Blend ("forward",0,0.2F);
		} else {
			//Fade in walking animation
			anim.Blend ("forward",1,0.2F);
			
			//Modify animation speed to match velocity
			AnimationState state = anim["forward"];
			
			float speed = relVelocity.z;
			state.speed = speed*animationSpeed;
		}
	}
}
