using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour {

	public CharacterController controller;
	public float speed = 1;
	public bool p1;
	bool pressedHorizontal;
	bool pressedVertical;
	public List<Vector3> pos;

	void Update () {
		Vector3 tp = transform.position;
		transform.position = new Vector3 (Mathf.Clamp(tp.x, -19, 19), 1, Mathf.Clamp(tp.z, -19, 19));

		if (p1) {
			//controller.Move (new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")) * Time.deltaTime * speed);

			if (Input.GetAxisRaw ("Horizontal") != 0) {
				Debug.Log((transform.position + new Vector3 (Input.GetAxisRaw ("Horizontal") * 2, 0, 0)).ToString());
				if (!pressedHorizontal && transform.position + new Vector3 (Input.GetAxisRaw ("Horizontal") * 2, 0, 0) != GameObject.Find("Player2").transform.position) {
					transform.position += new Vector3 (Input.GetAxisRaw ("Horizontal") * 2 * speed, 1, 0);
					pressedHorizontal = true;
					pos.Add (transform.position - Vector3.up);
					Vector3 toAdd = new Vector3 (transform.position.x, 0.05f, transform.position.z);
					pos.Add (toAdd);
					GameManager.lastWave.p1Ghost.transform.LookAt(new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical")) + GameManager.lastWave.p1Ghost.transform.position);
					GameManager.lastWave.p1Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
				}
			} else {
				pressedHorizontal = false;
			} 
			if (Input.GetAxisRaw ("Vertical") != 0) {
				Debug.Log((transform.position + new Vector3 (Input.GetAxisRaw ("Vertical") * 2, 0, 0)).ToString());
				if (!pressedVertical && transform.position + new Vector3 (0, 0, Input.GetAxisRaw ("Vertical") * 2 * speed) != GameObject.Find("Player2").transform.position) {
					transform.position += new Vector3 (0, 0, Input.GetAxisRaw ("Vertical") * 2);
					pressedVertical = true;
					Vector3 toAdd = new Vector3 (transform.position.x, 0.05f, transform.position.z);
					pos.Add (toAdd);
					GameManager.lastWave.p1Ghost.transform.LookAt(new Vector3 (Input.GetAxisRaw ("Horizontal"), 0, Input.GetAxisRaw ("Vertical")) + GameManager.lastWave.p1Ghost.transform.position);
					GameManager.lastWave.p1Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
				}
			} else {
				pressedVertical = false;
			} 

		} else {
			//controller.Move (new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical")) * Time.deltaTime * speed);
			
			if (Input.GetAxisRaw ("2Horizontal") != 0) {
				Debug.Log((transform.position + new Vector3 (Input.GetAxisRaw ("2Horizontal") * 2, 0, 0)).ToString());
				if (!pressedHorizontal && transform.position + new Vector3 (Input.GetAxisRaw ("2Horizontal") * 2 * speed, 0, 0) != GameObject.Find("Player1").transform.position) {
					transform.position += new Vector3 (Input.GetAxisRaw ("2Horizontal") * 2, 1, 0);
					pressedHorizontal = true;
					Vector3 toAdd = new Vector3 (transform.position.x, 0.05f, transform.position.z);
					pos.Add (toAdd);
					GameManager.lastWave.p2Ghost.transform.LookAt(new Vector3 (Input.GetAxisRaw ("2Horizontal"), 0, Input.GetAxisRaw ("2Vertical")) + GameManager.lastWave.p2Ghost.transform.position);
					GameManager.lastWave.p2Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
				}
			} else {
				pressedHorizontal = false;
			} 
			if (Input.GetAxisRaw ("2Vertical") != 0) {
				Debug.Log((transform.position + new Vector3 (0, Input.GetAxisRaw ("2Vertical") * 2, 0)).ToString());
				if (!pressedVertical && transform.position + new Vector3 (0, 0, Input.GetAxisRaw ("2Vertical") * 2 * speed) != GameObject.Find("Player1").transform.position) {
					transform.position += new Vector3 (0, 0, Input.GetAxisRaw ("2Vertical") * 2);
					pressedVertical = true;
					Vector3 toAdd = new Vector3 (transform.position.x, 0.05f, transform.position.z);
					pos.Add (toAdd);
					GameManager.lastWave.p2Ghost.transform.LookAt(new Vector3 (Input.GetAxisRaw ("2Horizontal"), 0, Input.GetAxisRaw ("2Vertical")) + GameManager.lastWave.p2Ghost.transform.position);
					GameManager.lastWave.p2Ghost.transform.rotation *= Quaternion.Euler (-90, 0, 90);
				}
			} else {
				pressedVertical = false;
			} 
		}
	}
}
