using UnityEngine;
using ScrambleKit.FX;

namespace ScrambleKit.Hits
{
    public class HitPoints : MonoBehaviour
    {
        public int MaxHP = 3;
        public int HP;

        public FXTrigger fxTrigger;

        void Start()
        {
            HP = MaxHP;
        }

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

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

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

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