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

public class Controller : MonoBehaviour {

	public Animator anim;
	public float speed;
	public float rotation;
	public float rotateSpeed;
	public Rigidbody rb;
	public GameObject target;
	public Quaternion newRotation = Quaternion.identity;


	// Use this for initialization
	void Start () {
		rb = this.gameObject.GetComponent<Rigidbody> ();
	}
	
	// Update is called once per frame
	void Update () {

		if (Input.GetKeyDown (KeyCode.A)) {
			rotation = 0f;
		}
			
		if (Input.GetKeyDown (KeyCode.D)) {
			rotation = 180f;
		}

		if (Input.GetKeyDown (KeyCode.W)) {
			rotation = 90f;
		}

		if(Input.GetKeyDown(KeyCode.S)){
			rotation = -90f;
		}

		newRotation = Quaternion.Euler (0, rotation, 0);

		Rotate ();
		if (Input.GetKey (KeyCode.S) || Input.GetKey (KeyCode.A) || Input.GetKey (KeyCode.D) || Input.GetKey (KeyCode.W)) {
			Move ();
		} else {
			anim.SetInteger ("State", 0);
		}
			

	}

	void Rotate(){

		transform.rotation = Quaternion.RotateTowards (transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
	}

	void Move(){
		anim.SetInteger ("State", 1);
		transform.position = Vector3.MoveTowards (transform.position, target.transform.position, speed * Time.deltaTime);
	}
}
