53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Code.Gameplay.Core;
|
|
|
|
namespace Code.Gameplay.Character.Stats
|
|
{
|
|
public class HealthSystem : MonoBehaviour, IDamageable
|
|
{
|
|
[SerializeField] private float maxHealth = 100f;
|
|
[SerializeField] private float currentHealth;
|
|
|
|
public event Action<float, float> OnHealthChanged;
|
|
public event Action OnDied;
|
|
|
|
public float CurrentHealth => currentHealth;
|
|
public float MaxHealth => maxHealth;
|
|
public float HealthPercentage => currentHealth / maxHealth;
|
|
public bool IsAlive => currentHealth > 0;
|
|
|
|
private void Awake()
|
|
{
|
|
currentHealth = maxHealth;
|
|
}
|
|
|
|
public void TakeDamage(float damage)
|
|
{
|
|
if (!IsAlive) return;
|
|
|
|
currentHealth = Mathf.Max(0, currentHealth - damage);
|
|
OnHealthChanged?.Invoke(currentHealth, maxHealth);
|
|
|
|
if (currentHealth <= 0)
|
|
{
|
|
Die();
|
|
}
|
|
}
|
|
|
|
public void Heal(float amount)
|
|
{
|
|
if (!IsAlive) return;
|
|
|
|
currentHealth = Mathf.Min(maxHealth, currentHealth + amount);
|
|
OnHealthChanged?.Invoke(currentHealth, maxHealth);
|
|
}
|
|
|
|
private void Die()
|
|
{
|
|
OnDied?.Invoke();
|
|
}
|
|
|
|
public Transform GetTransform() => transform;
|
|
}
|
|
} |