diff --git a/src/Rule.cpp b/src/Rule.cpp index 87f97c7..f29612b 100644 --- a/src/Rule.cpp +++ b/src/Rule.cpp @@ -13,22 +13,26 @@ Rule::Rule(std::shared_ptr entity, State targetState, EntityType targetT targetType(targetType), entities(entities), occupiedSquares(occupiedSquares), - mapSize(mapSize) + mapSize(mapSize), + previousPosition(this->entity->getPosition()), + previousTarget(this->entity->target) {} void Rule::update() { if (targetState == State::Moving || targetState == State::Fleeing) { - entity->nextTarget = findTarget(); - if(entity->nextTarget == entity->getPosition()) + pro_maat::GridPos target = findTarget(); + if(target != previousPosition || entity->getPosition() != previousTarget) { - entity->nextState = State::Idle; + entity->nextTarget = target; + entity->nextState = targetState; } else { - entity->nextState = targetState; + entity->nextState = State::Idle; } + entity->update(); } else if (targetState == State::Waiting) @@ -43,6 +47,9 @@ void Rule::update() entity->nextState = State::Idle; entity->update(); } + + previousPosition = entity->getPosition(); + previousTarget = entity->getTarget(); } pro_maat::GridPos Rule::findTarget() diff --git a/src/Rule.h b/src/Rule.h index 97e0228..ae52f63 100644 --- a/src/Rule.h +++ b/src/Rule.h @@ -43,13 +43,16 @@ private: std::shared_ptr entity; - State targetState; - EntityType targetType; + const State targetState; + const EntityType targetType; const std::vector>& entities; const std::vector& occupiedSquares; const pro_maat::GridPos& mapSize; + + pro_maat::GridPos previousPosition; + pro_maat::GridPos previousTarget; }; #endif //PROJECT_MAAT_RULES_H