Skip to content

Instantly share code, notes, and snippets.

@ronjunevaldoz
Created August 29, 2016 01:24
Show Gist options
  • Save ronjunevaldoz/f4425d179e598f5c08d9f70c38d7eb9a to your computer and use it in GitHub Desktop.
Save ronjunevaldoz/f4425d179e598f5c08d9f70c38d7eb9a to your computer and use it in GitHub Desktop.
Libgdx Path-finding Test (Simple Tiled Map) - Not working
// Tiled Connection
public class TiledConnection extends DefaultConnection<TiledNode> {
public TiledConnection(TiledNode fromNode, TiledNode toNode) {
super(fromNode, toNode);
}
}
// Tiled Graph
public class TiledGraph extends DefaultGraphPath<TiledNode> implements IndexedGraph<TiledNode> {
public static int width;
public static int height;
public Array<Connection<TiledNode>> connections = new Array<Connection<TiledNode>>();
public TiledGraph(int width, int height) {
super(width * height);
this.width = width;
this.height = height;
for (int x = 0; x < width; x ++) {
for (int y = 0; y < height; y++) {
add(new TiledNode(x, y, connections));
}
}
}
@Override
public int getIndex(TiledNode node) {
return node.getIndex();
}
@Override
public int getNodeCount() {
return getCount();
}
@Override
public Array<Connection<TiledNode>> getConnections(TiledNode fromNode) {
return fromNode.connections;
}
}
// Heuristic
public class TiledManhattanDistance<N extends TiledNode> implements Heuristic<N> {
public TiledManhattanDistance () {
}
@Override
public float estimate (N node, N endNode) {
return Math.abs(endNode.x - node.x) + Math.abs(endNode.y - node.y);
}
}
// Tiled Node
public class TiledNode {
public boolean isWalkable = true;
public final int x;
public final int y;
public Array<Connection<TiledNode>> connections;
public TiledNode(int x, int y) {
this.x = x;
this.y = y;
this.connections = new Array<Connection<TiledNode>>(4);
}
public TiledNode(int x, int y, int capacity) {
this.x = x;
this.y = y;
this.connections = new Array<Connection<TiledNode>>(capacity);
}
public TiledNode(int x, int y, Array<Connection<TiledNode>> connections) {
this.x = x;
this.y = y;
this.connections = connections;
}
public int getIndex() {
return x * TiledGraph.height + y;
}
}
// Test
TiledGraph graph = new TiledGraph(25, 15);
TiledManhattanDistance<TiledNode> heuristic = new TiledManhattanDistance<TiledNode>();
IndexedAStarPathFinder<TiledNode> pathFinder = new IndexedAStarPathFinder<TiledNode>(graph, true);
TiledNode start = new TiledNode(1,1);
TiledNode end = new TiledNode(5,5);
DefaultGraphPath<TiledNode> outPath = new DefaultGraphPath<TiledNode>();
// DefaultGraphPath<Connection<TiledNode>> outPath = new DefaultGraphPath<Connection<TiledNode>>();
if(pathFinder.searchNodePath(start, end, heuristic, outPath)) {
while(outPath.iterator().hasNext()) {
// Connection<TiledNode> connection = outPath.iterator().next();
TiledNode node = outPath.iterator().next();
Gdx.app.log("Connection", node.x + ", " + node.y);
}
} else {
Gdx.app.log("", "No path");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment