This commit is contained in:
2026-11-04 15:51:04 +01:00
parent d21f0c9b6c
commit cd65f702ae
111 changed files with 246848 additions and 1021 deletions

View File

@@ -3,81 +3,85 @@ using UnityEngine;
namespace Arena.Combat
{
/// <summary>
/// Hit points of an entity. Serialized inside the component that owns it (Player, Enemy),
/// which calls Initialize in its Awake.
/// </summary>
[System.Serializable]
public class Health
{
#region Fields
/// <summary>
/// Health points of an entity. Serialized inside the component that owns it (Player, Destructible),
/// which calls Initialize in its Awake.
/// </summary>
[System.Serializable]
public class Health
{
#region FIELDS
[Tooltip("Hit points when full.")]
[Min(1)] public int Max = 100;
[Tooltip("Health points when full.")]
[Min(1)] public int Max = 100;
[Tooltip("Current hit points, visible for debugging.")]
[SerializeField] private int _current;
[Tooltip("Current health points, visible for debugging.")]
[SerializeField] private int _current;
/// <summary>
/// Raised with the current and max hit points each time they change.
/// </summary>
public event Action<int, int> OnChange;
/// <summary>
/// Raised with the current and max health points each time they change.
/// </summary>
public event Action<int, int> OnChange;
/// <summary>
/// Raised once, when the hit points reach zero.
/// </summary>
public event Action OnDied;
/// <summary>
/// Raised once, when the health points reach zero.
/// </summary>
public event Action OnDied;
public int Current => _current;
public bool IsDead => _current <= 0;
public int Current => _current;
public bool IsDead => _current <= 0;
#endregion
#endregion
#region Public Methods
#region PUBLIC METHODS
/// <summary>
/// Fills the hit points. Called by the owner in its Awake.
/// </summary>
public void Initialize()
{
SetCurrent(Max);
}
/// <summary>
/// Fills the health points. Called by the owner in its Awake.
/// </summary>
public void Initialize()
{
SetCurrent(Max);
}
/// <summary>
/// Removes hit points and raises OnDied the first time they reach zero.
/// </summary>
public void TakeDamage(int amount)
{
if (IsDead || amount <= 0)
return;
/// <summary>
/// Removes health points and raises OnDied the first time they reach zero.
/// Returns the health points actually removed.
/// </summary>
public int TakeDamage(DamageInfo damage)
{
if (IsDead || damage.Amount <= 0)
return 0;
SetCurrent(_current - amount);
int previous = _current;
SetCurrent(_current - damage.Amount);
if (IsDead)
OnDied?.Invoke();
}
if (IsDead)
OnDied?.Invoke();
/// <summary>
/// Gives hit points back without exceeding Max. Has no effect once dead.
/// </summary>
public void Heal(int amount)
{
if (IsDead || amount <= 0)
return;
return previous - _current;
}
SetCurrent(_current + amount);
}
/// <summary>
/// Gives health points back without exceeding Max. Has no effect once dead.
/// </summary>
public void Heal(int amount)
{
if (IsDead || amount <= 0)
return;
#endregion
SetCurrent(_current + amount);
}
#region Private Methods
#endregion
private void SetCurrent(int value)
{
_current = Mathf.Clamp(value, 0, Max);
OnChange?.Invoke(_current, Max);
}
#region PRIVATE METHODS
#endregion
}
private void SetCurrent(int value)
{
_current = Mathf.Clamp(value, 0, Max);
OnChange?.Invoke(_current, Max);
}
#endregion
}
}