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

public class P_Controller : MonoBehaviour
{

    public float speed;
    private Rigidbody2D body;
    public Animator animator;
    public SpriteRenderer playerSprite;

    // Start is called before the first frame update
    void Start()
    {
        body = GetComponent<Rigidbody2D>();
    }

    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis("Horizontal");
        float moveVertical = Input.GetAxis("Vertical");
        if (moveVertical > 0)
        {
            StartAnim("Walk Up");
        }
        else
        {
            if (moveVertical < 0)
            {
                StartAnim("Walk Down");
            }
            else
            {
                StartAnim("Walk Sideways");
            }
        }
        if (moveHorizontal > 0)
        {
            playerSprite.flipX = true;
        }
        else
        {
            playerSprite.flipX = false;
        }
        Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0);
  
        gameObject.transform.position = gameObject.transform.position + movement*speed;
    }

    private void StartAnim(string animName)
    {
        if (!(animator.GetCurrentAnimatorClipInfo(0)[0].clip.name == animName))
        {
            animator.enabled = false;
            animator.enabled = true;
        }
        animator.Play(animName);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
