Fixed oscillating around end position

This commit is contained in:
trotFunky 2019-06-10 19:14:43 +02:00
parent 5ccd7d0c13
commit e1f23f67a2
2 changed files with 17 additions and 7 deletions

View file

@ -13,22 +13,26 @@ Rule::Rule(std::shared_ptr<Entity> entity, State targetState, EntityType targetT
targetType(targetType), targetType(targetType),
entities(entities), entities(entities),
occupiedSquares(occupiedSquares), occupiedSquares(occupiedSquares),
mapSize(mapSize) mapSize(mapSize),
previousPosition(this->entity->getPosition()),
previousTarget(this->entity->target)
{} {}
void Rule::update() void Rule::update()
{ {
if (targetState == State::Moving || targetState == State::Fleeing) if (targetState == State::Moving || targetState == State::Fleeing)
{ {
entity->nextTarget = findTarget(); pro_maat::GridPos target = findTarget();
if(entity->nextTarget == entity->getPosition()) if(target != previousPosition || entity->getPosition() != previousTarget)
{ {
entity->nextState = State::Idle; entity->nextTarget = target;
entity->nextState = targetState;
} }
else else
{ {
entity->nextState = targetState; entity->nextState = State::Idle;
} }
entity->update(); entity->update();
} }
else if (targetState == State::Waiting) else if (targetState == State::Waiting)
@ -43,6 +47,9 @@ void Rule::update()
entity->nextState = State::Idle; entity->nextState = State::Idle;
entity->update(); entity->update();
} }
previousPosition = entity->getPosition();
previousTarget = entity->getTarget();
} }
pro_maat::GridPos Rule::findTarget() pro_maat::GridPos Rule::findTarget()

View file

@ -43,13 +43,16 @@ private:
std::shared_ptr<Entity> entity; std::shared_ptr<Entity> entity;
State targetState; const State targetState;
EntityType targetType; const EntityType targetType;
const std::vector<std::shared_ptr<Entity>>& entities; const std::vector<std::shared_ptr<Entity>>& entities;
const std::vector<pro_maat::GridPos>& occupiedSquares; const std::vector<pro_maat::GridPos>& occupiedSquares;
const pro_maat::GridPos& mapSize; const pro_maat::GridPos& mapSize;
pro_maat::GridPos previousPosition;
pro_maat::GridPos previousTarget;
}; };
#endif //PROJECT_MAAT_RULES_H #endif //PROJECT_MAAT_RULES_H