TMXPathFinding for Cocos2d-x 3.x
TMXPathFinding knows how to find path into TMX map.
It uses TMX tiled map created by http://www.mapeditor.org It supports orthogonal, hexagonal and isometric tile maps. During Path finding, it takes current GID values from TMXLayers.
Features:
Limitations:
How to use?
You have to pass TMXTiledMap and TMXPathFinding::DIRECTION to create TMXPathFinding object
TMXTiledMap tileMap = TMXTiledMap::create("level_1.tmx");
TMXPathFinding *pathFind = new TMXPathFinding(tileMap, TMXPathFinding::DIRECTION::FOUR);
Note : For Hexagonal you must pass DIRECTION::SIX
For Orthogonal & Isometric DIRECTION::FOUR or DIRECTION::EIGHT
You can set desired Layers list, [Note: If you don't call this, it will check for all layers in given TMXTiledMap]
pathFind->setTileLayers({"Layer_1", "Layer_2", "Layer_3"});
If you want to reset tile layers, means algo should check for all layers
pathFind->setTileLayers( { } );
To get path by using Walkable list
Vec2 startPos(5, 5);
Vec2 goalPos(10, 10);
std::vector
To get path by using Obstacle list
std::vector
Note : TILE_ROAD, TILE_GRASS, TILE_WALL, TILE_WATER will be GID as per your tileset define in TMX map.
Remeber GID starts with 1 not 0.
Once you got path
if (path.size() == 0)
log("No Path is available...");
else {
for (Vec2 tilePos : path)
log("x: %f y: %f", tilePos.x, tilePos.y);
}
Once you finish Delete path finding object
delete pathFind;