using UnityEngine;
using System.Collections;
using UnityEngine.UI;

[RequireComponent(typeof(Animator))]
public class PlayerInput : MonoBehaviour
{
    [Range(0.5f, 10.0f)]
    public float MaxVelocity = 2.0f;
    private Animator _animator;

    // Use this for initialization
    void Start()
    {
        _animator = GetComponentInChildren<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (!GameStateController.Instance.AcceptPlayerInput)
        {
            return;
        }

        var horizontal = Input.GetAxis("Horizontal");
        var vertical = Input.GetAxis("Vertical");

        rigidbody2D.velocity = new Vector2(horizontal * MaxVelocity, vertical * MaxVelocity);

        if (rigidbody2D.velocity.magnitude > 0.05)
        {
            if (Mathf.Abs(vertical) >= Mathf.Abs(horizontal))
            {
                _animator.SetInteger("Direction", (vertical > 0 ? 1 : 3));
            }
            else
            {
                _animator.SetInteger("Direction", (horizontal > 0 ? 4 : 2));
            }
        }

        _animator.SetFloat("Velocity", rigidbody2D.velocity.magnitude);
    }
}
