From 4ab83f11245141883edddd12a8e775f785421c84 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 10 Jun 2019 01:58:14 +0200 Subject: [PATCH] Fixed pathfinding for usage with Rule::findTarget() Fixed Entity::target being public --- src/Entity.cpp | 5 +++++ src/Entity.h | 8 ++++---- src/Level.cpp | 14 +++----------- src/Rule.h | 12 +++++++++++- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Entity.cpp b/src/Entity.cpp index 81026a4..987eaa3 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -79,6 +79,11 @@ State Entity::getState() const return currentState; } +pro_maat::GridPos Entity::getTarget() const +{ + return target; +} + const pro_maat::GridPos Entity::getPosition() const { // Safe : size is a multiple of pro_maat::pixelsPerUnit diff --git a/src/Entity.h b/src/Entity.h index a0cf0ac..081f956 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -51,6 +51,7 @@ public: virtual void update(); virtual State getState() const; + virtual pro_maat::GridPos getTarget() const; virtual const sf::RectangleShape& getShape() const; /// Returns the grid coordinates at the center of the entity @@ -59,15 +60,12 @@ public: // Don't like it : iterates over every square every tick virtual const std::vector getOccupiedSquares() const; - // FIXME : Replace with getter - /// Position of the target of the current action on the map - pro_maat::GridPos target; protected: - /// Empty constructor for derived class instanciation Entity(); + private: static const std::map entityTypeLookup; @@ -81,6 +79,8 @@ private: /// Used with rules : last to update has priority State nextState; + /// Target position on the map of the current action + pro_maat::GridPos target; /// Used with rules : last to update has priority pro_maat::GridPos nextTarget; diff --git a/src/Level.cpp b/src/Level.cpp index 2653ad5..2e4a2cb 100644 --- a/src/Level.cpp +++ b/src/Level.cpp @@ -57,9 +57,9 @@ void Level::runStep() { heuristicSign = -1; } - if(entity.target != entity.getPosition()) + if(entity.getTarget() != entity.getPosition()) { - entity.move(findPath(entity.getPosition(),entity.target,heuristicSign)); + entity.move(findPath(entity.getPosition(),entity.getTarget(),heuristicSign)); } break; } @@ -93,14 +93,6 @@ Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int std::map paths{}; - // TODO : Should be OK with raycasting to find goal - // FIXME : Just get to goal (Might break) - const std::vector goalNeighbours = pro_maat::getNeighbours(goal,size); - // Save the iterators : vector is const and .begin() and .end() might get called a lot - auto goalNeighboursBeginIterator = goalNeighbours.begin(); - auto goalNeighboursEndIterator = goalNeighbours.end(); - - // FIXME : Find an efficient way to get rid of openNodes.find calls // Lambda checking if the current element is also in the open nodes set auto compWithOpen = [&openNodes](const std::pair& leftHandSide, @@ -125,7 +117,7 @@ Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int // Expand from the open node with the smallest estimated cost pro_maat::GridPos currentNode = std::min_element(estimatedCosts.begin(),estimatedCosts.end(),compWithOpen)->first; - if(std::find(goalNeighboursBeginIterator,goalNeighboursEndIterator,currentNode) != goalNeighboursEndIterator) + if(currentNode == goal) { if(currentNode == start) { diff --git a/src/Rule.h b/src/Rule.h index 3cb0e4e..fae04f9 100644 --- a/src/Rule.h +++ b/src/Rule.h @@ -13,17 +13,21 @@ /// Decorates entities with rules which will modify its behaviour template -class Rule : public Entity +class Rule : private Entity { public: Rule(Entity& entity, const std::vector& entities, const std::vector& occupiedSquares, const pro_maat::GridPos& mapSize); + /// Update according to the targetState and targetType void update() override; + // Simply delegate the following function calls to the original entity + void move(Orientation orientation) override; State getState() const override; const sf::RectangleShape& getShape() const override; + pro_maat::GridPos getTarget() const override; const pro_maat::GridPos getPosition() const override; const std::vector getOccupiedSquares() const override; @@ -188,6 +192,12 @@ const sf::RectangleShape& Rule::getShape() const return entity.getShape(); } +template +pro_maat::GridPos Rule::getTarget() const +{ + return entity.getTarget(); +} + template const pro_maat::GridPos Rule::getPosition() const {