Files
MovementTests/components/health/CHealth.cs
2026-01-23 13:31:11 +01:00

36 lines
958 B
C#

using Godot;
using System;
using Movementtests.interfaces;
[GlobalClass]
public partial class CHealth : Node, IHealthable
{
public event Action<IHealthable, HealthChangedRecord> HealthChanged;
public event Action<IHealthable> HealthDepleted;
[Export]
public RHealth RHealth { get; set; }
public float CurrentHealth { get; set; }
public override void _Ready()
{
CurrentHealth = RHealth.StartingHealth;
}
public HealthChangedRecord ReduceHealth(IDamageable source, DamageRecord damageRecord)
{
var previousHealth = CurrentHealth;
CurrentHealth -= damageRecord.Damage.DamageDealt;
var record = new HealthChangedRecord(CurrentHealth, previousHealth, RHealth.StartingHealth);
HealthChanged?.Invoke(this, record);
if (CurrentHealth <= 0)
{
CurrentHealth = 0;
HealthDepleted?.Invoke(this);
}
return record;
}
}