#pragma strict

	var speed : float = 10;
	var translationV : float; 
	var translationH : float; 
	var turntranslationV : float; 
	var turntranslationH : float; 	
	var tickcount : float;
	var controller : int;
	var isWaving : boolean;
	var isFreezing : boolean;
	var isPushing : boolean;
	var isTeleporting : boolean;
	var animator : Animator;
	var sounds : AudioClip[];
	var lenghtready = 30;
	var lenghtmakemove = 80;
	var lenghtreadymove = 30;
	var lenghtmoving = 200;
	var sprRender : SpriteRenderer;
	var PlayerColor : Color;
	var dashDist : int = 0;
	var vibrate = false;
	var vibStrengthA : float = 0;
	var vibStrengthB : float = 0;
	var vibeConfirmAbility : float = 0.1;
	var tickVibeInput : int = 0;
	var turnDirDefined : boolean = false;
	
	
	function Start (){
	animator = GetComponent(Animator);
	sprRender = GetComponent(SpriteRenderer);
	sprRender.color = PlayerColor; 
	
	
	
	}
	function Update () {
		if (tickVibeInput >= 121) {
			tickVibeInput = 0;
			stopVibration();
		}
		if (tickVibeInput >= 1) {
			tickVibeInput++;
		}
	
		if(tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready){
		if (Mathf.Abs(Input.GetAxis ("Vertical" + controller)) > 0.9 || Mathf.Abs(Input.GetAxis ("Horizontal"+ controller)) > 0.9) {
			dashDist++;
		}
		/*translationV += Input.GetAxis ("Vertical" + controller) * speed;
		translationH += Input.GetAxis ("Horizontal" + controller) * speed;*/
		if ((Mathf.Abs(Input.GetAxis("turnVertical")) > 0.9 || Mathf.Abs(Input.GetAxis("turnHorizontal")) > 0.9)){
			turntranslationV = Input.GetAxis ("turnVertical" + controller);
			turntranslationH = Input.GetAxis ("turnHorizontal" + controller);
			turnDirDefined = true;
			
		}else{
		if (!turnDirDefined) {
		turntranslationV = Input.GetAxis ("Vertical" + controller);
		turntranslationH = Input.GetAxis ("Horizontal" + controller);		
		}
		}
		}
		if(tickcount == lenghtready + lenghtmakemove+ lenghtreadymove + 1){
		translationV = Input.GetAxis ("Vertical" + controller) * dashDist * speed;
		translationH = Input.GetAxis ("Horizontal" + controller) * dashDist * speed;
		Move(translationH, translationV);
		translationH = 0;
		translationV = 0;
	//	transform.Rotate(Vector3(0,0,Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg));
		transform.rotation.eulerAngles.z = Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg -90 ;
		//print( Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg - 90 );
		//var angle = Quaternion.Euler(0,0, Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg - 90 );
		//transform.rotation = transform.rotation.Slerp( transform.rotation, Quaternion.Euler(0,0, Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg - 90 ) , 0.1 * Time.deltaTimetime);
		//transform.rotation = Quaternion.Euler(0,0, Mathf.Atan2(turntranslationV, turntranslationH) * Mathf.Rad2Deg - 90);
		if(isTeleporting){
		transform.position=-transform.position;
		audio.PlayOneShot(sounds[4], 1.0f);

		}
		if(isFreezing){
		animator.SetBool("shieldOn",true);
		audio.PlayOneShot(sounds[2], 1.0f);
		}
		
		if(isPushing){
		animator.SetBool("push", true);
		audio.PlayOneShot(sounds[1], 1.0f);
	
		
	
		}
		if(isWaving){
		animator.SetBool("wave", true);
		audio.PlayOneShot(sounds[6], 1.0f);

		}
		
		}
		
		if(tickcount == lenghtready + lenghtmakemove+ lenghtreadymove + 60 && isFreezing)
		audio.PlayOneShot(sounds[3], 1.0f);
		
		if(tickcount == lenghtready + lenghtmakemove + lenghtreadymove + 30){
		animator.SetBool("push",false);
		animator.SetBool("wave",false);
		
		}

		if(tickcount == lenghtready + lenghtmakemove + lenghtreadymove + 100){
		animator.SetBool("shieldOn",false);
		animator.SetBool("shieldOff", true);
		
		}

			
		if(tickcount == lenghtready + lenghtmakemove + lenghtmoving + lenghtreadymove){
		animator.SetBool("shieldOff", false);
		turnDirDefined = false;
		tickcount = 0;
		dashDist = 0;
		isWaving=false;
		isPushing=false;
		isFreezing=false;
		isTeleporting=false;
		gameObject.rigidbody2D.isKinematic=false;
		if(isFreezing){

		
		}
		}
		

		tickcount ++;
		
		if(Input.GetButtonDown("y "+ controller) && tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready && !isFreezing && !isPushing && !isTeleporting && !isWaving){
			print("wave");
    		Wave();
    		setVibration(vibeConfirmAbility, vibeConfirmAbility, 20);
    		audio.PlayOneShot(sounds[5], 1.0f);
    	}
		if(Input.GetButtonDown("a "+ controller) && tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready && !isWaving && !isFreezing && !isTeleporting && !isPushing){
			print("push");
			audio.PlayOneShot(sounds[5], 1.0f);
    		setVibration(vibeConfirmAbility, vibeConfirmAbility, 20);
    		Push();
    	}
    	if(Input.GetButtonDown("x "+ controller) && tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready && !isWaving && !isPushing && !isTeleporting && !isFreezing){
    		print("Freez");
    		audio.PlayOneShot(sounds[5], 1.0f);
    		setVibration(vibeConfirmAbility, vibeConfirmAbility, 20);
    		Freeze();
    	}
    	if(Input.GetButtonDown("b "+ controller) && tickcount < lenghtready + lenghtmakemove && tickcount > lenghtready && !isWaving && !isPushing && !isFreezing && !isTeleporting){
    		print("tele");
    		Teleport();
    		setVibration(vibeConfirmAbility, vibeConfirmAbility, 20);
    		audio.PlayOneShot(sounds[5], 1.0f);
    	}
   }
	
	function Move( translationH:float,translationV:float){
	rigidbody2D.AddForce(Vector2(translationH, translationV));
	}
	
	function Wave(){
		isWaving=true;
	}
	
	function Push(){
		isPushing=true;
		print("push"+controller);
	}
	function Freeze(){
		isFreezing=true;
		gameObject.rigidbody2D.isKinematic=true;
		print("freeze"+controller);
		
	}
	function Teleport(){
		isTeleporting=true;
		
	}
	function setVibration (a: float, b:float, length:int) {
		if (length <= 120) {
		vibStrengthA = a;
		vibStrengthB = b;
		tickVibeInput = 121 - length;
		}	
	}
	function stopVibration () {
		vibStrengthA = 0.0;
		vibStrengthB = 0.0;
	}
	
	function OnTriggerStay2D (other : Collider2D){
		if(other!=null && isWaving){
			var distance : float;
			distance = Vector2.Distance(other.transform.position, transform.position);	
			other.attachedRigidbody.AddForce( (10/Mathf.Sqrt(distance)) * Vector2((other.transform.position.x- transform.position.x),(other.transform.position.y- transform.position.y)).normalized);
		}

	}
	
