Fixed pathfinding for usage with Rule::findTarget()

Fixed Entity::target being public
This commit is contained in:
trotFunky 2019-06-10 01:58:14 +02:00
parent 08e922b0d4
commit 4ab83f1124
4 changed files with 23 additions and 16 deletions

View file

@ -79,6 +79,11 @@ State Entity::getState() const
return currentState;
}
pro_maat::GridPos Entity::getTarget() const
{
return target;
}
const pro_maat::GridPos Entity::getPosition() const
{
// Safe : size is a multiple of pro_maat::pixelsPerUnit

View file

@ -51,6 +51,7 @@ public:
virtual void update();
virtual State getState() const;
virtual pro_maat::GridPos getTarget() const;
virtual const sf::RectangleShape& getShape() const;
/// Returns the grid coordinates at the center of the entity
@ -59,15 +60,12 @@ public:
// Don't like it : iterates over every square every tick
virtual const std::vector<pro_maat::GridPos> getOccupiedSquares() const;
// FIXME : Replace with getter
/// Position of the target of the current action on the map
pro_maat::GridPos target;
protected:
/// Empty constructor for derived class instanciation
Entity();
private:
static const std::map<std::string,EntityType> entityTypeLookup;
@ -81,6 +79,8 @@ private:
/// Used with rules : last to update has priority
State nextState;
/// Target position on the map of the current action
pro_maat::GridPos target;
/// Used with rules : last to update has priority
pro_maat::GridPos nextTarget;

View file

@ -57,9 +57,9 @@ void Level::runStep()
{
heuristicSign = -1;
}
if(entity.target != entity.getPosition())
if(entity.getTarget() != entity.getPosition())
{
entity.move(findPath(entity.getPosition(),entity.target,heuristicSign));
entity.move(findPath(entity.getPosition(),entity.getTarget(),heuristicSign));
}
break;
}
@ -93,14 +93,6 @@ Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int
std::map<pro_maat::GridPos,pro_maat::GridPos> paths{};
// TODO : Should be OK with raycasting to find goal
// FIXME : Just get to goal (Might break)
const std::vector<pro_maat::GridPos> goalNeighbours = pro_maat::getNeighbours(goal,size);
// Save the iterators : vector is const and .begin() and .end() might get called a lot
auto goalNeighboursBeginIterator = goalNeighbours.begin();
auto goalNeighboursEndIterator = goalNeighbours.end();
// FIXME : Find an efficient way to get rid of openNodes.find calls
// Lambda checking if the current element is also in the open nodes set
auto compWithOpen = [&openNodes](const std::pair<pro_maat::GridPos,float>& leftHandSide,
@ -125,7 +117,7 @@ Orientation Level::findPath(pro_maat::GridPos start, pro_maat::GridPos goal, int
// Expand from the open node with the smallest estimated cost
pro_maat::GridPos currentNode = std::min_element(estimatedCosts.begin(),estimatedCosts.end(),compWithOpen)->first;
if(std::find(goalNeighboursBeginIterator,goalNeighboursEndIterator,currentNode) != goalNeighboursEndIterator)
if(currentNode == goal)
{
if(currentNode == start)
{

View file

@ -13,17 +13,21 @@
/// Decorates entities with rules which will modify its behaviour
template <State targetState, EntityType targetType>
class Rule : public Entity
class Rule : private Entity
{
public:
Rule(Entity& entity, const std::vector<Entity>& entities,
const std::vector<pro_maat::GridPos>& occupiedSquares, const pro_maat::GridPos& mapSize);
/// Update according to the targetState and targetType
void update() override;
// Simply delegate the following function calls to the original entity
void move(Orientation orientation) override;
State getState() const override;
const sf::RectangleShape& getShape() const override;
pro_maat::GridPos getTarget() const override;
const pro_maat::GridPos getPosition() const override;
const std::vector<pro_maat::GridPos> getOccupiedSquares() const override;
@ -188,6 +192,12 @@ const sf::RectangleShape& Rule<targetState, targetType>::getShape() const
return entity.getShape();
}
template<State targetState, EntityType targetType>
pro_maat::GridPos Rule<targetState, targetType>::getTarget() const
{
return entity.getTarget();
}
template<State targetState, EntityType targetType>
const pro_maat::GridPos Rule<targetState, targetType>::getPosition() const
{