Files
brick-framework/GameCore/Combat/WeaponSystem.cs

50 lines
1.8 KiB
C#

using GameCore.Combat.Effects;
using GameCore.ECS;
using GameCore.ECS.Interfaces;
using GameCore.Events;
using GameCore.Input;
using GameCore.Movement;
using GameCore.Physics;
namespace GameCore.Combat;
public class WeaponSystem : ISystem
{
public void Update(World world, float deltaTime)
{
var entities = world.GetEntitiesWith<InputStateComponent>();
foreach (var entity in entities)
{
var input = world.GetComponent<InputStateComponent>(entity);
var weapon = world.GetComponent<WeaponComponent>(entity);
var position = world.GetComponent<PositionComponent>(entity);
var rotation = world.GetComponent<RotationComponent>(entity);
if (input == null || weapon == null || position == null || rotation == null)
continue;
if (weapon.CooldownTimer > 0f) weapon.CooldownTimer -= deltaTime;
if (input.IsFiring && weapon.CooldownTimer <= 0f)
{
var context = new EffectContext { World = world, Owner = entity };
var canFire = true;
foreach (var cost in weapon.FireCosts)
if (!cost.CanAfford(context))
{
canFire = false;
//TODO: Publish an event or notify the player they can't fire
break;
}
if (!canFire) continue;
foreach (var cost in weapon.FireCosts) cost.Execute(context);
foreach (var effect in weapon.OnFireEffects) effect.Execute(context);
world.PublishEvent(new WeaponFiredEvent(entity, input.MuzzlePosition));
weapon.CooldownTimer = 1f / weapon.FireRate;
}
}
}
}