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(OnDamageDealt); } public void Update(World world, float deltaTime) { var entitiesWithHealth = world.GetEntitiesWith(); foreach (var entity in entitiesWithHealth) { if (world.GetComponent(entity) != null) continue; var attributes = world.GetComponent(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(e.Target); targetAttributes?.ModifyValue(Attribute.Health, -e.Amount); var newHealth = targetAttributes?.GetValue(Attribute.Health) ?? 0f; _world.PublishEvent(new EntityDamagedEvent(e.Target, newHealth, e.Amount)); } }