Implement inventory system with item management and pickup functionality
This commit is contained in:
36
GameCore/Inventory/PickupSystem.cs
Normal file
36
GameCore/Inventory/PickupSystem.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using GameCore.Combat;
|
||||
using GameCore.ECS;
|
||||
using GameCore.ECS.Interfaces;
|
||||
using GameCore.Events;
|
||||
using GameCore.Physics;
|
||||
using GameCore.Player;
|
||||
|
||||
namespace GameCore.Inventory;
|
||||
|
||||
public class PickupSystem : ISystem
|
||||
{
|
||||
public void Update(World world, float deltaTime)
|
||||
{
|
||||
var entitiesWithCollisions = world.GetEntitiesWith<CollisionEventComponent>();
|
||||
var withCollisions = entitiesWithCollisions.ToList();
|
||||
foreach (var entity in withCollisions)
|
||||
{
|
||||
if (world.GetComponent<PlayerComponent>(entity) == null) return;
|
||||
|
||||
var collision = world.GetComponent<CollisionEventComponent>(entity);
|
||||
if (collision == null) continue;
|
||||
|
||||
var pickup = world.GetComponent<PickupComponent>(entity);
|
||||
var inventory = world.GetComponent<InventoryComponent>(entity);
|
||||
|
||||
if (pickup == null || inventory == null) continue;
|
||||
|
||||
var item = new Item(pickup.ItemId, pickup.Quantity);
|
||||
// In the future, handle IsInstantUse items here
|
||||
world.PublishEvent(new AddItemToInventoryEvent(entity, item));
|
||||
world.AddComponent(collision.OtherEntity, new DeathComponent());
|
||||
}
|
||||
|
||||
foreach (var entity in withCollisions) world.RemoveComponent<CollisionEventComponent>(entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user