36 lines
958 B
C#
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;
|
|
}
|
|
}
|