Files
2D-Plattformer-Example/Assets/Scripts/Mechanics/Health.cs
T
2026-04-30 00:53:30 +02:00

61 lines
1.5 KiB
C#

using System;
using Platformer.Gameplay;
using UnityEngine;
using static Platformer.Core.Simulation;
namespace Platformer.Mechanics
{
/// <summary>
/// Represebts the current vital statistics of some game entity.
/// </summary>
public class Health : MonoBehaviour
{
/// <summary>
/// The maximum hit points for the entity.
/// </summary>
public int maxHP = 1;
/// <summary>
/// Indicates if the entity should be considered 'alive'.
/// </summary>
public bool IsAlive => currentHP > 0;
int currentHP;
/// <summary>
/// Increment the HP of the entity.
/// </summary>
public void Increment()
{
currentHP = Mathf.Clamp(currentHP + 1, 0, maxHP);
}
/// <summary>
/// Decrement the HP of the entity. Will trigger a HealthIsZero event when
/// current HP reaches 0.
/// </summary>
public void Decrement()
{
currentHP = Mathf.Clamp(currentHP - 1, 0, maxHP);
if (currentHP == 0)
{
var ev = Schedule<HealthIsZero>();
ev.health = this;
}
}
/// <summary>
/// Decrement the HP of the entitiy until HP reaches 0.
/// </summary>
public void Die()
{
while (currentHP > 0) Decrement();
}
void Awake()
{
currentHP = maxHP;
}
}
}