Add ammo consumption and item pickup systems
This commit is contained in:
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user