PathFinder simpleAI
This commit is contained in:
parent
e395067ab5
commit
1d6a44aa37
@ -2,6 +2,8 @@ package ruscz.game.gameOfProd.Cells;
|
||||
|
||||
import ruscz.game.gameOfProd.Map.Map;
|
||||
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 java.awt.*;
|
||||
@ -22,41 +24,41 @@ public class Npc extends Person {
|
||||
public void move(int endX, int endY) {
|
||||
x = this.getX();
|
||||
y = this.getY();
|
||||
path = new int[2];
|
||||
path[0] = endX-x;
|
||||
path[1] = endY-y;
|
||||
for (int i = 0; i < Math.abs(path[0]); i++) {
|
||||
PathFinder pathFinder = new PathFinder(getMap());
|
||||
for (Direction direction: pathFinder.getWayBySimpleAI(getX(), getY(), endX, endY)){
|
||||
WindowOfFreeMove.sleep(500);
|
||||
moveX();
|
||||
mapPainter.repaintCanvas();
|
||||
switch (direction){
|
||||
case UP:
|
||||
moveUp();
|
||||
break;
|
||||
case DOWN:
|
||||
moveDown();
|
||||
break;
|
||||
case LEFT:
|
||||
moveLeft();
|
||||
break;
|
||||
case RIGHT:
|
||||
moveRight();
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < Math.abs(path[1]); i++) {
|
||||
WindowOfFreeMove.sleep(500);
|
||||
moveY();
|
||||
mapPainter.repaintCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
public void moveX() {
|
||||
if (path[0] > 0) {
|
||||
x++;
|
||||
set(x, y);
|
||||
}
|
||||
else {
|
||||
x--;
|
||||
set(x,y);
|
||||
}
|
||||
public void moveUp() {
|
||||
set(x, y--);
|
||||
}
|
||||
|
||||
public void moveY() {
|
||||
if (path[1] > 0) {
|
||||
y++;
|
||||
set(x, y);
|
||||
public void moveDown() {
|
||||
set(x, y++);
|
||||
}
|
||||
else {
|
||||
y--;
|
||||
set(x,y);
|
||||
|
||||
public void moveLeft() {
|
||||
set(x--, y);
|
||||
}
|
||||
|
||||
public void moveRight() {
|
||||
set(x++, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -16,6 +16,10 @@ public class Person extends Cell {
|
||||
private static boolean isInEvent;
|
||||
private int size;
|
||||
|
||||
public Map getMap() {
|
||||
return map;
|
||||
}
|
||||
|
||||
public void setDirectionMoveLast(int directionMoveLast) {
|
||||
this.directionMoveLast = directionMoveLast;
|
||||
}
|
||||
|
||||
@ -0,0 +1,5 @@
|
||||
package ruscz.game.gameOfProd.Scripts;
|
||||
|
||||
public enum Direction {
|
||||
UP, DOWN, LEFT, RIGHT
|
||||
}
|
||||
@ -1,19 +1,46 @@
|
||||
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 {
|
||||
Map map;
|
||||
private int startX;
|
||||
private int startY;
|
||||
private int endX;
|
||||
private int endY;
|
||||
private Map map;
|
||||
private boolean foundWay;
|
||||
|
||||
PathFinder(Map map, Npc npc, int endX, int endY){
|
||||
public PathFinder(Map map) {
|
||||
this.map = map;
|
||||
startX = npc.getX();
|
||||
startY = npc.getY();
|
||||
this.endX = endX;
|
||||
this.endY = endY;
|
||||
foundWay = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user