#pragma strict

// Use this for initialization
var rearWheel1 : WheelCollider;
var rearWheel2 : WheelCollider;
var frontWheel1 : WheelCollider;
var frontWheel2 : WheelCollider;
 
var wheelFL : Transform;
var wheelFR : Transform;
var wheelRL : Transform;
var wheelRR : Transform;

var up : GUITexture;
var down : GUITexture;
var left : GUITexture;
var right : GUITexture;
var fire : GUITexture;

var life1 : MeshRenderer;
var life2 : MeshRenderer;
var life3 : MeshRenderer;

var Player1 : TextMesh;
var Player2 : TextMesh;

var controlObject: Transform;

var steer_max = 20;
var motor_max = 40;
var brake_max = 100;
var steerSpeed = 20;

private var steer = 0;
private var forward = 0;
private var back = 0;
private var brakeRelease = false;
private var motor = 0;
private var brake = 0;
private var reverse = false;
private var speed = 0;

private var axisH: float = 0;
private var axisV: float = 0;

private var LIVES = 3;
private var gameisover = false;
private var pause = false;

function Start () {
	controlObject.rigidbody.centerOfMass = Vector3(0, -0.05, 0);
}

function Update() {	
	if (Input.GetKeyUp(KeyCode.Escape) && pause) {
		Time.timeScale = 1;
		pause = false;
	} else
	if (Input.GetKeyUp(KeyCode.Escape) && !pause) {
		pause = true;
		Time.timeScale = 0;
	}
}

function FixedUpdate () {
	var VeloX = controlObject.rigidbody.velocity.x;
	//print(VVeloX);
	var VeloY = controlObject.rigidbody.velocity.y;
	
	var VeloZ = controlObject.rigidbody.velocity.z;
	
	
	if(InputGetAxis("Horizontal") < 0) Control("left");
	if(InputGetAxis("Horizontal") > 0) Control("right");
	if(InputGetAxis("Vertical") > 0) Control("up");
	if(InputGetAxis("Vertical") < 0) Control("down");
	
	speed = rigidbody.velocity.sqrMagnitude;
	
	
	//steer = InputGetAxis("Horizontal");
	
	if ( steer == 0 && frontWheel1.steerAngle != 0) {
	if (Mathf.Abs(frontWheel1.steerAngle) <= (steerSpeed * Time.deltaTime)) {
		frontWheel1.steerAngle = 0;
	} else if (frontWheel1.steerAngle > 0) {
		frontWheel1.steerAngle = frontWheel1.steerAngle - (steerSpeed * Time.deltaTime);
	} else {
		frontWheel1.steerAngle = frontWheel1.steerAngle + (steerSpeed * Time.deltaTime);
	}
	} else {
		frontWheel1.steerAngle = frontWheel1.steerAngle + (steer * steerSpeed * Time.deltaTime);
		if (frontWheel1.steerAngle > steer_max) { frontWheel1.steerAngle = steer_max; }
		if (frontWheel1.steerAngle < -1 * steer_max) { frontWheel1.steerAngle = -1 * steer_max; }
	}
	frontWheel2.steerAngle = frontWheel1.steerAngle;
	wheelFL.localEulerAngles.y = frontWheel1.steerAngle;
	wheelFR.localEulerAngles.y = frontWheel2.steerAngle;
	speed *= 3;
	wheelFR.Rotate(0, 0, speed * -6 * Time.deltaTime);
	wheelFL.Rotate(0, 0, speed * -6 * Time.deltaTime);
	wheelRR.Rotate(0, 0, speed * -6 * Time.deltaTime);
	wheelRL.Rotate(0, 0, speed * -6 * Time.deltaTime);
	
	steer = 0;
	
	
	switch(LIVES)
	{
		case 3:
			life1.enabled = true;
			life2.enabled = true;
			life3.enabled = true;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.emit = false;
		break;
		case 2:
			life1.enabled = true;
			life2.enabled = true;
			life3.enabled = false;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.emit = false;
		break;
		case 1:
			life1.enabled = true;
			life2.enabled = false;
			life3.enabled = false;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.emit = true;
		break;
		case 0:
			life1.enabled = false;
			life2.enabled = false;
			life3.enabled = false;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.emit = true;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.maxEmission = 6;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.minEmission = 3;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.maxSize = 2;
		break;
		case -1:
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.emit = true;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.maxEmission = 25;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.minEmission = 25;
			this.GetComponentInChildren(ParticleEmitter).particleEmitter.maxSize = 2;
			GameOver();
		break;
	}
	
	/*
	if (Input.GetButtonUp(escape) && !pause) {
		pause = true;
		Time.timeScale = 0;
	}
	if (Input.GetButtonUp("escape") && pause) {
		pause = false;
		Time.timeScale = 1;
	}
	*/


}

function OnEnable(){
		Gesture.onMouse1E += Pressed;
		Gesture.onTouchE += Pressed;
}

function OnDisable(){
		Gesture.onMouse1E -= Pressed;
		Gesture.onTouchE -= Pressed;
}

/*	
	// Update is called once per frame
	void Update () {
		
		
		//limit the position of the control object
		float x=controlObject.position.x;
		float z=controlObject.position.z;
		
		//x=Mathf.Clamp(x, -5, 5);
		//z=Mathf.Clamp(z, -5, 5);
		
		controlObject.position=new Vector3(x, controlObject.position.y, z);
	}
	*/
function Control(ctrl) {
	//set a zero vector
	var moveDir: Vector3 = Vector3.zero;
	var fwd: Vector3 = transform.forward * 0;
	
	var eulerAngleVelocity: Vector3 = new Vector3(0, 0, 0);
	
	var VeloX = controlObject.rigidbody.velocity.x;
	//velocity.x;
	var VeloY = controlObject.rigidbody.velocity.y;
	var VeloZ = controlObject.rigidbody.velocity.z;
	
	speed = rigidbody.velocity.sqrMagnitude;
	
	if(ctrl == ("up")) fwd = transform.forward * -15000;
	if(ctrl == ("down")) fwd = transform.forward * 15000;
	speed = Mathf.Clamp(speed + steerSpeed, 0, steer_max * 5);
	if(ctrl == ("left"))
		//if (speed > 50)
		//eulerAngleVelocity = new Vector3(0, -speed, 0);
		steer = -1;
	if(ctrl == ("right"))
		//if (Mathf.Sqrt(Mathf.Pow(VeloX,2) + Mathf.Pow(VeloZ,2)) > 50)
		//if (speed > 50)
		//eulerAngleVelocity = new Vector3(0, speed, 0);
		steer = 1;
	
	//normalized the total moveDir
	moveDir=moveDir.normalized;
	
	//move the controlObject according to the input move direction
	controlObject.rigidbody.AddForce(fwd);
	
	//print("Steer=" + steer + " VeloX="+ Mathf.Round(VeloX) + "Velo:" +Mathf.Round(VeloY) +" "+ Mathf.Round(VeloZ)); 
	//controlObject.Translate(moveDir*Time.deltaTime*3);
	//Quaternion target = Quaternion.Euler(tiltAroundX, tiltAroundY, tiltAroundZ);
	//transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
	
	var deltaRotation: Quaternion = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
	//print(eulerAngleVelocity.y);
	
	//steer = Mathf.Clamp(eulerAngleVelocity.y, -1, 1);
	//else steer = 0;
	
	//rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
	
	
}

function InputGetAxis(axis: String): float {
	var v = Input.GetAxis(axis);
	if (Mathf.Abs(v) > 0.005) return v;
	if (axis=="Horizontal") return axisH;
	if (axis=="Vertical") return axisV;
}

	//call when the screen is touched/clicked
function Pressed(pos: Vector2) {
	//if any of the button is pressed, set the corresponding  move direction
	if(up.HitTest(pos)){
		Control("up");
		//axisV = 1;
		//moveDir+=new Vector3(0, 0, 1);
	}
	if(down.HitTest(pos)){
		Control("down");
		//axisV = -1;
		//moveDir+=new Vector3(0, 0, -1);
	}
	if(left.HitTest(pos)){
		//Input.GetAxis("Horizontal") = Input.GetAxis("Horizontal") -0.1;
		Control("left");
		//axisH = 1;
		//moveDir+=new Vector3(-1, 0, 0);
	}
	if(right.HitTest(pos)){
		//Input.GetAxis("Horizontal") = Input.GetAxis("Horizontal") +0.1;
		Control("right");
		//axisH = -1;
		//moveDir+=new Vector3(1, 0, 0);
	}
		//if(fire.HitTest(pos)){ }
}

function OnCollisionEnter(col: Collision) {
		//if (GameObject(col).name("missile")) lives -= 1;
		if(col.gameObject.name == "Missile(Clone)") LIVES -= 1;
		else print("not missile but" + col.gameObject.name);
}
	
function OnGUI() {
	var butH = 35; // buttonHeight
	var butW = 130; // buttonWidth
	
	if (pause) {
		if(GUI.Button(new Rect((Screen.width / 2) - butW, (Screen.height / 2) - (butH / 2), butW, butH), "Back to Menu")){
			Application.LoadLevel("Menu");
		}
		
		if(GUI.Button(new Rect((Screen.width / 2), (Screen.height / 2) - (butH / 2), butW, butH), "Restart")){
			Application.LoadLevel("BattleCars");
		}
		if(GUI.Button(new Rect((Screen.width / 2 - butW/2), (Screen.height / 2) - (butH * 1.5), butW, butH), "Resume")){
			Time.timeScale = 1;
			pause = false;
		}
	}
	if (gameisover) {
	
	butH = 35; // buttonHeight
	butW = 130; // buttonWidth
	
	// complete center: Rect((Screen.width / 2) - (butW / 2), (Screen.height / 2) - (butH / 2), butW, butH)
	
	if(GUI.Button(new Rect((Screen.width / 2) - butW, (Screen.height / 2) - (butH / 2), butW, butH), "Back to Menu")){
		Application.LoadLevel("Menu");
	}
	
	if(GUI.Button(new Rect((Screen.width / 2), (Screen.height / 2) - (butH / 2), butW, butH), "Play again")){
		Application.LoadLevel("BattleCars");
	}
	}
		
}

function GameOver() {
	Destroy(GameObject.Find("P1 Controls"));
	Destroy(GameObject.Find("P2 Controls"));
	Player1.transform.DetachChildren();
	Player2.transform.DetachChildren();
	gameisover = true;
	//Player1.GetComponentInChildren(MeshRenderer).enabled = false;
	//Player2.GetComponentInChildren(MeshRenderer).enabled = false;
	
	Player1.transform.position = Vector3(-30,12,-10);
	Player1.text = (controlObject.name + " is the winner!");
	Player1.fontSize = 50;
	Player2.transform.position = Vector3(30,12,2);
	Player2.text = (controlObject.name + " is the winner!");
	Player2.fontSize = 50;
	

	
}