Created
July 23, 2012 15:09
-
-
Save pxpc2/3164165 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Tile[] getPath(Tile start, Tile end) { | |
final Node[] path = getPath( | |
nodes[start.getX() - Game.getBaseX()][start.getY() - Game.getBaseY()], | |
nodes[end.getX() - Game.getBaseX()][end.getY() - Game.getBaseY()] | |
); | |
Tile[] route = new Tile[path.length]; | |
for (int i = 0; i < path.length; i++) { | |
final Node node = path[i]; | |
final Tile tile = new Tile(node.x + Game.getBaseX(), node.y + Game.getBaseY(), Game.getPlane()); | |
route[i] = tile; | |
} | |
return route; | |
} | |
public static Node[] getPath(Node start, Node end) { | |
start.cost = 0; | |
open.addLast(start); | |
Node current; | |
open.addLast(start); | |
nodes[start.x][start.y] = start; | |
while(!open.isEmpty()) { | |
current = getCheapest(); | |
open.remove(current); | |
for (Node n : current.getNeighbours()) { | |
int cost; | |
if (n.cost > 208 || n.cost < 0) { | |
cost = 0; | |
} else { | |
cost = n.cost; | |
} | |
cost += Calculations.distance(n.x, n.y, end.x, end.y); | |
if (cost < n.cost) { | |
n.cost = cost; | |
n.parent = current; | |
nodes[n.x][n.y] = n; | |
} | |
} | |
} | |
LinkedList<Node> path = new LinkedList<Node>(); | |
Node next = nodes[end.x][end.y]; | |
while (next != null) { | |
path.addFirst(next); | |
next = next.parent; | |
} | |
return path.toArray(new Node[path.size()]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment