using UnityEngine;
using ScrambleKit.FX;

public class EatHuman : MonoBehaviour 
{
    public FXTrigger EatFX;
    public float EatDuration = 4f;

    private MonsterAI monster;
    private bool eating = false;

    void Start()
    {
        monster = GetComponent<MonsterAI>();
    }

    void OnCollisionEnter2D(Collision2D c)
    {
        if (eating || Exit.ExitClosed || c.transform.GetComponent<Human>() == null)
            return;

        EatFX.Trigger(transform);

        HumanControl.RemoveHuman(c.transform);
        monster.Eat(c.transform);
        Score.HumansEaten++;
        eating = true;
        Invoke("FinishEating", EatDuration);
        Destroy(c.gameObject);
    }

    void FinishEating()
    {
        eating = false;
        monster.FinishEating();
    }

    void OnDestroy()
    {
        CancelInvoke();
    }
}
