Add ammo consumption and item pickup systems
This commit is contained in:
19
GameCore/Combat/Effects/ConsumeAmmoCost.cs
Normal file
19
GameCore/Combat/Effects/ConsumeAmmoCost.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using GameCore.Combat.Interfaces;
|
||||
using GameCore.Inventory;
|
||||
|
||||
namespace GameCore.Combat.Effects;
|
||||
|
||||
public class ConsumeAmmoCost(string ammoId, int amount) : ICostEffect
|
||||
{
|
||||
public void Execute(EffectContext context)
|
||||
{
|
||||
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
||||
inventory?.RemoveItem(ammoId, amount);
|
||||
}
|
||||
|
||||
public bool CanAfford(EffectContext context)
|
||||
{
|
||||
var inventory = context.World.GetComponent<InventoryComponent>(context.Owner);
|
||||
return inventory != null && inventory.HasItem(ammoId, amount);
|
||||
}
|
||||
}
|
||||
8
GameCore/Combat/Interfaces/ICostEffect.cs
Normal file
8
GameCore/Combat/Interfaces/ICostEffect.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using GameCore.Combat.Effects;
|
||||
|
||||
namespace GameCore.Combat.Interfaces;
|
||||
|
||||
public interface ICostEffect : IEffect
|
||||
{
|
||||
bool CanAfford(EffectContext context);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ public class WeaponComponent : IComponent
|
||||
{
|
||||
public float FireRate { get; set; } = 1f;
|
||||
|
||||
public List<ICostEffect> FireCosts { get; set; } = [];
|
||||
public List<IEffect> OnFireEffects { get; set; } = [];
|
||||
public List<IEffect> OnHitEffects { get; set; } = [];
|
||||
|
||||
|
||||
@@ -27,12 +27,21 @@ public class WeaponSystem : ISystem
|
||||
|
||||
if (input.IsFiring && weapon.CooldownTimer <= 0f)
|
||||
{
|
||||
// Check for ammo if applicable
|
||||
|
||||
var context = new EffectContext { World = world, Owner = entity };
|
||||
|
||||
foreach (var effect in weapon.OnFireEffects) effect.Execute(context);
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user