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

public class Health : MonoBehaviour
{
    public UnityEvent death;
    public UnityEvent hit;
    public float maxHealth;

    private float _health;
    private bool _dead = false;
    void Start()
    {
        _health = maxHealth;
        death.AddListener(OnDeath);
        var player = GameObject.FindGameObjectWithTag("Player");
        if(player != gameObject)
            death.AddListener(player.GetComponent<PlayerUltimate>().AddPoint);
    }

    private void OnDeath()
    {
        print("died");
    }

    public void Damage(int dmg){
        if(_dead) return;
        _health -= dmg;
        hit.Invoke();
        if(_health <= 0){
            _health = 0;
            death.Invoke();
            _dead = true;
        }
    }

    public float GetCurrentHealth(){
        return _health;
    }
}
