diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 7fee58a..0cc7456 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -21,11 +21,14 @@ public class Npc extends Person { } public void move(int endX, int endY) { +// System.out.println("NPC: " + getX() + " == " + endX + " | " + getY() + " == " + endY); x = this.getX(); y = this.getY(); PathFinder pathFinder = new PathFinder(getMap()); for (Direction direction: pathFinder.getWayWithCollision(getX(), getY(), endX, endY)){ - WindowOfFreeMove.sleep(500); + System.out.println(getX() + " " + getY()); + System.out.println(direction); +// WindowOfFreeMove.sleep(500); switch (direction){ case UP: moveUp(); @@ -42,6 +45,8 @@ public class Npc extends Person { } mapPainter.repaintCanvas(); } + System.out.println("-----------------------"); + System.out.println("WTF ARE YOU?! " + getX() + " " + getY()); } public void moveUp() { diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java index d81a537..ee2073c 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Person.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Person.java @@ -113,7 +113,8 @@ public class Person extends Cell { map.findEventByXY(x, y).moveToMap(); } } else { - if (!map.isBusyCell(x, y) && !map.isInteractionCell(x, y)) { + if (!map.isInteractionCell(x, y)) //TODO isBusyCell + { set(x, y); } } diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index 4d58b34..f846e16 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -250,10 +250,10 @@ public class Map { public boolean isBusyCell(int x, int y) { -// for (Block cell : block) { -// if (cell.isInBlock(x, y)) -// return true; -// } + for (Block cell : block) { + if (cell.isInBlock(x, y)) + return true; + } return false; } diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java b/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java index a9fb133..80677f8 100644 --- a/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/Direction.java @@ -1,5 +1,5 @@ package ruscz.game.gameOfProd.Scripts; public enum Direction { - UP, DOWN, LEFT, RIGHT + UP, DOWN, LEFT, RIGHT, NONE } diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java index d704acb..f671071 100644 --- a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java @@ -6,20 +6,20 @@ 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; foundWay = false; } + private Direction[] getAllWays(){ + return new Direction[]{Direction.DOWN, Direction.UP, Direction.RIGHT, Direction.LEFT}; + } + public LinkedList getWayBySimpleAI(int startX, int startY, int endX, int endY) { - nowX = startX; - nowY = startY; + int nowX = startX; + int nowY = startY; LinkedList directions = new LinkedList<>(); while (!foundWay) { if (nowX == endX && nowY == endY) { @@ -46,113 +46,52 @@ public class PathFinder { return directions; } - public LinkedList getWayWithCollision(int startX, int startY, int endX, int endY) { - nowX = startX; - nowY = startY; - LinkedList 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; + private LinkedList getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList directions) { +// System.out.println(startX + " == " + endX + " | " + startY + " == " + endY); + if (startX == endX && startY == endY){ + System.out.println("YAS!"); + return directions; + } + + for (Direction direction: getAllWays()){ + switch (direction){ + case UP: + if (!map.isBusyCell(startX, startY - 1) && directions.getLast() != Direction.DOWN){ directions.add(Direction.UP); - } else { - directions.add(findOptimalStep(nowX, nowY, nowX, nowY - 1, directions)); + startY--; + return getWayWithCollision(startX, startY, endX, endY, directions); } - } + break; + case DOWN: + if (!map.isBusyCell(startX, startY + 1) && directions.getLast() != Direction.UP){ + directions.add(Direction.DOWN); + startY++; + return getWayWithCollision(startX, startY, endX, endY, directions); + } + break; + case LEFT: + if (!map.isBusyCell(startX - 1, startY) && directions.getLast() != Direction.RIGHT){ + directions.add(Direction.LEFT); + startX--; + return getWayWithCollision(startX, startY, endX, endY, directions); + } + break; + case RIGHT: + if (!map.isBusyCell(startX + 1, startY) && directions.getLast() != Direction.LEFT){ + directions.add(Direction.RIGHT); + startX++; + return getWayWithCollision(startX, startY, endX, endY, directions); + } + break; } } - if (noWayFound) { - return new LinkedList<>(); - } - return directions; + directions.removeLast(); + return getWayWithCollision(startX, startY, endX, endY, directions); } - private Direction findOptimalStep(int nowX, int nowY, int blockX, int blockY, LinkedList 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; + public LinkedList getWayWithCollision(int startX, int startY, int endX, int endY){ + LinkedList directions = new LinkedList<>(); + directions.add(Direction.NONE); + return getWayWithCollision(startX, startY, endX, endY, directions); } -} +} \ No newline at end of file diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index 4300d80..4ed24a7 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -200,8 +200,8 @@ public class WindowOfFreeMove extends JFrame { isReadyToPaint = true; canvas.repaint(); - Npc npc2 = new Npc(1,1, m1, mapPainter); - m1.setNpcs(npc2); + Npc npc2 = new Npc(1,26, m6, mapPainter); + m6.setNpcs(npc2); Timer timer = new Timer(10, new ActionListener() { @Override @@ -213,8 +213,7 @@ public class WindowOfFreeMove extends JFrame { }); timer.start(); while (true){ - npc2.move(6,1); - npc2.move(1, 6); + npc2.move(9,34); } }