41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using GameCore.Attributes;
|
|
using GameCore.ECS;
|
|
using GameCore.ECS.Interfaces;
|
|
using GameCore.Events;
|
|
using Attribute = GameCore.Attributes.Attribute;
|
|
|
|
namespace GameCore.Combat;
|
|
|
|
public class DamageSystem : ISystem
|
|
{
|
|
private readonly World _world;
|
|
|
|
public DamageSystem(World world)
|
|
{
|
|
_world = world;
|
|
world.Subscribe<DamageDealtEvent>(OnDamageDealt);
|
|
}
|
|
|
|
public void Update(World world, float deltaTime)
|
|
{
|
|
var entitiesWithHealth = world.GetEntitiesWith<AttributeComponent>();
|
|
foreach (var entity in entitiesWithHealth)
|
|
{
|
|
if (world.GetComponent<DeathComponent>(entity) != null) continue;
|
|
|
|
var attributes = world.GetComponent<AttributeComponent>(entity);
|
|
if (attributes == null) continue;
|
|
|
|
if (attributes.GetValue(Attribute.Health) <= 0) world.AddComponent(entity, new DeathComponent());
|
|
}
|
|
}
|
|
|
|
private void OnDamageDealt(DamageDealtEvent e)
|
|
{
|
|
var targetAttributes = _world.GetComponent<AttributeComponent>(e.Target);
|
|
targetAttributes?.ModifyValue(Attribute.Health, -e.Amount);
|
|
|
|
var newHealth = targetAttributes?.GetValue(Attribute.Health) ?? 0f;
|
|
_world.PublishEvent(new EntityDamagedEvent(e.Target, newHealth, e.Amount));
|
|
}
|
|
} |