update tùm lum tùm la
This commit is contained in:
46
Assets/Scripts/Attributes/Health.cs
Normal file
46
Assets/Scripts/Attributes/Health.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user