Add door interaction system with state management and event publishing
This commit is contained in:
26
GameCore/Interaction/RequiresItemRequirement.cs
Normal file
26
GameCore/Interaction/RequiresItemRequirement.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using GameCore.ECS;
|
||||
using GameCore.Events;
|
||||
using GameCore.Interaction.Interfaces;
|
||||
using GameCore.Inventory;
|
||||
|
||||
namespace GameCore.Interaction;
|
||||
|
||||
public class RequiresItemRequirement(string itemId, int quantity, bool consumeItem) : IInteractionRequirement
|
||||
{
|
||||
public bool IsMet(Entity interactor, World world)
|
||||
{
|
||||
var inventory = world.GetComponent<InventoryComponent>(interactor);
|
||||
return inventory != null && inventory.HasItem(itemId, quantity);
|
||||
}
|
||||
|
||||
public void ApplySideEffects(Entity interactor, World world)
|
||||
{
|
||||
if (!consumeItem) return;
|
||||
|
||||
var inventory = world.GetComponent<InventoryComponent>(interactor);
|
||||
if (inventory == null || !inventory.RemoveItem(itemId, quantity)) return;
|
||||
|
||||
var newQuantity = inventory.GetItemCount(itemId);
|
||||
world.PublishEvent(new InventoryItemChangedEvent(interactor, itemId, newQuantity));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user