Now that I've got my collision handling working (and components dependant on that) quite well, it's time to move to next problem.
How should I integrate pathfinding in to the book structure?
When and how should I update pathfinding?
How often should I update pathfinding for moving entities?
How does my pathfinding know what entities are placed on the map?
One idea I had was to set pathfinding nodes to my collisiongrid and check nodes to see if they collide with anything and change their characteristics based on that.
Example map:
Start and goal location for 2 entities
###################################
# start ooo end #
# x ooo x #
# ooo #
# ooo #
# ooo #
# ooo #
# ooo #
# ooo #
# ooo #
# ooo #
# oooooooooo #
# oooooooooo #
# oooooooooo #
# #
###################################
Entity 1 can dig slowly through (o) wall while Entity 2 cannot
###################################
# start ooo end #
# x 1________________________ x #
# 2 ooo / #
# | ooo / #
# | ooo / #
# | ooo / #
# | ooo / #
# | ooo / #
# | ooo / #
# | ooo / #
# |oooooooooo / #
# |oooooooooo / #
# |oooooooooo / #
# \ _________/ #
###################################
Pathfinding nodes and entity pathfinding should be updated so Entity 2 can see that shorter path has appeared
###################################
# start ooo end #
# x...../__1_________________ x #
# . |ooo #
# . / ooo #
# . / ooo #
# . / ooo #
# . / ooo #
# . / ooo #
# ./ ooo #
# / ooo #
# |oooooooooo #
# 2oooooooooo #
# oooooooooo #
# #
###################################
And my current collision detection
#include <CollisionGrid.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/System/Time.hpp>
#include <vector>
class Entity;
class CommandQueue;
class Collision
{
public:
typedef std::pair<Entity*, Entity*> Pair;
public:
Collision();
void update(sf::Time dt);
void updateCollisionGrid(CommandQueue& commands);
private:
void handleCollision(Pair colliders, sf::Vector2f offset, sf::Time dt);
private:
CollisionGrid mCollisionGrid;
std::vector<Entity*> mMovingEntities;
};
#include <SFML/System/Vector2.hpp>
#include <map>
#include <set>
#include <vector>
class Entity;
class Cell
{
public:
typedef std::set<Entity*> Container;
public:
void setNeighbours(Cell* left, Cell* right, Cell* up, Cell* down);
void addEntity(Entity& entity);
void removeEntity(Entity& entity);
Container getNearbyEntities();
void clear();
private:
void appendEntitiesTo(Container& entities) const;
private:
Container mEntities;
protected:
Cell* mLeft;
Cell* mRight;
Cell* mUp;
Cell* mDown;
};
class CollisionGrid
{
public:
CollisionGrid(float sceneWidth, float sceneHeight, float cellSize);
void addEntity(Entity& entity);
void removeEntity(Entity& entity);
Cell::Container getNearbyEntities(Entity& entity);
void clear();
private:
Cell& getCell(const sf::Vector2f position);
private:
std::vector<Cell> mCells;
int mCols;
int mRows;
float mSceneWidth;
float mSceneHeight;
float mCellSize;
};