Add healing effects and systems with event handling
This commit is contained in:
14
GameCore/Combat/Effects/HealEffect.cs
Normal file
14
GameCore/Combat/Effects/HealEffect.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
46
GameCore/Combat/HealingSystem.cs
Normal file
46
GameCore/Combat/HealingSystem.cs
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
||||||
11
GameCore/Events/EntityHealedEvent.cs
Normal file
11
GameCore/Events/EntityHealedEvent.cs
Normal 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;
|
||||||
|
}
|
||||||
10
GameCore/Events/HealDealtEvent.cs
Normal file
10
GameCore/Events/HealDealtEvent.cs
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user