OK, so I been searching and searching for pathfinder stuff in java and finally found how to make a pathfinder that can avoid walls. But my trouble is how to make the enemy follow the path. I been looking it up but been getting results from unity, gamemaker and a few other nonjava junk. I tried finding tutorials but been in wrong language, so does anyone know how to make my enemy follow the path my pathmaker made? Here the code I’m using to make my path I can’t find the next step.
// here is the main script
import pathfinder.*;
Path p;
Control con = new Control();
int w = 25;
ArrayList<Wall> wall = new ArrayList<Wall>();
// PathFinding_01
Graph graph;
// These next 2 are only needed to display
// the nodes and edges.
GraphEdge[] edges;
GraphNode[] nodes;
GraphNode[] route;
// Pathfinder algorithm
IGraphSearch pathFinder;
// Used to indicate the start and end nodes as selected by the user.
GraphNode startNode, endNode;
void setup() {
size(650, 650);
textSize(8);
p = new Path();
// Create graph
p.createGraph();
// Get nodes and edges
nodes = graph.getNodeArray();
edges = graph.getAllEdgeArray();
// Now get a path finder object
pathFinder = new GraphSearch_Astar(graph);
// Now get a route between 2 nodes
// You can change the parameter values but they must be valid IDs
pathFinder.search(2, 550);
route = pathFinder.getRoute();
}
void draw() {
background(0);
p.drawGraph();
p.drawPath();
for (Wall w: wall){
w.show();
}
noLoop();
}
// here control where I would be placing the walls up
class Control{
void update(){
graph.removeNode(25);
wall.add(new Wall(0,1));
}
}
// here the pathmaker
class Path{
void drawGraph() {
// Edges first
strokeWeight(2);
stroke(180, 180, 200);
for (int i = 0; i < edges.length; i++) {
GraphNode from = edges[i].from();
GraphNode to = edges[i].to();
line(from.xf(), from.yf(), to.xf(), to.yf());
}
// Nodes next
noStroke();
fill(240, 255, 240);
for (int i = 0; i < nodes.length; i++) {
GraphNode node = nodes[i];
ellipse(node.xf(), node.yf(), 6, 6);
text(node.id(), node.xf() + 4, node.yf() - 2);
}
}
void drawPath() {
strokeWeight(10);
stroke(200, 255, 200, 160);
for (int i = 1; i < route.length; i++) {
GraphNode from = route[i-1];
GraphNode to = route[i];
line(from.xf(), from.yf(), to.xf(), to.yf());
}
}
void createGraph() {
graph = new Graph();
// Create and add node
GraphNode node;
int node_id = 0;
for (int v = 0; v < w; v++) {
for (int h = 0; h < w; h++) {
node = new GraphNode(node_id, h * (w + 1), w + v * w);
graph.addNode(node);
node_id++;
}
}
con.update();
// Create horizontal edges
for (int v = 0; v < w; v++) {
for (int h = 1; h < w; h++) {
graph.addEdge(v * w + h-1, v * w + h, 0, 0);
}
}
// Create vertical edges
for (int h = 0; h < w; h++) {
for (int v = 1; v < w; v++) {
graph.addEdge(v * w - w + h, v * w + h, 0, 0);
}
}
}
}
// here the wall
class Wall{
int x, y;
Wall(int x, int y){
this.x = x * w;
this.y = y * w;
}
void show(){
noStroke();
fill(255,0,0);
rect(x,y,w,w);
}
}
the enemy haven’t been made because I found no code to make it do what I want. So does anyone know how to make the enemy follow the path made by this pathfinder. I’ll be happy with tutorials as long as they are in java