#pragma strict
var sprite : SpriteRenderer;
var direction : int = 4;
var keyboardEnabled : boolean = true;
var ani : Animator;
var cp = 0;

var players : String[,] = new String[4,4];
//Player1
players[0,0] = "w";
players[0,1] = "a";
players[0,2] = "s";
players[0,3] = "d";
//Player2
players[1,0] = "y";
players[1,1] = "g";
players[1,2] = "h";
players[1,3] = "j";
//Player3
players[2,0] = "p";
players[2,1] = "l";
players[2,2] = ";";
players[2,3] = "'";
//Player4
players[3,0] = "up";
players[3,1] = "left";
players[3,2] = "down";
players[3,3] = "right";

var score0:float=0;
var score1:float=0;
var score2:float=0;
var score3:float=0;

ani = GetComponent(Animator);
function Start () {

sprite = GetComponent(SpriteRenderer);
ani.SetBool("Dusted",false);
ani.SetInteger("direction",4);
}

function Update ()
{
	
	if(keyboardEnabled)
		if(Input.GetKey(players[cp,2]))
		{
		addscore(0.005);
			
			direction = 0;//s,h,;,down
			ani.SetInteger("direction",direction);
			
			
		}
		else if(Input.GetKey(players[cp,0]))
		{
			addscore(0.005);
			direction = 2;//w,y,p,up
			ani.SetInteger("direction",direction);
			
			
		}
		else if(Input.GetKey(players[cp,1]))
		{
			addscore(0.005);
			direction = 1;
			ani.SetInteger("direction",direction);
			
			
		}
		else if(Input.GetKey(players[cp,3]))
		{
			addscore(0.005);
			direction = 3;
			ani.SetInteger("direction",direction);
			
			
		}
		else
		{
			direction = 4;
			ani.SetInteger("direction",direction);
			
		}
	else
		Input.ResetInputAxes();
	
}

function OnDeath()
{	
	
	if(cp + 1 != 4)
		cp++;
	else 
		cp = 0;
	keyboardEnabled = false;
	ani.SetBool("Dusted", true);
	yield WaitForSeconds(1.00);
	ani.SetBool("Dusted", false);
	yield WaitForSeconds(2.5);
	var respawns = GameObject.FindGameObjectsWithTag("Player");
	for(var respawn in respawns)
		respawn.transform.position = Vector3(0,5.5,0);
	//transform.position = Vector3(0,1.8,0);
	yield WaitForSeconds(2.0);

	keyboardEnabled = true; 
}

function Dusty()
{	
	var dust : AudioClip = Resources.Load("SFX/death_1", typeof(AudioClip));
	audio.PlayOneShot(dust, 0.99);
}





function addscore(amount:float){
	switch(cp)
	{
		case 0:
			score0+=amount;
			
			
			break;
		case 1:
			score1+=amount;
			
			break;
		case 2:
			score2+=amount;
			
			break;
		case 3:
			score3+=amount;
			
			break;
		default: Debug.Log("");
		}

}



