2013-04-28 16:11:39 +00:00
|
|
|
/*
|
|
|
|
* Pathfinder.h
|
|
|
|
*
|
|
|
|
* Created on: 28.04.2013
|
|
|
|
* Author: Felix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DG_PATHFINDER_H_
|
|
|
|
#define DG_PATHFINDER_H_
|
|
|
|
|
|
|
|
#include <SFML/System.hpp>
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
|
2013-08-07 15:39:43 +00:00
|
|
|
#include "util/Vector.h"
|
|
|
|
|
2013-06-21 15:40:39 +00:00
|
|
|
/**
|
|
|
|
* Used to find paths between points in the world.
|
|
|
|
*
|
|
|
|
* For this, a representation of all walkable areas in the world is
|
|
|
|
* saved, which is then used to run A* on.
|
|
|
|
*/
|
|
|
|
class Pathfinder : public sf::Drawable {
|
2013-04-28 16:11:39 +00:00
|
|
|
private:
|
|
|
|
struct Area;
|
|
|
|
struct Portal;
|
|
|
|
|
|
|
|
public:
|
2013-06-21 15:40:39 +00:00
|
|
|
void insertArea(const sf::FloatRect& rect);
|
2013-04-28 16:11:39 +00:00
|
|
|
void generatePortals();
|
2013-08-07 15:39:43 +00:00
|
|
|
std::vector<Vector2f> getPath(const Vector2f& start,
|
|
|
|
const Vector2f& end, float radius) const;
|
2013-04-28 16:11:39 +00:00
|
|
|
|
|
|
|
private:
|
2013-08-07 15:39:43 +00:00
|
|
|
Area* getArea(const Vector2f& point) const;
|
2013-04-28 16:11:39 +00:00
|
|
|
std::vector<Portal*> astarArea(Area* start, Area* end) const;
|
2013-06-21 15:40:39 +00:00
|
|
|
void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
2013-04-28 16:11:39 +00:00
|
|
|
|
|
|
|
private:
|
2013-09-01 20:32:07 +00:00
|
|
|
/// character radius multiplied with this gives movement distance
|
|
|
|
// from the nearest wall.
|
|
|
|
static constexpr float WALL_DISTANCE_MULTIPLIER = 1.5f;
|
|
|
|
//< This has to be a vector as objects are compared by address.
|
|
|
|
std::vector<Area> mAreas;
|
2013-04-28 16:11:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edges
|
|
|
|
*
|
|
|
|
* Redundant data as portals are saved twice.
|
|
|
|
*/
|
|
|
|
struct Pathfinder::Portal {
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f start;
|
|
|
|
Vector2f end;
|
2013-04-28 16:11:39 +00:00
|
|
|
Area* area;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Nodes
|
|
|
|
*/
|
|
|
|
struct Pathfinder::Area {
|
2013-06-21 15:40:39 +00:00
|
|
|
sf::FloatRect area;
|
2013-08-07 15:39:43 +00:00
|
|
|
Vector2f center;
|
2013-04-28 16:11:39 +00:00
|
|
|
std::vector<Portal> portals;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* DG_PATHFINDER_H_ */
|