// ENUMERACION DE ESTADOS.
public enum EstadoPlayer {IDLE, WALK, ATTACK};
var estados : EstadoPlayer;

var IsMoving : boolean = false;
var speed : float = 6.0;
var jumpSpeed : float = 8.0;
var gravity : float = 20.0;

private var moveDirection : Vector3 = Vector3.zero;
private var grounded : boolean = false;

function Update () {
	var AttackButton = Input.GetMouseButtonDown (1);
	switch (estados){
	case (EstadoPlayer.IDLE):
		if (IsMoving == false)
			Debug.Log ("EstadoPlayer = IDLE");
		else
			estados = EstadoPlayer.WALK;
		break;
	case (EstadoPlayer.WALK):
		if (IsMoving == true)
			Debug.Log ("EstadoPlayer = WALK");
		else
			estados = EstadoPlayer.IDLE;
		break;
	case (EstadoPlayer.ATTACK):
		if (AttackButton && !IsMoving )
			Debug.Log ("EstadoPlayer = ATTACK");
	break;
	}
}

function FixedUpdate() {
	IsMoving = false;
	if (grounded) {
		// We are grounded, so recalculate movedirection directly from axes
		moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
		
		moveDirection = transform.TransformDirection(moveDirection);
		moveDirection *= speed;
		
		
		if (Input.GetButton ("Jump")) {
			moveDirection.y = jumpSpeed;
		}
	}
	else
		IsMoving = true;
	
	// Apply gravity
	moveDirection.y -= gravity * Time.deltaTime;
	
	//Debug.Log (moveDirection);
	if(moveDirection != new Vector3(0, -0.4, 0))
		IsMoving = true;
		
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(moveDirection * Time.deltaTime);
	grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}

@script RequireComponent(CharacterController)