using ScrambleKit.FX;
using ScrambleKit.Hits;
using UnityEngine;

public class ChomperHead : MonoBehaviour 
{
    public int HP = 3;
    public FXTrigger fxTrigger;

    private ChomperBody body;

    void Start()
    {
        body = transform.parent.GetComponent<ChomperBody>();
    }

    void TakeHit(HitInfo hit)
    {
        if (HP <= 0)
            return;

        HitFX(hit.Direction);
        HP = Mathf.Max(0, HP - hit.Damage);
        if (HP <= 0)
            Invoke("Die", hit.StunTime);
    }

    private void Die()
    {
        body.LostHead();
        transform.SendMessage("Dying", SendMessageOptions.DontRequireReceiver);
        Destroy(gameObject);
    }

    private void HitFX(Vector2 dir)
    {
        fxTrigger.Trigger(transform);        
    }
}
