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.*;
|
import java.awt.*;
|
||||||
|
|
||||||
public class Npc extends Person {
|
public class Npc extends Person {
|
||||||
private int[] path;
|
|
||||||
private MapPainter mapPainter;
|
private MapPainter mapPainter;
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
@ -25,7 +24,7 @@ public class Npc extends Person {
|
|||||||
x = this.getX();
|
x = this.getX();
|
||||||
y = this.getY();
|
y = this.getY();
|
||||||
PathFinder pathFinder = new PathFinder(getMap());
|
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);
|
WindowOfFreeMove.sleep(500);
|
||||||
switch (direction){
|
switch (direction){
|
||||||
case UP:
|
case UP:
|
||||||
|
|||||||
@ -40,9 +40,6 @@ public class Map {
|
|||||||
imageCell = new ImageCell();
|
imageCell = new ImageCell();
|
||||||
|
|
||||||
TypeOfCell type;
|
TypeOfCell type;
|
||||||
if (isBlocked)
|
|
||||||
type = TypeOfCell.BLOCK;
|
|
||||||
else
|
|
||||||
type = TypeOfCell.EMPTY;
|
type = TypeOfCell.EMPTY;
|
||||||
|
|
||||||
for (int i = 0; i < mapArray.length; i++) {
|
for (int i = 0; i < mapArray.length; i++) {
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
package ruscz.game.gameOfProd.Scripts;
|
package ruscz.game.gameOfProd.Scripts;
|
||||||
|
|
||||||
import ruscz.game.gameOfProd.Cells.*;
|
|
||||||
import ruscz.game.gameOfProd.Map.Map;
|
import ruscz.game.gameOfProd.Map.Map;
|
||||||
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
|
||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
public class PathFinder {
|
public class PathFinder {
|
||||||
private Map map;
|
private Map map;
|
||||||
|
private boolean noWayFound;
|
||||||
private boolean foundWay;
|
private boolean foundWay;
|
||||||
|
private boolean dontXMove;
|
||||||
|
private int nowX;
|
||||||
|
private int nowY;
|
||||||
|
|
||||||
public PathFinder(Map map) {
|
public PathFinder(Map map) {
|
||||||
this.map = map;
|
this.map = map;
|
||||||
@ -16,8 +18,8 @@ public class PathFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) {
|
public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) {
|
||||||
int nowX = startX;
|
nowX = startX;
|
||||||
int nowY = startY;
|
nowY = startY;
|
||||||
LinkedList<Direction> directions = new LinkedList<>();
|
LinkedList<Direction> directions = new LinkedList<>();
|
||||||
while (!foundWay) {
|
while (!foundWay) {
|
||||||
if (nowX == endX && nowY == endY) {
|
if (nowX == endX && nowY == endY) {
|
||||||
@ -43,4 +45,114 @@ public class PathFinder {
|
|||||||
}
|
}
|
||||||
return directions;
|
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);
|
mapPainter = new MapPainter(mapLinkedList, canvas);
|
||||||
|
|
||||||
Npc npc = new Npc(1,1, m5, mapPainter);
|
Npc npc1 = new Npc(1,1, m5, mapPainter);
|
||||||
m5.setNpcs(npc);
|
m5.setNpcs(npc1);
|
||||||
|
|
||||||
setMapPainter(m5);
|
setMapPainter(m5);
|
||||||
|
|
||||||
@ -207,8 +207,8 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
});
|
});
|
||||||
timer.start();
|
timer.start();
|
||||||
while (true){
|
while (true){
|
||||||
npc2.move(5,1);
|
npc2.move(6,1);
|
||||||
npc2.move(1, 5);
|
npc2.move(1, 6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user