Fixed pathfinding for usage with Rule::findTarget()
Fixed Entity::target being public
This commit is contained in:
parent
08e922b0d4
commit
4ab83f1124
4 changed files with 23 additions and 16 deletions
|
@ -79,6 +79,11 @@ State Entity::getState() const
|
||||||
return currentState;
|
return currentState;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pro_maat::GridPos Entity::getTarget() const
|
||||||
|
{
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
const pro_maat::GridPos Entity::getPosition() const
|
const pro_maat::GridPos Entity::getPosition() const
|
||||||
{
|
{
|
||||||
// Safe : size is a multiple of pro_maat::pixelsPerUnit
|
// Safe : size is a multiple of pro_maat::pixelsPerUnit
|
||||||
|
|
|
@ -51,6 +51,7 @@ public:
|
||||||
virtual void update();
|
virtual void update();
|
||||||
|
|
||||||
virtual State getState() const;
|
virtual State getState() const;
|
||||||
|
virtual pro_maat::GridPos getTarget() const;
|
||||||
|
|
||||||
virtual const sf::RectangleShape& getShape() const;
|
virtual const sf::RectangleShape& getShape() const;
|
||||||
/// Returns the grid coordinates at the center of the entity
|
/// 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
|
// Don't like it : iterates over every square every tick
|
||||||
virtual const std::vector<pro_maat::GridPos> getOccupiedSquares() const;
|
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:
|
protected:
|
||||||
|
|
||||||
/// Empty constructor for derived class instanciation
|
/// Empty constructor for derived class instanciation
|
||||||
Entity();
|
Entity();
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const std::map<std::string,EntityType> entityTypeLookup;
|
static const std::map<std::string,EntityType> entityTypeLookup;
|
||||||
|
|
||||||
|
@ -81,6 +79,8 @@ private:
|
||||||
/// Used with rules : last to update has priority
|
/// Used with rules : last to update has priority
|
||||||
State nextState;
|
State nextState;
|
||||||
|
|
||||||
|
/// Target position on the map of the current action
|
||||||
|
pro_maat::GridPos target;
|
||||||
/// Used with rules : last to update has priority
|
/// Used with rules : last to update has priority
|
||||||
pro_maat::GridPos nextTarget;
|
pro_maat::GridPos nextTarget;
|
||||||
|
|
||||||
|
|
|
@ -57,9 +57,9 @@ void Level::runStep()
|
||||||
{
|
{
|
||||||
heuristicSign = -1;
|
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;
|
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{};
|
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
|
// 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
|
// Lambda checking if the current element is also in the open nodes set
|
||||||
auto compWithOpen = [&openNodes](const std::pair<pro_maat::GridPos,float>& leftHandSide,
|
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
|
// Expand from the open node with the smallest estimated cost
|
||||||
pro_maat::GridPos currentNode = std::min_element(estimatedCosts.begin(),estimatedCosts.end(),compWithOpen)->first;
|
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)
|
if(currentNode == start)
|
||||||
{
|
{
|
||||||
|
|
12
src/Rule.h
12
src/Rule.h
|
@ -13,17 +13,21 @@
|
||||||
|
|
||||||
/// Decorates entities with rules which will modify its behaviour
|
/// Decorates entities with rules which will modify its behaviour
|
||||||
template <State targetState, EntityType targetType>
|
template <State targetState, EntityType targetType>
|
||||||
class Rule : public Entity
|
class Rule : private Entity
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Rule(Entity& entity, const std::vector<Entity>& entities,
|
Rule(Entity& entity, const std::vector<Entity>& entities,
|
||||||
const std::vector<pro_maat::GridPos>& occupiedSquares, const pro_maat::GridPos& mapSize);
|
const std::vector<pro_maat::GridPos>& occupiedSquares, const pro_maat::GridPos& mapSize);
|
||||||
|
|
||||||
|
/// Update according to the targetState and targetType
|
||||||
void update() override;
|
void update() override;
|
||||||
|
|
||||||
|
// Simply delegate the following function calls to the original entity
|
||||||
|
|
||||||
void move(Orientation orientation) override;
|
void move(Orientation orientation) override;
|
||||||
State getState() const override;
|
State getState() const override;
|
||||||
const sf::RectangleShape& getShape() const override;
|
const sf::RectangleShape& getShape() const override;
|
||||||
|
pro_maat::GridPos getTarget() const override;
|
||||||
const pro_maat::GridPos getPosition() const override;
|
const pro_maat::GridPos getPosition() const override;
|
||||||
const std::vector<pro_maat::GridPos> getOccupiedSquares() 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();
|
return entity.getShape();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<State targetState, EntityType targetType>
|
||||||
|
pro_maat::GridPos Rule<targetState, targetType>::getTarget() const
|
||||||
|
{
|
||||||
|
return entity.getTarget();
|
||||||
|
}
|
||||||
|
|
||||||
template<State targetState, EntityType targetType>
|
template<State targetState, EntityType targetType>
|
||||||
const pro_maat::GridPos Rule<targetState, targetType>::getPosition() const
|
const pro_maat::GridPos Rule<targetState, targetType>::getPosition() const
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue