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 float FireRate { get; set; } = 1f;
|
||||||
|
|
||||||
|
public List<ICostEffect> FireCosts { get; set; } = [];
|
||||||
public List<IEffect> OnFireEffects { get; set; } = [];
|
public List<IEffect> OnFireEffects { get; set; } = [];
|
||||||
public List<IEffect> OnHitEffects { get; set; } = [];
|
public List<IEffect> OnHitEffects { get; set; } = [];
|
||||||
|
|
||||||
|
|||||||
@@ -27,12 +27,21 @@ public class WeaponSystem : ISystem
|
|||||||
|
|
||||||
if (input.IsFiring && weapon.CooldownTimer <= 0f)
|
if (input.IsFiring && weapon.CooldownTimer <= 0f)
|
||||||
{
|
{
|
||||||
// Check for ammo if applicable
|
|
||||||
|
|
||||||
var context = new EffectContext { World = world, Owner = entity };
|
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));
|
world.PublishEvent(new WeaponFiredEvent(entity, input.MuzzlePosition));
|
||||||
weapon.CooldownTimer = 1f / weapon.FireRate;
|
weapon.CooldownTimer = 1f / weapon.FireRate;
|
||||||
}
|
}
|
||||||
|
|||||||
17
GameCore/Events/ItemPickupAttemptEvent.cs
Normal file
17
GameCore/Events/ItemPickupAttemptEvent.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.Events.Interfaces;
|
||||||
|
|
||||||
|
namespace GameCore.Events;
|
||||||
|
|
||||||
|
public readonly struct ItemPickupAttemptEvent(
|
||||||
|
Entity picker,
|
||||||
|
Entity itemEntity,
|
||||||
|
string itemId,
|
||||||
|
int quantity
|
||||||
|
) : IEvent
|
||||||
|
{
|
||||||
|
public readonly Entity Picker = picker;
|
||||||
|
public readonly Entity ItemEntity = itemEntity;
|
||||||
|
public readonly string ItemId = itemId;
|
||||||
|
public readonly int Quantity = quantity;
|
||||||
|
}
|
||||||
47
GameCore/Inventory/ItemAcquisitionSystem.cs
Normal file
47
GameCore/Inventory/ItemAcquisitionSystem.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using GameCore.Combat;
|
||||||
|
using GameCore.Combat.Effects;
|
||||||
|
using GameCore.ECS;
|
||||||
|
using GameCore.ECS.Interfaces;
|
||||||
|
using GameCore.Events;
|
||||||
|
|
||||||
|
namespace GameCore.Inventory;
|
||||||
|
|
||||||
|
public class ItemAcquisitionSystem : ISystem
|
||||||
|
{
|
||||||
|
private readonly World _world;
|
||||||
|
|
||||||
|
public ItemAcquisitionSystem(World world)
|
||||||
|
{
|
||||||
|
_world = world;
|
||||||
|
_world.Subscribe<ItemPickupAttemptEvent>(OnItemPickupAttempt);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Update(World world, float deltaTime)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnItemPickupAttempt(ItemPickupAttemptEvent e)
|
||||||
|
{
|
||||||
|
var pickupComponent = _world.GetComponent<PickupComponent>(e.ItemEntity);
|
||||||
|
if (pickupComponent == null) return;
|
||||||
|
|
||||||
|
if (pickupComponent.IsInstantUse)
|
||||||
|
{
|
||||||
|
var context = new EffectContext
|
||||||
|
{
|
||||||
|
World = _world,
|
||||||
|
Owner = e.Picker,
|
||||||
|
Target = e.Picker
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var effect in pickupComponent.OnAcquireEffects) effect.Execute(context);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var item = new Item(e.ItemId, e.Quantity);
|
||||||
|
_world.PublishEvent(new AddItemToInventoryEvent(e.Picker, item));
|
||||||
|
}
|
||||||
|
|
||||||
|
_world.AddComponent(e.ItemEntity, new DeathComponent());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using GameCore.Combat.Interfaces;
|
||||||
using GameCore.ECS.Interfaces;
|
using GameCore.ECS.Interfaces;
|
||||||
|
|
||||||
namespace GameCore.Inventory;
|
namespace GameCore.Inventory;
|
||||||
@@ -7,4 +8,5 @@ public class PickupComponent : IComponent
|
|||||||
public string ItemId { get; set; }
|
public string ItemId { get; set; }
|
||||||
public int Quantity { get; set; } = 1;
|
public int Quantity { get; set; } = 1;
|
||||||
public bool IsInstantUse { get; set; } = false;
|
public bool IsInstantUse { get; set; } = false;
|
||||||
|
public List<IEffect> OnAcquireEffects { get; set; } = [];
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
using GameCore.Combat;
|
|
||||||
using GameCore.ECS;
|
using GameCore.ECS;
|
||||||
using GameCore.ECS.Interfaces;
|
using GameCore.ECS.Interfaces;
|
||||||
using GameCore.Events;
|
using GameCore.Events;
|
||||||
@@ -20,15 +19,17 @@ public class PickupSystem : ISystem
|
|||||||
var collision = world.GetComponent<CollisionEventComponent>(entity);
|
var collision = world.GetComponent<CollisionEventComponent>(entity);
|
||||||
if (collision == null) continue;
|
if (collision == null) continue;
|
||||||
|
|
||||||
var pickup = world.GetComponent<PickupComponent>(entity);
|
var pickup = world.GetComponent<PickupComponent>(collision.OtherEntity);
|
||||||
var inventory = world.GetComponent<InventoryComponent>(entity);
|
var inventory = world.GetComponent<InventoryComponent>(entity);
|
||||||
|
|
||||||
if (pickup == null || inventory == null) continue;
|
if (pickup == null || inventory == null) continue;
|
||||||
|
|
||||||
var item = new Item(pickup.ItemId, pickup.Quantity);
|
world.PublishEvent(new ItemPickupAttemptEvent(
|
||||||
// In the future, handle IsInstantUse items here
|
entity,
|
||||||
world.PublishEvent(new AddItemToInventoryEvent(entity, item));
|
collision.OtherEntity,
|
||||||
world.AddComponent(collision.OtherEntity, new DeathComponent());
|
pickup.ItemId,
|
||||||
|
pickup.Quantity
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (var entity in withCollisions) world.RemoveComponent<CollisionEventComponent>(entity);
|
foreach (var entity in withCollisions) world.RemoveComponent<CollisionEventComponent>(entity);
|
||||||
|
|||||||
Reference in New Issue
Block a user