From bc7a8dfdff9bc9892ebeb04450e716313155b2ee Mon Sep 17 00:00:00 2001 From: ineslelin Date: Sat, 22 Jun 2024 12:58:51 +0200 Subject: [PATCH] finished interact(), put down idea for getting closest entity --- include/InteractionComponent.h | 4 ++-- src/InteractionComponent.cpp | 37 +++++++++++++++++++++++++--------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/include/InteractionComponent.h b/include/InteractionComponent.h index ba07004..09b10f6 100644 --- a/include/InteractionComponent.h +++ b/include/InteractionComponent.h @@ -14,8 +14,8 @@ public: void init() override; void update() override; - Entity* getClosestInteractableEntity(/*last direction key input*/ std::vector entities); - bool interact(Entity* interactee); + Entity* getClosestInteractableEntity(Key directionKey, Entity* interactor, std::vector entities); + bool interact(Entity* interactor, Entity* interactee); private: bool canInteract; diff --git a/src/InteractionComponent.cpp b/src/InteractionComponent.cpp index 62a83bb..07b71fc 100644 --- a/src/InteractionComponent.cpp +++ b/src/InteractionComponent.cpp @@ -18,29 +18,48 @@ void InteractionComponent::update() } -Entity* getClosestInteractableEntity(/*last direction key input*/ std::vector entities) +Entity* getClosestInteractableEntity(Key directionKey, Entity* interactor, std::vector entities) { - // code to compare and find closest entity + for(auto e : entities) + { + if(!e->hasComponent() || !e->getComponent().isInteractable) + { + auto it = std::remove(entities.begin(), entities.end(), e); + entities.erase(it, entities.end()); + } + } + if(entities.empty()) + { + return nullptr; + } + for(auto e : entities) + { + if(e->getComponent().position.x) + } } -bool InteractionComponent::interact(Entity* interactee) +bool InteractionComponent::interact(Entity* interactor, Entity* interactee) { - if(!interactee->hasComponent || !interactee->getComponent.isInteractable) + if(!interactor->hasComponent() || !interactor->getComponent().canInteract) + { + throw std::logic_error("Interactor entity cannot interact"); + } + + if(!interactee->hasComponent() || !interactee->getComponent().isInteractable) { return false; } - return true; - // code to interact, basically + return true; } // TODO: -// - find a way to determine which entities are interactible (bool, map?) -// - get the last saved key input => if two interactible entities, favour the one closer to last input/m -// maybe only make the one in direction of last input interactible +// - get the last saved key input => if two interactible entities, favour the one closer to last input/ +// maybe only make the one in direction of last input interactible, or if two entities are ex. left and right and the last +// input was down, then either try to calc the closer one or if theyre the same distance away, favour one side automatically // - add toggleable marker to show last direction => like stardew? (probably only possible once tmx + layers properly implemented) // - add key input e for interact/maybe alt gr for other person?