Skip to content

Commit a3334b1

Browse files
committed
A* Search Algorithm pathfinding
Wrote the A* Search Algorithm, not yet implemented
1 parent bcb480e commit a3334b1

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

src/com/redomar/game/level/LevelHandler.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.io.File;
55
import java.io.IOException;
66
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.Comparator;
79
import java.util.List;
810
import java.util.logging.Level;
911

@@ -15,6 +17,7 @@
1517
import com.redomar.game.entities.PlayerMP;
1618
import com.redomar.game.gfx.Screen;
1719
import com.redomar.game.level.tiles.Tile;
20+
import com.redomar.game.lib.utils.Vector2i;
1821
import com.redomar.game.net.packets.Packet01Disconnect;
1922

2023
public class LevelHandler {
@@ -26,6 +29,16 @@ public class LevelHandler {
2629
private String imagePath;
2730
private BufferedImage image;
2831

32+
private Comparator<Node> nodeSorter = new Comparator<Node>() {
33+
34+
public int compare(Node n0, Node n1) {
35+
if(n1.fCost < n0.fCost) return +1;
36+
if(n1.fCost > n0.fCost) return -1;
37+
return 0;
38+
}
39+
40+
};
41+
2942
public LevelHandler(String imagePath) {
3043

3144
if (imagePath != null) {
@@ -194,6 +207,60 @@ public void movePlayer(String username, int x, int y, int numSteps,
194207
player.setMovingDir(movingDir);
195208
}
196209

210+
public List<Node> findPath(Vector2i start, Vector2i goal){
211+
List<Node> openList = new ArrayList<Node>();
212+
List<Node> closedList = new ArrayList<Node>();
213+
Node current = new Node(start, null, 0, getDistance(start, goal));
214+
openList.add(current);
215+
while(openList.size() > 0){
216+
Collections.sort(openList, nodeSorter);
217+
current = openList.get(0);
218+
if(current.tile.equals(goal)){
219+
List<Node> path = new ArrayList<Node>();
220+
while (current.parent != null) {
221+
path.add(current);
222+
current = current.parent;
223+
}
224+
openList.clear();
225+
closedList.clear();
226+
return path;
227+
}
228+
openList.remove(current);
229+
closedList.add(current);
230+
for(int i = 0; i < 9; i++){
231+
if(i == 4) continue;
232+
int x = current.tile.getX();
233+
int y = current.tile.getY();
234+
int xi = (i % 3) - 1;
235+
int yi = (i / 3) - 1;
236+
Tile at = getTile(x + xi,y + yi);
237+
if(at == null) continue;
238+
if(at.isSolid()) continue;
239+
Vector2i a = new Vector2i(x + xi, y + yi);
240+
double gCost = current.gCost + getDistance(current.tile, a);
241+
double hCost = getDistance(a, goal);
242+
Node node = new Node(a, current, gCost, hCost);
243+
if(isVectorInList(closedList, a) && gCost >= node.gCost) continue;
244+
if(!isVectorInList(openList, a) || gCost < node.gCost) openList.add(node);
245+
}
246+
}
247+
closedList.clear();
248+
return null;
249+
}
250+
251+
private boolean isVectorInList(List<Node> list, Vector2i vector){
252+
for(Node n : list){
253+
if(n.tile.equals(vector)) return true;
254+
}
255+
return false;
256+
}
257+
258+
private double getDistance(Vector2i tile, Vector2i goal){
259+
double dx = tile.getX() - goal.getX();
260+
double dy = tile.getY() - goal.getY();
261+
return Math.sqrt(dx * dx + dy * dy);
262+
}
263+
197264
public List<Entity> getEntities(Entity e, int radius){
198265
List<Entity> result = new ArrayList<Entity>();
199266
int ex = (int) e.getX();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.redomar.game.level;
2+
3+
import com.redomar.game.lib.utils.Vector2i;
4+
5+
public class Node {
6+
7+
public Vector2i tile;
8+
public Node parent;
9+
public double fCost, gCost, hCost;
10+
11+
public Node(Vector2i tile, Node parent, double gCost, double hCost){
12+
this.tile = tile;
13+
this.parent = parent;
14+
this.gCost = gCost;
15+
this.hCost = hCost;
16+
this.fCost = (this.gCost + this.hCost);
17+
}
18+
19+
}

src/com/redomar/game/lib/utils/Vector2i.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ public Vector2i subtract(Vector2i vector){
2828
return this;
2929
}
3030

31+
public boolean equals(Object object){
32+
if (!(object instanceof Vector2i)) return false;
33+
Vector2i vector = (Vector2i) object;
34+
if(vector.getX() == this.getX() && vector.getY() == this.getY()) return true;
35+
return false;
36+
}
37+
3138
public void set(int x, int y){
3239
this.x = x;
3340
this.y = y;

0 commit comments

Comments
 (0)