Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package blockygames.game.pathfinding;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.List;
- import blockygames.game.Game;
- import blockygames.game.level.tiles.Tile;
- public class FindPath {
- private boolean isFindingPath = false;
- private List<Point> openList = new ArrayList<Point>();
- private List<Point> closedList = new ArrayList<Point>();
- private List<Point> path = new ArrayList<Point>();
- private Point lastLowestF = new Point(-1, -1);
- public List<Point> findPath(Point start, Point end) {
- if(isFindingPath){
- return null;
- }
- isFindingPath = true;
- openList.clear();
- closedList.clear();
- path.clear();
- if(Game.level.isTileWalkable((int)end.getX(), (int)end.getY())){
- checkNodesAroundPoint(start);
- changeToClosedList(start);
- changeToClosedList(searchLowestF(start, end));
- while(isFindingPath && !openList.isEmpty() && closedList.size() <= 10000){
- System.out.println(openList.size() + ", " + closedList.size());
- Point lowestF = searchLowestF(start, end);
- checkNodesAroundPoint(lowestF);
- }
- for (Point p : closedList) {
- Game.level.alterTile((int)p.getX(), (int)p.getY(), Tile.POSIONGRASS);
- }
- isFindingPath = false;
- return path;
- }
- isFindingPath = false;
- return null;
- }
- private void changeToClosedList(Point change){
- closedList.add(change);
- openList.remove(change);
- }
- public void checkNodesAroundPoint(Point search){
- for(int x = (int)search.getX() - 1; x <= (int)search.getX() + 1; x++){
- for(int y = (int)search.getY() - 1; y <= (int)search.getY() + 1; y++){
- if(!openList.contains(new Point(x,y))){
- if(Game.level.isTileWalkable(x, y)){
- if(x == (int)search.getX() - 1 && y == (int)search.getY() - 1){
- if(Game.level.isTileWalkable(x + 1, y)){
- if(Game.level.isTileWalkable(x, y + 1)){
- openList.add(new Point(x,y));
- Game.level.alterTile(x, y, Tile.ROCK);
- }
- }
- } else if(x == (int)search.getX() - 1 && y == (int)search.getY() + 1){
- if(Game.level.isTileWalkable(x + 1, y)){
- if(Game.level.isTileWalkable(x, y - 1)){
- openList.add(new Point(x,y));
- Game.level.alterTile(x, y, Tile.ROCK);
- }
- }
- } else if(x == (int)search.getX() + 1 && y == (int)search.getY() - 1){
- if(Game.level.isTileWalkable(x - 1, y)){
- if(Game.level.isTileWalkable(x, y + 1)){
- openList.add(new Point(x,y));
- Game.level.alterTile(x, y, Tile.ROCK);
- }
- }
- } else if(x == (int)search.getX() + 1 && y == (int)search.getY() + 1){
- if(Game.level.isTileWalkable(x - 1, y)){
- if(Game.level.isTileWalkable(x, y - 1)){
- openList.add(new Point(x,y));
- Game.level.alterTile(x, y, Tile.ROCK);
- }
- }
- } else{
- openList.add(new Point(x,y));
- Game.level.alterTile(x, y, Tile.ROCK);
- }
- }
- }
- }
- }
- }
- public Point searchLowestF(Point start, Point end){
- Point lowestF = new Point(-1, -1);
- int lowestFInList = -1;
- int g = 0;
- int h = 0;
- int f = 0;
- for (Point p : openList) {
- if(lastLowestF == new Point(-1, -1)){
- if(p.getX() == start.getX() || p.getY() == start.getY()){
- g = 10;
- } else {
- g = 14;
- }
- } else {
- if(p.getX() == lastLowestF.getX() || p.getY() == lastLowestF.getY()){
- g = 10;
- } else {
- g = 14;
- }
- }
- if(p.getX() <= end.getX() && p.getY() <= end.getY()){
- h = (((int)end.getX() - (int)p.getX()) + ((int)end.getY() - (int)p.getY()))* 10;
- } else if(p.getX() > end.getX() && p.getY() <= end.getY()){
- h = (((int)p.getX() - (int)end.getX()) + ((int)end.getY() - (int)p.getY()))* 10;
- } else if(p.getX() <= end.getX() && p.getY() > end.getY()){
- h = (((int)end.getX() - (int)p.getX()) + ((int)p.getY() - (int)end.getY()))* 10;
- } else if(p.getX() > end.getX() && p.getY() > end.getY()){
- h = (((int)p.getX() - (int)end.getX()) + ((int)p.getY() - (int)end.getY()))* 10;
- }
- if(h == 0){
- isFindingPath = false;
- }
- f = g + h;
- if(lowestFInList == -1){
- lowestFInList = f;
- lowestF = p;
- } else if(f < lowestFInList){
- lowestFInList = f;
- lowestF = p;
- }
- }
- lastLowestF = lowestF;
- changeToClosedList(lowestF);
- return lowestF;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement