/* returned Path contains game coordinates, not tile coordinates */ public Path FindPath() { if (this.found) { PathTile end = this.map[this.targetX][this.targetY]; PathTile current = end.parent; ArrayList<PathTile> temp = new ArrayList<PathTile>(); Path path = new Path(); end.onPath = true; temp.add(end); while (current != null) { current.onPath = true; if (!(current.x == this.startX && current.y == this.startY)) { temp.add(current); } current = current.parent; } int sx = this.startX; int sy = this.startY; for (int i = temp.size() - 1; i >= 0; i--) { PathTile tempTile = temp.get(i); if (tempTile.x < sx) { path.AddTarget(tempTile.x * tilesize, tempTile.y * tilesize, Direction.LEFT); } else if (tempTile.x > sx) { path.AddTarget(tempTile.x * tilesize, tempTile.y * tilesize, Direction.RIGHT); } else if (tempTile.y < sy) { path.AddTarget(tempTile.x * tilesize, tempTile.y * tilesize, Direction.UP); } else if (tempTile.y > sy) { path.AddTarget(tempTile.x * tilesize, tempTile.y * tilesize, Direction.DOWN); } sx = tempTile.x; sy = tempTile.y; } for (int x = 0; x < map.length; x++) { for (int y = 0; y < map[0].length; y++) { map[x][y].parent = null; map[x][y].f = 0; map[x][y].g = 0; map[x][y].onPath = false; } } return path; } else { return null; } }
public PathTile CalculateTotalCost( PathTile[][] map, int x, int y, ArrayList<PathTile> open, ArrayList<PathTile> closed) { if (x < map.length && y < map[0].length) { PathTile current = map[x][y]; PathTile temp; temp = GetNorth(map, x, y); if (temp != null && !temp.solid) { if (!closed.contains(temp) && !open.contains(temp)) { temp.g = current.g + 1; temp.f = temp.g + temp.h; temp.parent = current; map[temp.x][temp.y] = temp; open.add(temp); } } temp = GetSouth(map, x, y); if (temp != null && !temp.solid) { if (!closed.contains(temp) && !open.contains(temp)) { temp.g = current.g + 1; temp.f = temp.g + temp.h; temp.parent = current; map[temp.x][temp.y] = temp; open.add(temp); } } temp = GetEast(map, x, y); if (temp != null && !temp.solid) { if (!closed.contains(temp) && !open.contains(temp)) { temp.g = current.g + 1; temp.f = temp.g + temp.h; temp.parent = current; map[temp.x][temp.y] = temp; open.add(temp); } } temp = GetWest(map, x, y); if (temp != null && !temp.solid) { if (!closed.contains(temp) && !open.contains(temp)) { temp.g = current.g + 1; temp.f = temp.g + temp.h; temp.parent = current; map[temp.x][temp.y] = temp; open.add(temp); } } open.remove(current); closed.add(current); return current; } return null; }