using UnityEngine;

public enum EnemyState
{
    Idle,
    Hurt,
    Telegraph,
    Attack
}

public class EnemyAnimation : MonoBehaviour 
{
    public Sprite Idle1;
    public Sprite Idle2;
    public Sprite Hurt;
    public Sprite Telegraph;
    public Sprite Attack;
    public float IdleTime = 0.35f;

    private EnemyState state;
    private SpriteRenderer rend;
    private Rigidbody2D rb;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        rend = GetComponent<SpriteRenderer>();
        state = EnemyState.Idle;
    }

    public void ChangeAnimationState(EnemyState s)
    {
        state = s;
    }

    void Update()
    {
        switch(state)
        {
            case EnemyState.Attack:
                rend.sprite = Attack;
                break;
            case EnemyState.Hurt:
                rend.sprite = Hurt;
                break;
            case EnemyState.Telegraph:
                rend.sprite = Telegraph;
                break;
            case EnemyState.Idle:
                if((rb != null && rb.velocity.magnitude < 0.01f) || Mathf.RoundToInt(Time.time / IdleTime) % 2 == 0)
                    rend.sprite = Idle1;
                else
                    rend.sprite = Idle2;
                break;
        }
    }
}
