33 lines
822 B
C#
33 lines
822 B
C#
|
using UnityEngine;
|
||
|
using UnityEngine.Events;
|
||
|
using System;
|
||
|
|
||
|
public class HealthComponent : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField] float startHealth = 100;
|
||
|
|
||
|
public float currentHealth { get; private set; }
|
||
|
|
||
|
public static event Action<Vector3, float> OnHealthChangeAtPos;
|
||
|
|
||
|
public UnityEvent OnHealthZero;
|
||
|
public UnityEvent<float, float> OnHealthChange;
|
||
|
|
||
|
void Awake()
|
||
|
{
|
||
|
currentHealth = startHealth;
|
||
|
}
|
||
|
|
||
|
public void TakeDamage(float damage)
|
||
|
{
|
||
|
OnHealthChange?.Invoke(currentHealth, currentHealth - damage);
|
||
|
OnHealthChangeAtPos?.Invoke(transform.position, currentHealth - damage);
|
||
|
currentHealth -= damage;
|
||
|
|
||
|
currentHealth = (int) Mathf.Clamp(currentHealth, 0f, Mathf.Infinity);
|
||
|
if (currentHealth == 0)
|
||
|
OnHealthZero?.Invoke();
|
||
|
}
|
||
|
}
|
||
|
|