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

public class PlayerController : MonoBehaviour {
	public float speed = 1f;

	public string playerId;

	private float targetAngle;

	// Use this for initialization
	void Start () {
	}
	
	// Update is called once per frame
	void Update () {
		var moveDir = Vector3.zero;

		moveDir += Input.GetAxisRaw("Horizontal" + this.playerId) * Vector3.right;
		moveDir += Input.GetAxisRaw("Vertical" + this.playerId) * Vector3.up;

		moveDir.Normalize ();

		moveDir *= Time.deltaTime * this.speed;

		this.transform.position += moveDir;

		if (moveDir.sqrMagnitude > 0.0001) {
			this.targetAngle = Helper.AngleBetween (Vector2.up, moveDir);
		}

		this.transform.rotation = Quaternion.Euler(0f, 0f, Mathf.MoveTowardsAngle(this.transform.rotation.eulerAngles.z, this.targetAngle, 20f));

		if (Input.GetButtonDown ("Reset") == true) {
			Scene loadedLevel = SceneManager.GetActiveScene ();
			SceneManager.LoadScene (loadedLevel.buildIndex);
		}
	}

	void OnTriggerEnter2D(Collider2D other) {
	}
}
