using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class AllCharMovement : MonoBehaviour {

Vector3 pos;                                // For movement
float speed = 2.0f;                         // Speed of movement
float angle;
float lastMove=0f;
public bool canMoveWithKey=true;
public Vector3 lastPos;
public bool safeCollided=false;

public Animator anim;

void Awake () {

		//Physics.IgnoreLayerCollision (8,8);
	anim= gameObject.GetComponent<Animator> ();
	pos = transform.position;          // Take the initial position
		lastPos=pos;
}

void FixedUpdate () {



		if (Input.GetKey (KeyCode.F10))
			SceneManager.LoadScene (SceneManager.GetActiveScene().name);

		if(canMoveWithKey && !safeCollided)
		{
			

		if (Input.GetKey (KeyCode.A) && transform.position == pos) {        // Left
				AudioHandler.playSoundRandom(4,7);

				lastMove = Time.timeSinceLevelLoad;
				if(transform.position.x-lastPos.x>0.5f || transform.position.y-lastPos.y>0.5f )
					lastPos = transform.position;
			if (pos.x > -7)
				pos += Vector3.left;
			RotateLeft ();
			if (anim != null)
				anim.SetBool ("walking", true);


		}
		if (Input.GetKey (KeyCode.D) && transform.position == pos) {        // Right
				AudioHandler.playSoundRandom(12,15);
				lastMove = Time.timeSinceLevelLoad;
				if(transform.position.x-lastPos.x>0.5f || transform.position.y-lastPos.y>0.5f )
					lastPos = transform.position;
			if (pos.x < 8)
				pos += Vector3.right;
			RotateRight ();
			if (anim != null)
				anim.SetBool ("walking", true);

		}
		if (Input.GetKey (KeyCode.W) && transform.position == pos) {        // Up
				AudioHandler.playSoundRandom(0,3);
				lastMove = Time.timeSinceLevelLoad;
				if(transform.position.x-lastPos.x>0.5f || transform.position.y-lastPos.y>0.5f )
					lastPos = transform.position;
			if (pos.y < 4)
				pos += Vector3.up;
			RotateUp ();
			if (anim != null)
				anim.SetBool ("walking", true);

		}
		if (Input.GetKey (KeyCode.S) && transform.position == pos) {        // Down
				
				AudioHandler.playSoundRandom(8,11);
				//Debug.Log("S");
				lastMove = Time.timeSinceLevelLoad;
				if(transform.position.x-lastPos.x>0.5f || transform.position.y-lastPos.y>0.5f )
					lastPos = transform.position;
			if (pos.y > -4)
				pos += Vector3.down;
			RotateDown ();
			if (anim != null)
				anim.SetBool ("walking", true);

		}

	}
		if (Input.GetKey (KeyCode.A) ||Input.GetKey (KeyCode.W) ||Input.GetKey (KeyCode.S) ||Input.GetKey (KeyCode.D))
			lastMove = Time.timeSinceLevelLoad;

	
		if (Time.timeSinceLevelLoad - lastMove > 0.5f && canMoveWithKey && safeCollided) {
			pos = lastPos;
			transform.position = lastPos;
			//Debug.Log ("Position Restored");
			safeCollided = false;

		}

		if (Time.timeSinceLevelLoad - lastMove > 0.2f && anim != null) {
			anim.SetBool ("walking",false);

		}
	transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);    // 

			
	}

	void RotateLeft () {
		transform.rotation= Quaternion.AngleAxis(90,Vector3.forward);
	}
	void RotateRight () {
		transform.rotation= Quaternion.AngleAxis(-90,Vector3.forward);
	}

	void RotateUp () {
		transform.rotation= Quaternion.AngleAxis(0,Vector3.forward);
	}

	void RotateDown () {
		transform.rotation= Quaternion.AngleAxis(180,Vector3.forward);
	}


}

