PathFinder simpleAI

This commit is contained in:
iryupnik 2020-03-19 22:33:50 +01:00
parent e395067ab5
commit 1d6a44aa37
4 changed files with 76 additions and 38 deletions

View File

@ -2,6 +2,8 @@ package ruscz.game.gameOfProd.Cells;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapPainter; import ruscz.game.gameOfProd.Map.MapPainter;
import ruscz.game.gameOfProd.Scripts.Direction;
import ruscz.game.gameOfProd.Scripts.PathFinder;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.awt.*; import java.awt.*;
@ -22,41 +24,41 @@ public class Npc extends Person {
public void move(int endX, int endY) { public void move(int endX, int endY) {
x = this.getX(); x = this.getX();
y = this.getY(); y = this.getY();
path = new int[2]; PathFinder pathFinder = new PathFinder(getMap());
path[0] = endX-x; for (Direction direction: pathFinder.getWayBySimpleAI(getX(), getY(), endX, endY)){
path[1] = endY-y;
for (int i = 0; i < Math.abs(path[0]); i++) {
WindowOfFreeMove.sleep(500); WindowOfFreeMove.sleep(500);
moveX(); switch (direction){
mapPainter.repaintCanvas(); case UP:
} moveUp();
for (int i = 0; i < Math.abs(path[1]); i++) { break;
WindowOfFreeMove.sleep(500); case DOWN:
moveY(); moveDown();
break;
case LEFT:
moveLeft();
break;
case RIGHT:
moveRight();
break;
}
mapPainter.repaintCanvas(); mapPainter.repaintCanvas();
} }
} }
public void moveX() { public void moveUp() {
if (path[0] > 0) { set(x, y--);
x++;
set(x, y);
}
else {
x--;
set(x,y);
}
} }
public void moveY() { public void moveDown() {
if (path[1] > 0) { set(x, y++);
y++; }
set(x, y);
} public void moveLeft() {
else { set(x--, y);
y--; }
set(x,y);
} public void moveRight() {
set(x++, y);
} }
@Override @Override

View File

@ -16,6 +16,10 @@ public class Person extends Cell {
private static boolean isInEvent; private static boolean isInEvent;
private int size; private int size;
public Map getMap() {
return map;
}
public void setDirectionMoveLast(int directionMoveLast) { public void setDirectionMoveLast(int directionMoveLast) {
this.directionMoveLast = directionMoveLast; this.directionMoveLast = directionMoveLast;
} }

View File

@ -0,0 +1,5 @@
package ruscz.game.gameOfProd.Scripts;
public enum Direction {
UP, DOWN, LEFT, RIGHT
}

View File

@ -1,19 +1,46 @@
package ruscz.game.gameOfProd.Scripts; package ruscz.game.gameOfProd.Scripts;
import ruscz.game.gameOfProd.Cells.*; 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;
public class PathFinder { public class PathFinder {
Map map; private Map map;
private int startX; private boolean foundWay;
private int startY;
private int endX;
private int endY;
PathFinder(Map map, Npc npc, int endX, int endY){ public PathFinder(Map map) {
this.map = map; this.map = map;
startX = npc.getX(); foundWay = false;
startY = npc.getY(); }
this.endX = endX;
this.endY = endY; public LinkedList<Direction> getWayBySimpleAI(int startX, int startY, int endX, int endY) {
int nowX = startX;
int nowY = startY;
LinkedList<Direction> directions = new LinkedList<>();
while (!foundWay) {
if (nowX == endX && nowY == endY) {
foundWay = true;
}
if (nowX != endX) {
if (endX > nowX) {
directions.add(Direction.RIGHT);
nowX++;
} else {
directions.add(Direction.LEFT);
nowX--;
}
} else {
if (endY > nowY) {
directions.add(Direction.DOWN);
nowY++;
} else {
directions.add(Direction.UP);
nowY--;
}
}
}
return directions;
} }
} }