PathFinder AI (buged)
This commit is contained in:
parent
1d6a44aa37
commit
0a3b9a8d91
@ -9,7 +9,6 @@ import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
||||
import java.awt.*;
|
||||
|
||||
public class Npc extends Person {
|
||||
private int[] path;
|
||||
private MapPainter mapPainter;
|
||||
int x;
|
||||
int y;
|
||||
@ -25,7 +24,7 @@ public class Npc extends Person {
|
||||
x = this.getX();
|
||||
y = this.getY();
|
||||
PathFinder pathFinder = new PathFinder(getMap());
|
||||
for (Direction direction: pathFinder.getWayBySimpleAI(getX(), getY(), endX, endY)){
|
||||
for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){
|
||||
WindowOfFreeMove.sleep(500);
|
||||
switch (direction){
|
||||
case UP:
|
||||
|
||||
@ -40,10 +40,7 @@ public class Map {
|
||||
imageCell = new ImageCell();
|
||||
|
||||
TypeOfCell type;
|
||||
if (isBlocked)
|
||||
type = TypeOfCell.BLOCK;
|
||||
else
|
||||
type = TypeOfCell.EMPTY;
|
||||
type = TypeOfCell.EMPTY;
|
||||
|
||||
for (int i = 0; i < mapArray.length; i++) {
|
||||
for (int j = 0; j < mapArray.length; j++) {
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
package ruscz.game.gameOfProd.Scripts;
|
||||
|
||||
import ruscz.game.gameOfProd.Cells.*;
|
||||
import ruscz.game.gameOfProd.Map.Map;
|
||||
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class PathFinder {
|
||||
private Map map;
|
||||
private boolean noWayFound;
|
||||
private boolean foundWay;
|
||||
private boolean dontXMove;
|
||||
private int nowX;
|
||||
private int nowY;
|
||||
|
||||
public PathFinder(Map map) {
|
||||
this.map = map;
|
||||
@ -16,8 +18,8 @@ public class PathFinder {
|
||||
}
|
||||
|
||||
public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) {
|
||||
int nowX = startX;
|
||||
int nowY = startY;
|
||||
nowX = startX;
|
||||
nowY = startY;
|
||||
LinkedList<Direction> directions = new LinkedList<>();
|
||||
while (!foundWay) {
|
||||
if (nowX == endX && nowY == endY) {
|
||||
@ -43,4 +45,114 @@ public class PathFinder {
|
||||
}
|
||||
return directions;
|
||||
}
|
||||
|
||||
public LinkedList<Direction> getWayWithCollision(int startX, int startY, int endX, int endY) {
|
||||
nowX = startX;
|
||||
nowY = startY;
|
||||
LinkedList<Direction> directions = new LinkedList<>();
|
||||
while (!foundWay) {
|
||||
System.out.println(directions);
|
||||
System.out.println(nowX + " " + nowY);
|
||||
if (directions.toArray().length > 50) {
|
||||
noWayFound = true;
|
||||
break;
|
||||
}
|
||||
if (nowX == endX && nowY == endY) {
|
||||
foundWay = true;
|
||||
}
|
||||
if (nowX != endX && !dontXMove) {
|
||||
if (endX > nowX) {
|
||||
if (!map.isBusyCell(nowX + 1, nowY) && !map.isInteractionCell(nowX + 1, nowY)) {
|
||||
nowX++;
|
||||
System.out.println("YEAH");
|
||||
directions.add(Direction.RIGHT);
|
||||
} else {
|
||||
directions.add(findOptimalStep(nowX, nowY, nowX + 1, nowY, directions));
|
||||
}
|
||||
} else {
|
||||
if (!map.isBusyCell(nowX - 1, nowY) && !map.isInteractionCell(nowX - 1, nowY)) {
|
||||
nowX--;
|
||||
directions.add(Direction.LEFT);
|
||||
} else {
|
||||
directions.add(findOptimalStep(nowX, nowY, nowX - 1, nowY, directions));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (endY > nowY) {
|
||||
if (!map.isBusyCell(nowX, nowY + 1) && !map.isInteractionCell(nowX, nowY + 1)) {
|
||||
nowY++;
|
||||
dontXMove = false;
|
||||
directions.add(Direction.DOWN);
|
||||
} else {
|
||||
directions.add(findOptimalStep(nowX, nowY, nowX, nowY + 1, directions));
|
||||
}
|
||||
} else {
|
||||
if (!map.isBusyCell(nowX, nowY - 1) && !map.isInteractionCell(nowX, nowY - 1)) {
|
||||
nowY--;
|
||||
dontXMove = false;
|
||||
directions.add(Direction.UP);
|
||||
} else {
|
||||
directions.add(findOptimalStep(nowX, nowY, nowX, nowY - 1, directions));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (noWayFound) {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
return directions;
|
||||
}
|
||||
|
||||
private Direction findOptimalStep(int nowX, int nowY, int blockX, int blockY, LinkedList<Direction> directions) {
|
||||
if (nowX == blockX) {
|
||||
if (!map.isBusyCell(nowX, nowY - 1) && !map.isInteractionCell(nowX, nowY - 1)) {
|
||||
this.nowX--;
|
||||
dontXMove = true;
|
||||
return Direction.LEFT;
|
||||
}
|
||||
if (!map.isBusyCell(nowX, nowY + 1) && !map.isInteractionCell(nowX, nowY + 1)) {
|
||||
this.nowX++;
|
||||
dontXMove = true;
|
||||
return Direction.RIGHT;
|
||||
}
|
||||
} else if (nowY == blockY) {
|
||||
if (!map.isBusyCell(nowX + 1, nowY) && !map.isInteractionCell(nowX + 1, nowY)) {
|
||||
this.nowY--;
|
||||
dontXMove = false;
|
||||
return Direction.UP;
|
||||
}
|
||||
if (!map.isBusyCell(nowX - 1, nowY) && !map.isInteractionCell(nowX - 1, nowY)) {
|
||||
this.nowY++;
|
||||
dontXMove = false;
|
||||
return Direction.DOWN;
|
||||
}
|
||||
}
|
||||
|
||||
Direction dir;
|
||||
switch (directions.getLast()){
|
||||
case UP:
|
||||
this.nowY--;
|
||||
dontXMove = false;
|
||||
dir = Direction.UP;
|
||||
break;
|
||||
case DOWN:
|
||||
this.nowY++;
|
||||
dontXMove = false;
|
||||
dir = Direction.DOWN;
|
||||
break;
|
||||
case LEFT:
|
||||
this.nowX--;
|
||||
dontXMove = true;
|
||||
dir = Direction.LEFT;
|
||||
break;
|
||||
case RIGHT:
|
||||
this.nowX++;
|
||||
dontXMove = true;
|
||||
dir = Direction.RIGHT;
|
||||
break;
|
||||
default:
|
||||
dir = null;
|
||||
}
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
|
||||
@ -184,8 +184,8 @@ public class WindowOfFreeMove extends JFrame {
|
||||
|
||||
mapPainter = new MapPainter(mapLinkedList, canvas);
|
||||
|
||||
Npc npc = new Npc(1,1, m5, mapPainter);
|
||||
m5.setNpcs(npc);
|
||||
Npc npc1 = new Npc(1,1, m5, mapPainter);
|
||||
m5.setNpcs(npc1);
|
||||
|
||||
setMapPainter(m5);
|
||||
|
||||
@ -207,8 +207,8 @@ public class WindowOfFreeMove extends JFrame {
|
||||
});
|
||||
timer.start();
|
||||
while (true){
|
||||
npc2.move(5,1);
|
||||
npc2.move(1, 5);
|
||||
npc2.move(6,1);
|
||||
npc2.move(1, 6);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user