46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using UnityEngine;
|
|
using System;
|
|
|
|
namespace OnlyScove.Scripts
|
|
{
|
|
public class Health : MonoBehaviour
|
|
{
|
|
[SerializeField] private float maxHealth = 100f;
|
|
[SerializeField] private float currentHealth;
|
|
|
|
public event Action<float> OnHealthChanged;
|
|
public event Action OnDeath;
|
|
|
|
public float CurrentHealth => currentHealth;
|
|
public float MaxHealth => maxHealth;
|
|
|
|
private void Awake()
|
|
{
|
|
currentHealth = maxHealth;
|
|
}
|
|
|
|
public void Heal(float amount)
|
|
{
|
|
if (currentHealth <= 0) return;
|
|
|
|
currentHealth = Mathf.Min(currentHealth + amount, maxHealth);
|
|
OnHealthChanged?.Invoke(currentHealth);
|
|
Debug.Log($"[Health] Healed for {amount}. Current health: {currentHealth}");
|
|
}
|
|
|
|
public void TakeDamage(float amount)
|
|
{
|
|
if (currentHealth <= 0) return;
|
|
|
|
currentHealth = Mathf.Max(currentHealth - amount, 0);
|
|
OnHealthChanged?.Invoke(currentHealth);
|
|
Debug.Log($"[Health] Took {amount} damage. Current health: {currentHealth}");
|
|
|
|
if (currentHealth <= 0)
|
|
{
|
|
OnDeath?.Invoke();
|
|
Debug.Log("[Health] Died.");
|
|
}
|
|
}
|
|
}
|
|
} |