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

public class Health : MonoBehaviour
{
    public static System.Action<float> OnHealthChanged;
    public static System.Action<float> OnDied;

    public float Value;
    public float Max;
    public bool DestroyOnDeath = true;

    public void SetValue(float value)
    {
        if (value == Value) return;

        Value = value;
        OnHealthChanged?.Invoke(Value);
    }

    public void Hit(float damage)
    {
        SetValue(Value - damage);

        if (Value > 0) return;

        Die();
    }

    private void Die()
    {
        OnDied?.Invoke(Value);

        if (DestroyOnDeath)
            Destroy(gameObject);
    }
}
