From 2d8b1e53ce4983fd13d4f27f4a10d6348dbb8d14 Mon Sep 17 00:00:00 2001 From: trotFunky Date: Mon, 10 Jun 2019 21:53:39 +0200 Subject: [PATCH] Final fix for end of movement I was being dumb again : if we are adjacent to a target entity, stop moving --- src/Rule.cpp | 16 +++++++++------- src/Rule.h | 3 --- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Rule.cpp b/src/Rule.cpp index 15ce9e9..adc9737 100644 --- a/src/Rule.cpp +++ b/src/Rule.cpp @@ -13,9 +13,7 @@ Rule::Rule(std::shared_ptr entity, State targetState, EntityType targetT targetType(targetType), entities(entities), occupiedSquares(occupiedSquares), - mapSize(mapSize), - previousPosition(this->entity->getPosition()), - previousTarget(this->entity->target) + mapSize(mapSize) {} void Rule::update() @@ -23,13 +21,10 @@ void Rule::update() if (targetState == State::Moving || targetState == State::Fleeing) { pro_maat::GridPos target = findTarget(); - if(target != previousPosition && (entity->getPosition() != previousTarget || entity->getPosition() == previousPosition)) + if(target != entity->getPosition()) { entity->nextTarget = target; entity->nextState = targetState; - - previousPosition = entity->getPosition(); - previousTarget = entity->getTarget(); } else { @@ -130,6 +125,13 @@ pro_maat::GridPos Rule::findTarget() std::sort(potentialTargets.begin(),potentialTargets.end(),distanceSortSquares); + + // If we are adjacent to a target, we do not need to move + if(std::find(potentialTargets.begin(),potentialTargets.end(),entity->getPosition()) != potentialTargets.end()) + { + break; + } + auto target = std::min_element(potentialTargets.begin(),potentialTargets.end(),bestTarget); if(target != potentialTargets.end()) diff --git a/src/Rule.h b/src/Rule.h index ae52f63..bb36c50 100644 --- a/src/Rule.h +++ b/src/Rule.h @@ -50,9 +50,6 @@ private: const std::vector& occupiedSquares; const pro_maat::GridPos& mapSize; - - pro_maat::GridPos previousPosition; - pro_maat::GridPos previousTarget; }; #endif //PROJECT_MAAT_RULES_H