basic healthbars for enemies

This commit is contained in:
2026-01-23 13:31:11 +01:00
parent 8b2bf3e32e
commit 4d419b9010
13 changed files with 139 additions and 18 deletions

View File

@@ -16,7 +16,7 @@ public partial class Enemy : CharacterBody3D,
{
// Signals and events
public event Action<IDamageable, DamageRecord> DamageTaken;
public event Action<IHealthable, float> HealthChanged;
public event Action<IHealthable, HealthChangedRecord> HealthChanged;
public event Action<IHealthable> HealthDepleted;
// Public export components
@@ -54,6 +54,7 @@ public partial class Enemy : CharacterBody3D,
// Private stuff
private Area3D _damageBox;
private Node3D _target;
private CHealthbar _healthbar;
public override void _Ready()
{
@@ -75,6 +76,8 @@ public partial class Enemy : CharacterBody3D,
if (CHealth is null) GD.PrintErr("This node needs a 'CHealth' child of type IHealthable!");
if (CKnockback is null) GD.PrintErr("This node needs a 'CKnockback' child of type IKnockbackable!");
_healthbar = GetNode<CHealthbar>("CHealthBar");
if (RMovement != null) CMovement!.RMovement = RMovement;
if (RHealth != null)
{
@@ -86,9 +89,11 @@ public partial class Enemy : CharacterBody3D,
public void SetupSignals()
{
CDamageable.DamageTaken += ReduceHealth;
// Anonymous function call to erase return values of ReduceHealth
CDamageable.DamageTaken += (source, record) => ReduceHealth(source, record);
CDamageable.DamageTaken += RegisterKnockback;
CHealth.HealthDepleted += Kill;
HealthChanged += (source, record) => _healthbar.OnHealthChanged(record);
}
public override void _PhysicsProcess(double delta)
@@ -147,11 +152,12 @@ public partial class Enemy : CharacterBody3D,
return CDamageable.ComputeDamage(damageRecord);
}
public void ReduceHealth(IDamageable source, DamageRecord damageRecord)
public HealthChangedRecord ReduceHealth(IDamageable source, DamageRecord damageRecord)
{
if (CHealth is null) return;
CHealth.ReduceHealth(source, damageRecord);
HealthChanged?.Invoke(this, CHealth.CurrentHealth);
if (CHealth is null) return new HealthChangedRecord(0, 0, 0);
var record = CHealth.ReduceHealth(source, damageRecord);
HealthChanged?.Invoke(this, record);
return record;
}
public void Kill(IHealthable source)