#pragma strict

	var  playerNumber : int =1;
	var  isChecking : boolean = false;
	var assigned : boolean = false;
	var currentJoystick;
	var player  = new Array(null,null,null,null);
	var fu : boolean;
	var ship1 : GameObject;
	var ship2 : GameObject;
	var ship3 : GameObject;
	var ship4 : GameObject;
	var numberOfPlayers : int = 0;

	

	function Update(){
		if (playerNumber < 5 && Application.loadedLevel==1) {
		/*if (Mathf.Abs(Input.GetAxis("Vertical")) > 0.2 || Mathf.Abs(Input.GetAxis("Horizontal")) > 0.2){
			isChecking=true;
		}else{
			isChecking=false;
		}*/
//		print(isChecking);
//		print(playerNumber);
		/*for( int i = 0; i<3;i++){
			print(player[i]);
		}*/
		for (var i : int = 0; i < 11; i++) {			
			if ((Mathf.Abs(Input.GetAxis("Vertical"+i.ToString())) > 0.9 || Mathf.Abs(Input.GetAxis("Horizontal"+i.ToString())) > 0.9) && i != player[0] && i != player[1] && i != player[2] && i != player[3]){
//				Debug.Log (Input.GetJoystickNames()[i-1]+" is moved");
				assigned = true;
				player[playerNumber-1]=i;
				numberOfPlayers++;
			}else if(assigned){

				assigned = false;
				playerNumber++;
			}
		}
		}
		if(Application.loadedLevel==1){
			if (playerNumber < 5) {
			if(player[0]!=null)
			ship1.renderer.enabled=true;
			if(player[1]!=null)
			ship2.renderer.enabled=true;
			if(player[2]!=null)
			ship3.renderer.enabled=true;
			if(player[3]!=null)
			ship4.renderer.enabled=true;
			}
		}
	}

	function getPlayerControl (trg:int) {
		return player[trg];
	}

	
	/*function OnGUI () {
		if(Application.loadedLevel==0){
			if (playerNumber < 5) {
			GUI.Box (new Rect (30,60,1000,50), "Player" + playerNumber +  "use left Joystick" );
			}
			if(player[0]!=null)
			GUI.Box (new Rect (0,60,300,50), "Player1 Controller: " + player[0].ToString ());
			if(player[1]!=null)
			GUI.Box (new Rect (0,120,300,50), "Player2 Controller: " + player[1].ToString ());
			if(player[2]!=null)
			GUI.Box (new Rect (0,180,300,50), "Player3 Controller: " + player[2].ToString ());
			if(player[3]!=null)
			GUI.Box (new Rect (0,240,300,50), "Player4 Controller: " + player[3].ToString ());
		}
	}*/

