using UnityEngine;
using System.Collections;

public class player2_controls : MonoBehaviour {
	public float speed = 5;
	public static string axis = "Horizontal2";
	public bool iAmHuman = true;
	public static string axis2 = "Vertical2";
	public float multiplyer = 1;
	public bool iAmCrab = false;
   

	
	// Update is called once per frame
	void Update () {
		if (!iAmCrab) {
			float x = Input.GetAxis (axis) * speed * Time.deltaTime * multiplyer;
			float z = Input.GetAxis (axis2) * speed * Time.deltaTime * multiplyer;
			transform.Translate (x, 0, z, Space.World);

			if(iAmHuman && (x != 0 || z !=0)){
				this.gameObject.renderer.enabled = false;
				Transform obj = transform.GetChild(0);
				obj.animation["chuze"].speed = 2f;
				obj.gameObject.SetActive(true);
				obj.animation.Play("chuze");
			}
            if (!(iAmHuman && (x != 0 || z != 0)))
            {
				Transform obj = transform.GetChild(0);
				obj.animation.Stop();
				obj.gameObject.SetActive(false);
				this.gameObject.renderer.enabled = true;
			}

			if(x > 0){
				if(z > 0){
					transform.eulerAngles = new Vector3(0, 45, 0);
				}
				if(z == 0){
					transform.eulerAngles = new Vector3(0, 90, 0);
				}
				if(z < 0){
					transform.eulerAngles = new Vector3(0, 135, 0);
				}
				
			}
			if(x == 0){
				if(z > 0){
					transform.eulerAngles = new Vector3(0, 0, 0);
				}
				if(z < 0){
					transform.eulerAngles = new Vector3(0, 180, 0);
				}
			}
			if(x < 0){
				if(z > 0){
					transform.eulerAngles = new Vector3(0, 315, 0);
				}
				if(z == 0){
					transform.eulerAngles = new Vector3(0, 270, 0);
				}
				if(z < 0){
					transform.eulerAngles = new Vector3(0, 225, 0);
				}
			}
		}
		if(iAmCrab){
			float x = Input.GetAxis (axis) * speed * Time.deltaTime;
			float z = Input.GetAxis (axis2) * speed * Time.deltaTime;
			transform.Translate (x, 0, 0, Space.Self);
			transform.Rotate(0, z*10f, 0, Space.Self);
            Transform obj = transform.GetChild(0);
            obj.animation.Stop();
            obj.gameObject.SetActive(false);
            this.gameObject.renderer.enabled = true;
		}
		if (Input.GetAxis ("Reset") < 0) {
			openDoor.restart();
			Application.LoadLevel(Application.loadedLevel);
		}
	}
	
	void OnTriggerEnter(Collider other){
		if (other.tag == "tr2") {
			iAmHuman = false;
			iAmCrab = true;
			multiplyer = 1;
		}
        if (other.tag == "tr1" || other.tag == "tr3")
        {
            if (other.tag == "tr1" )
            {
				iAmHuman = false;
			}
			else {
				iAmHuman = true;
			}
			iAmCrab = false;
			multiplyer = 1;
		}
		if (other.tag == "tr4") {
			iAmHuman = false;
			iAmCrab = false;
			multiplyer = 0;
		}

        if (other.tag == "tr5")
        {
            string tempAxis = player1_controls.axis;

            player1_controls.axis = player2_controls.axis;
            player2_controls.axis = tempAxis;            

        }

	}
}

