Add healing effects and systems with event handling

This commit is contained in:
2025-10-29 02:27:41 +01:00
parent d6a31b12e3
commit 341173c53f
4 changed files with 81 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
using GameCore.Combat.Interfaces;
using GameCore.Events;
namespace GameCore.Combat.Effects;
public class HealEffect(float amount) : IEffect
{
public void Execute(EffectContext context)
{
if (context.Target == null) return;
context.World.PublishEvent(new HealDealtEvent(context.Target.Value, amount));
}
}

View File

@@ -0,0 +1,46 @@
using GameCore.Attributes;
using GameCore.ECS;
using GameCore.ECS.Interfaces;
using GameCore.Events;
using Attribute = GameCore.Attributes.Attribute;
namespace GameCore.Combat;
public class HealingSystem : ISystem
{
private readonly World _world;
public HealingSystem(World world)
{
_world = world;
_world.Subscribe<HealDealtEvent>(OnHealDealt);
}
public void Update(World world, float deltaTime)
{
}
private void OnHealDealt(HealDealtEvent e)
{
var attributes = _world.GetComponent<AttributeComponent>(e.Target);
if (attributes == null) return;
var currentHealth = attributes.GetValue(Attribute.Health);
var maxHealth = attributes.GetValue(Attribute.MaxHealth);
if (currentHealth >= maxHealth) return;
var amountToHeal = e.Amount;
var newHealth = currentHealth + amountToHeal;
if (newHealth > maxHealth)
{
amountToHeal = maxHealth - newHealth;
newHealth = maxHealth;
}
attributes.ModifyValue(Attribute.Health, amountToHeal);
_world.PublishEvent(new EntityHealedEvent(e.Target, newHealth, amountToHeal));
}
}

View File

@@ -0,0 +1,11 @@
using GameCore.ECS;
using GameCore.Events.Interfaces;
namespace GameCore.Events;
public readonly struct EntityHealedEvent(Entity target, float newHealth, float amountHealed) : IEvent
{
public readonly Entity Target = target;
public readonly float NewHealth = newHealth;
public readonly float AmountHealed = amountHealed;
}

View File

@@ -0,0 +1,10 @@
using GameCore.ECS;
using GameCore.Events.Interfaces;
namespace GameCore.Events;
public readonly struct HealDealtEvent(Entity target, float amount) : IEvent
{
public readonly Entity Target = target;
public readonly float Amount = amount;
}