var speed = 3.0;
var jumpSpeed = 4.0;
var gravity = 10.0;
var acceleration = 1.0;
var accelerationAir = 1.0;

var rotateSpeed = 0.9;
var targetCamera : Transform;

private var glimpseNb = 0;

private var velocity = Vector3.zero;
private var orientation = 0.0;

private var grounded : boolean = false;

var sndSource : AudioSource;
var sndJump : AudioClip;
var sndDead : AudioClip;
var sndGet : AudioClip;
var sndYeah : AudioClip;

private var playdead = false;

function Start() {
	Respawn();
}

function FixedUpdate() {
	//transform.Rotate(0, Input.GetAxis("Horizontal") * rotateSpeed * Time.deltaTime, 0);
	
	if (transform.position.y < -10.0) {
		Die();
	}
	if (!playdead && transform.position.y < -2.0) {
		playdead = true;
		sndSource.PlayOneShot(sndDead);
	}
	
	var forward = Vector3(targetCamera.forward.x, 0, targetCamera.forward.z).normalized;
	var right = Vector3(targetCamera.right.x, 0, targetCamera.right.z).normalized;
	
	var inputDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
	if (inputDirection.magnitude > 1.0) {
		inputDirection.Normalize();
	}
	
	inputDirection = (forward * inputDirection.z + right * inputDirection.x);
	var targetSpeed = inputDirection * speed;

	if (grounded) {
		velocity.y = 0.0;
		velocity = targetSpeed * acceleration + (1.0 - acceleration) * velocity;
		
		if (Input.GetButton("Jump")) {
			velocity.y = jumpSpeed;
			sndSource.PlayOneShot(sndJump);
		}
	} else {
		var saveYSpeed = velocity.y;
		velocity.y = 0.0;
		velocity = targetSpeed * accelerationAir + (1.0 - accelerationAir) * velocity;
		velocity.y = saveYSpeed;
	}

	// Apply gravity
	velocity.y -= gravity * Time.deltaTime;
	
	// Move the controller
	var controller : CharacterController = GetComponent(CharacterController);
	var flags = controller.Move(velocity * Time.deltaTime);
	grounded = (flags & CollisionFlags.CollidedBelow) != 0;
	
	if (inputDirection.magnitude > .1) {
		var angle = (Mathf.Rad2Deg * Mathf.Atan2(inputDirection.x, inputDirection.z));
		
		if (orientation - angle > 180.0) {
			angle += 360.0;
		}

		if (orientation - angle < -180.0) {
			angle -= 360.0;
		}
		
		orientation = rotateSpeed * angle + (1 - rotateSpeed) * orientation;
		if (orientation > 360.0) {
			orientation = orientation - 360.0;
		} else if (orientation < 0.0) {
			orientation = orientation + 360.0;
		}
	}
	transform.rotation = Quaternion.Euler(0, orientation, 0);
}

function Update() {
	var main : Main = GameObject.Find("Main").GetComponent("Main");

	if (Input.GetButtonDown("Fire1") && glimpseNb > 0) {
		glimpseNb--;
		UseGlimpse(transform.position, 5);
	}
}

function Die() {
	Application.LoadLevel(Application.loadedLevel);  //TEST
	//Respawn();
}

function Respawn() {
	var spawnpoint : GameObject = GameObject.Find("Spawnpoint");
	transform.position = spawnpoint.transform.position;
	transform.rotation = spawnpoint.transform.rotation;
	
	velocity = Vector3(0,0,0);
	orientation = transform.rotation.eulerAngles.y;

	GameObject.Find("CameraTarget").GetComponent("CameraControl").ResetCamera();
}

function OnControllerColliderHit(hit : ControllerColliderHit) {
	if(hit.gameObject.tag == "tile" && grounded) {
		hit.gameObject.SendMessage("OnWalk", SendMessageOptions.DontRequireReceiver);
	}
	if(hit.gameObject.tag == "item") {
		hit.gameObject.SendMessage("OnGetItem", SendMessageOptions.DontRequireReceiver);
	}
} 

function UseGlimpse(pos : Vector3, radius) {
	var colliders : Collider[];
	colliders = Physics.OverlapSphere(pos, radius);
	for(var i : int = 0; i <= colliders.length-1; i++) {
		if(colliders[i].gameObject.tag == "tile") {
			colliders[i].gameObject.SendMessage("OnGlimpse", SendMessageOptions.DontRequireReceiver);
		}
	} 	
}

function AddGlimpse() {
	glimpseNb++;
}

function GetGlimpseNb() {
	return glimpseNb;
}

function GetPushSpeed() {
	return Mathf.Min(velocity.magnitude, speed);
}

function PlayGet() {
	sndSource.PlayOneShot(sndGet);
}

function PlayYeah() {
	sndSource.PlayOneShot(sndYeah);
}

@script RequireComponent(CharacterController)