#pragma strict

enum RotationAxes { MouseXAndY, MouseX, MouseY }
public var axes : RotationAxes = RotationAxes.MouseXAndY;
public var sensitivityX : float = 10F;
public var sensitivityY : float = 17F;

public var minimumX : float = -10F;
public var maximumX : float = 10F;

public var minimumY : float = -10F;
public var maximumY : float = 10F;

//public Transform target;
public var damping : float = 6.0F;	
private var rotationY : float = 0F;
private var rotationX : float = 0F;


function Start(){
	// Make the rigid body not change rotation
	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function Update (){
	// No hacer ada en este script cuando estoy saltando y corriendo.
	if(FPCStatus.isRunning || FPCStatus.IsJumping)
		return;
		
	// No permitir giros cuado está apuntando.
	if(FPCStatus.IsAiming){
		transform.rotation = Camera.main.transform.rotation;
		return;
	}
	
	if(axes == RotationAxes.MouseXAndY && FPCStatus.IsAxisXActive  || FPCStatus.IsAxisYActive){
		var RotAuxX : float = FPCStatus.AxisXValue * sensitivityX;
		RotAuxX = Mathf.Clamp (RotAuxX, minimumX, maximumX);
		rotationX += RotAuxX;
		rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
		var RotTotalX : float = rotationX + transform.localEulerAngles.y;
		RotTotalX = LimitAngle (RotTotalX, maximumX, 360+minimumX);
		var RotTotalXLerp : float = Mathf.LerpAngle(transform.localEulerAngles.y, RotTotalX, damping*Time.deltaTime);
		
		var RotAuxY : float = FPCStatus.AxisYValue * sensitivityY;
		RotAuxY = Mathf.Clamp (RotAuxY, minimumY, maximumY);
		rotationY += RotAuxY;
		rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
		var RotTotalY : float = rotationY + transform.localEulerAngles.x;
		RotTotalY = LimitAngle (RotTotalY, maximumY, 360+minimumY);
		var RotTotalYLerp : float = Mathf.LerpAngle(transform.localEulerAngles.x, RotTotalY, damping*Time.deltaTime);
		
		transform.localEulerAngles = Vector3(RotTotalYLerp, RotTotalXLerp, 0);
	}
	else 
	if(axes == RotationAxes.MouseX && FPCStatus.IsAxisXActive){
		RotAuxX = FPCStatus.AxisXValue * sensitivityX;
		RotAuxX = Mathf.Clamp (RotAuxX, minimumX, maximumX);
		rotationX += RotAuxX;
		rotationX = Mathf.Clamp (rotationX, minimumX, maximumX);
		RotTotalX = rotationX + transform.localEulerAngles.y;
		RotTotalX = LimitAngle (RotTotalX, maximumX, 360+minimumX);
		RotTotalXLerp = Mathf.LerpAngle(transform.localEulerAngles.y, RotTotalX, damping*Time.deltaTime);
		transform.localEulerAngles = Vector3(transform.localEulerAngles.x, RotTotalXLerp, 0);
	}
	else
	if(FPCStatus.IsAxisYActive){
		RotAuxY = FPCStatus.AxisYValue * sensitivityY;
		RotAuxY = Mathf.Clamp (RotAuxY, minimumY, maximumY);
		rotationY += RotAuxY;
		rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
		RotTotalY = rotationY + transform.localEulerAngles.x;
		RotTotalY = LimitAngle (RotTotalY, maximumY, 360+minimumY);
		RotTotalYLerp = Mathf.LerpAngle(transform.localEulerAngles.x, RotTotalY, damping*Time.deltaTime);
		transform.localEulerAngles = Vector3(RotTotalYLerp, transform.localEulerAngles.y, 0);
	}
	
	// Hace el seguimiento para mirar al target en caso de que no estés girando el raton.
	if(!FPCStatus.IsAxisXActive && !FPCStatus.IsAxisYActive)
		transform.rotation = Quaternion.Slerp(transform.rotation, Camera.main.transform.rotation, damping * Time.deltaTime);
}

function LimitAngle (angle: float, min: float, max: float) : float{
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	if(angle > min && angle < 300)
		angle = min;
	else
	if(angle < max && angle > 100)
		angle = max;
	
	return angle;
}
