#pragma strict

var isMoving : boolean;
var tickcount : int;
var playerPrefab : GameObject;
var lenghtready = 30;
var lenghtmakemove = 80;
var lenghtreadymove = 30;
var lengthmoving = 200;
var ready : GameObject;
var make : GameObject;
var setS : GameObject;
var go : GameObject;
var black : GameObject;
var blackRenderer : SpriteRenderer;
var winsprite : GameObject;

function Start () {
	blackRenderer = black.GetComponent(SpriteRenderer);
	for (var i : int = 0; i < 4; i++) {
		if(gameObject.Find("ControllerManager").GetComponent(js_assignctrl).getPlayerControl(i)!=null){
		if(i == 0){
		var other0 =	Instantiate (playerPrefab,Vector3(-5, 3, 0), Quaternion.identity);
		other0.gameObject.GetComponent(Movement).controller= gameObject.Find("ControllerManager").GetComponent(js_assignctrl).getPlayerControl(i);
		other0.gameObject.GetComponent(Movement).PlayerColor = Color(255, 255, 255, 255);
		}
		if(i == 1){
		var other1 =	Instantiate (playerPrefab,Vector3(5, -3, 0), Quaternion.identity);
		other1.gameObject.GetComponent(Movement).controller= gameObject.Find("ControllerManager").GetComponent(js_assignctrl).getPlayerControl(i);
		other1.gameObject.GetComponent(Movement).PlayerColor = Color(0.5, 1, 0.5, 1);
		}
		if(i == 2){
		var other2 =	Instantiate (playerPrefab,Vector3(-5, -3, 0), Quaternion.identity);
		other2.gameObject.GetComponent(Movement).controller= gameObject.Find("ControllerManager").GetComponent(js_assignctrl).getPlayerControl(i);
		other2.gameObject.GetComponent(Movement).PlayerColor = Color(1, 0.3, 0.35, 1); 
		}
		if(i == 3){
		var other3 =	Instantiate (playerPrefab,Vector3(5, 3, 0), Quaternion.identity);
		other3.gameObject.GetComponent(Movement).controller= gameObject.Find("ControllerManager").GetComponent(js_assignctrl).getPlayerControl(i);
		other3.gameObject.GetComponent(Movement).PlayerColor = Color(0.48, 0.75, 0.75, 1); 
		}
		
		}
	}
}

function Update () {


		if(tickcount < lenghtready + lenghtmakemove){
			isMoving=false;
			Time.timeScale=0;
		}
		if(tickcount == lenghtready + lenghtmakemove + 1){
			isMoving=true;
			Time.timeScale=1;
		}	
		if(tickcount == lenghtready + lenghtmakemove + lengthmoving + lenghtreadymove){
		tickcount = 0;
		}
	
		
		
	if(tickcount < lenghtready){
    black.renderer.enabled=true;
    blackRenderer.color = Color(0.6,0,0,0.1);
    ready.renderer.enabled=true;
    }
	if(tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready){
	ready.renderer.enabled=false;
	blackRenderer.color = Color(1.0,1.0,1.0,0.1);
    make.renderer.enabled=true;
    }
    
    if(tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready+30){
    make.renderer.enabled=false;
    }
    if(tickcount > lenghtready + lenghtmakemove + 1 && tickcount < lenghtready + lenghtreadymove +  lenghtmakemove ){
    make.renderer.enabled=false;
    blackRenderer.color = Color(0.6,0,0,0.1);
    setS.renderer.enabled=true;
    }  
    
    if(tickcount > lenghtready + lenghtmakemove + lenghtreadymove + 1){
    black.renderer.enabled=false;
  
    setS.renderer.enabled=false;
    go.renderer.enabled=true;
  	}
    if(tickcount > lenghtready + lenghtmakemove + lenghtreadymove + 21){
    go.renderer.enabled=false;
    }

	tickcount ++;
	
	if(gameObject.Find("ControllerManager").GetComponent(js_assignctrl).numberOfPlayers <= 1) {
		winsprite.renderer.enabled = true;	
		win();
	}
}

	function OnGUI () {

	
	 if (GUI.Button (Rect (110,10,115,20), "Quit")) {
     Application.Quit();
 }


    
    }
    function win () {
    	yield WaitForSeconds(3);
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).player[0] = null;
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).player[1] = null;
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).player[2] = null;
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).player[3] = null;
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).playerNumber = 1;
    	gameObject.Find("ControllerManager").GetComponent(js_assignctrl).numberOfPlayers = 0;
    	Application.LoadLevel("menu");
    }