using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

	[SerializeField] protected Animator grimAnim;

	private Transform playerLoc;
	private Rigidbody playerBody;

	public void Awake()
	{
		playerLoc = this.GetComponent<Transform> ();
		playerBody = this.GetComponent<Rigidbody> ();
	}

	// Recieve input and handle it appropriately
	void Update () {
		if (Input.GetKeyDown (KeyCode.Space)) {
			grimAnim.SetTrigger ("killing");
		}

		if (Input.GetKey (KeyCode.DownArrow) || Input.GetKey (KeyCode.S)) {
			//make animation go down
			playerBody.velocity = new Vector3 (0, 0, -1f);
		} else if (Input.GetKey (KeyCode.UpArrow) || Input.GetKey (KeyCode.W)) {
			//make animation go up
			playerBody.velocity = new Vector3 (0, 0, 1f);
		} else if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) {
			//make animation go left
			playerBody.velocity = new Vector3 (-1f, 0, 0);
		} else if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) {
			//make animation go right
			playerBody.velocity = new Vector3 (1f, 0, 0);
		} 
		else {
			playerBody.velocity = new Vector3 (0, 0, 0);
			if(!grimAnim.GetBool("idle"))
				grimAnim.SetBool ("idle", true);
		}

		if (Input.GetKeyDown (KeyCode.DownArrow) || Input.GetKeyDown (KeyCode.S)) {
			grimAnim.SetBool ("idle", false);
			if (grimAnim.GetInteger ("vertical") <= 0) {
				grimAnim.SetInteger ("vertical", 1);
			}
		}
		if (Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.W)) {
			grimAnim.SetBool ("idle", false);
			if (grimAnim.GetInteger ("vertical") >= 0) {
				grimAnim.SetInteger ("vertical", -1);
			}
		}
		if (Input.GetKeyDown (KeyCode.LeftArrow) || Input.GetKeyDown (KeyCode.A)) {
			grimAnim.SetBool ("idle", false);
			if (grimAnim.GetInteger ("horizontal") >= 0) {
				grimAnim.SetInteger ("horizontal", -1);
			}
		}
		if (Input.GetKeyDown (KeyCode.RightArrow) || Input.GetKeyDown (KeyCode.D)) {
			grimAnim.SetBool ("idle", false);
			if (grimAnim.GetInteger ("horizontal") <= 0) {
				grimAnim.SetInteger ("horizontal", 1);
			}
		}
	}
}
