diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 109ca2d..8b36885 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -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(); - } - for (int i = 0; i < Math.abs(path[1]); i++) { - WindowOfFreeMove.sleep(500); - moveY(); + switch (direction){ + case UP: + moveUp(); + break; + case DOWN: + moveDown(); + break; + case LEFT: + moveLeft(); + break; + case RIGHT: + moveRight(); + break; + } 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); - } - else { - y--; - set(x,y); - } + public void moveDown() { + set(x, y++); + } + + public void moveLeft() { + set(x--, y); + } + + public void moveRight() { + set(x++, y); } @Override diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java index 58bac25..d81a537 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java @@ -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; } diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java b/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java new file mode 100644 index 0000000..a9fb133 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java @@ -0,0 +1,5 @@ +package ruscz.game.gameOfProd.Scripts; + +public enum Direction { + UP, DOWN, LEFT, RIGHT +} diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java index c456209..a4e805a 100644 --- a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java @@ -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 getWayBySimpleAI(int startX, int startY, int endX, int endY) { + int nowX = startX; + int nowY = startY; + LinkedList 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; } }