Add attribute system with core stats and gameplay components
This commit is contained in:
38
GameCore/Combat/DamageSystem.cs
Normal file
38
GameCore/Combat/DamageSystem.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user