31 lines
675 B
C#
31 lines
675 B
C#
|
using UnityEngine;
|
||
|
|
||
|
public class Health : MonoBehaviour
|
||
|
{
|
||
|
public float maxHealth;
|
||
|
public System.Action OnDeath;
|
||
|
|
||
|
private float _currentHealth;
|
||
|
|
||
|
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||
|
private void Start()
|
||
|
{
|
||
|
_currentHealth = maxHealth;
|
||
|
}
|
||
|
|
||
|
public void DealDamage(float damage)
|
||
|
{
|
||
|
_currentHealth -= damage;
|
||
|
if (_currentHealth <= 0)
|
||
|
{
|
||
|
Debug.Log($"{gameObject.name} is dead!");
|
||
|
OnDeath();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void HealHealth(float health)
|
||
|
{
|
||
|
_currentHealth = Mathf.Min(_currentHealth + health, maxHealth);
|
||
|
}
|
||
|
}
|