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