diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java b/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java index 83adbee..2958e80 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Cell.java @@ -18,6 +18,10 @@ public class Cell { this.y = y; } + public void setColor(Color color) { + this.color = color; + } + public int getX() { // get the X coordinate return x; } diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java index 0cc7456..34e1c6e 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -10,25 +10,18 @@ import java.awt.*; public class Npc extends Person { private MapPainter mapPainter; - int x; - int y; - Color color; + private Color color; public Npc(int x, int y, Map map, MapPainter mapPainter) { super(x, y, map); this.mapPainter = mapPainter; - this.color = Color.cyan; + setColor(Color.cyan); } 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)){ - System.out.println(getX() + " " + getY()); - System.out.println(direction); -// WindowOfFreeMove.sleep(500); + WindowOfFreeMove.sleep(500); switch (direction){ case UP: moveUp(); @@ -45,24 +38,26 @@ public class Npc extends Person { } mapPainter.repaintCanvas(); } - System.out.println("-----------------------"); - System.out.println("WTF ARE YOU?! " + getX() + " " + getY()); } - public void moveUp() { - set(x, y--); + private void moveUp() { + int y = getY() - 1; + set(getX(), y); } - public void moveDown() { - set(x, y++); + private void moveDown() { + int y = getY() + 1; + set(getX(), y); } - public void moveLeft() { - set(x--, y); + private void moveLeft() { + int x = getX() - 1; + set(x, getY()); } - public void moveRight() { - set(x++, y); + private void moveRight() { + int x = getX() + 1; + set(x, getY()); } @Override diff --git a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java index f671071..733d449 100644 --- a/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java +++ b/src/main/java/ruscz/game/gameOfProd/Scripts/PathFinder.java @@ -1,23 +1,34 @@ package ruscz.game.gameOfProd.Scripts; import ruscz.game.gameOfProd.Map.Map; +import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.util.LinkedList; public class PathFinder { + private static final int MAX_STEPS = 200; + private Map map; private boolean foundWay; + private int startX; + private int startY; public PathFinder(Map map) { this.map = map; foundWay = false; } + private LinkedList prepareDirection(){ + LinkedList directions = new LinkedList<>(); + directions.add(Direction.NONE); + return directions; + } + 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) { + public LinkedList getWayWithoutCollision(int startX, int startY, int endX, int endY) { int nowX = startX; int nowY = startY; LinkedList directions = new LinkedList<>(); @@ -47,37 +58,41 @@ public class PathFinder { } private LinkedList getWayWithCollision(int startX, int startY, int endX, int endY, LinkedList directions) { -// System.out.println(startX + " == " + endX + " | " + startY + " == " + endY); + System.out.println(directions); if (startX == endX && startY == endY){ - System.out.println("YAS!"); + foundWay = true; return directions; } + if (directions.toArray().length == MAX_STEPS){ + return prepareDirection(); + } + for (Direction direction: getAllWays()){ switch (direction){ case UP: - if (!map.isBusyCell(startX, startY - 1) && directions.getLast() != Direction.DOWN){ + if (!map.isBusyCell(startX, startY - 1) && startY - 1 != -1 && directions.getLast() != Direction.DOWN){ directions.add(Direction.UP); startY--; return getWayWithCollision(startX, startY, endX, endY, directions); } break; case DOWN: - if (!map.isBusyCell(startX, startY + 1) && directions.getLast() != Direction.UP){ + if (!map.isBusyCell(startX, startY + 1) & startY + 1 != WindowOfFreeMove.CANVAS_HEIGHT + 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){ + if (!map.isBusyCell(startX - 1, startY) && startX - 1 != -1 && 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){ + if (!map.isBusyCell(startX + 1, startY) && startX - 1 != WindowOfFreeMove.CANVAS_WIDTH + 1 && directions.getLast() != Direction.LEFT){ directions.add(Direction.RIGHT); startX++; return getWayWithCollision(startX, startY, endX, endY, directions); @@ -90,8 +105,13 @@ public class PathFinder { } 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); + this.startX = startX; + this.startY = startY; + + LinkedList directions = getWayWithCollision(startX, startY, endX, endY, prepareDirection()); + if (foundWay) + return directions; + else + return prepareDirection(); } } \ 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 4ed24a7..d2d96de 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -200,7 +200,7 @@ public class WindowOfFreeMove extends JFrame { isReadyToPaint = true; canvas.repaint(); - Npc npc2 = new Npc(1,26, m6, mapPainter); + Npc npc2 = new Npc(1,1, m6, mapPainter); m6.setNpcs(npc2); Timer timer = new Timer(10, new ActionListener() { @@ -213,7 +213,8 @@ public class WindowOfFreeMove extends JFrame { }); timer.start(); while (true){ - npc2.move(9,34); + npc2.move(9,9); + npc2.move(1,1); } }