Hi all,
I'm having some trouble working out how to handle entities which can be interacted with by other entities (typically the player).
A couple of examples I'm trying to implement first are:
- Doors
- Item containers (e.g. chest)
InputSystem
-----------
OnKeyDown(key)
if key == "interact"
interactableEntity = FindCollidingInteractable(player) // check all nearby entities in scene with an "Interactable" component?
interactionComp = interactableEntity.Get<InteractionComponent>();
// What now? How can interactionComp hold all data for all possible interactions?
If it's a door, we need to know if it's locked and what the required key is if so, as well as which room we teleport to once opened (Resident Evil style).
If it's a chest, it might have similar "lock" requirements as the door, as well as an inventory (InventoryComponent?) of items to transfer to the player.
I can image how this would work using interfaces & polymorphism:
class InteractionHandler {
void HandleInteraction(Entity instigator, Entity target);
}
class DoorInteractionHandler : InteractionHandler {
void HandleInteraction(Entity instigator, Entity target) {
if (requiredItem != null && instigator.Get<InventoryComponent>().HasItem(requiredItem)) {
instigator.Get<InventoryComponent>().Remove(requiredItem);
requiredItem = null;
LoadStage(nextStage);
} else {
// Door is not locked.
LoadStage(nextStage);
}
}
string requiredItem;
string nextStage;
} Where the InteractionComponent could hold a pointer to an instance of InteractionHandler and just call HandleInteraction blindly.
I understand however that components are not meant to contain any logic in a pure ECS, so how would we represent the interaction requirements on the entity?
I thought that maybe I need more components, such as a LockComponent, DoorComponent etc, but then how would this fit in with the keyboard handling code and where would the logic for acting on these go?
Very confused, so any advice appreciated.
Thanks!