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),
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()

View file

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