From 22bee3b9b4636049706f07b67687ddb64ef7cd4d Mon Sep 17 00:00:00 2001 From: musilto9 Date: Thu, 26 Mar 2020 22:13:31 +0100 Subject: [PATCH] bruteforce-fixed free camera --- .../gameOfProd/Cells/InteractionCell.java | 2 + .../java/ruscz/game/gameOfProd/Map/Map.java | 8 ++- .../ruscz/game/gameOfProd/Map/MapPainter.java | 11 ++++ .../ruscz/game/gameOfProd/Windows/Camera.java | 64 ++++++++++++------- .../gameOfProd/Windows/WindowOfFreeMove.java | 21 +++--- src/main/resources/maps/m1.txt | 2 +- src/main/resources/maps/m6.txt | 2 +- src/main/resources/roses-are-red.txt | 2 +- 8 files changed, 76 insertions(+), 36 deletions(-) diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java index 4759623..215fd09 100644 --- a/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java +++ b/src/main/java/ruscz/game/gameOfProd/Cells/InteractionCell.java @@ -64,6 +64,7 @@ public class InteractionCell extends Cell { window.getCamera().setCameraBound(false); window.getCamera().setCameraEnd(Integer.parseInt(cameraInfo[1]), Integer.parseInt(cameraInfo[2])); window.getCamera().setCameraTime(Double.parseDouble(cameraInfo[3])); + window.getCamera().setCameraStopped(false); readNextLine(); } else if (data.contains("")) { @@ -105,6 +106,7 @@ public class InteractionCell extends Cell { dialog.setImageDialog(null); } if (!window.getCamera().getCameraBound()) { + window.getCamera().setCameraStopped(false); window.getCamera().returnFreeCamera(); } else { diff --git a/src/main/java/ruscz/game/gameOfProd/Map/Map.java b/src/main/java/ruscz/game/gameOfProd/Map/Map.java index a9193de..72eb5e9 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/Map.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/Map.java @@ -23,6 +23,7 @@ public class Map { private LinkedList npcs; private int idExit; private ImageCell imageCell; + private int[] canvasSize; private static Scanner scanner; @@ -77,8 +78,8 @@ public class Map { mapArray[i][0] = type; mapArray[i][mapArray[0].length - 1] = type; } - person = new Person(this); + canvasSize = new int[2]; } public Map(TypeOfCell[][] mapArray) { @@ -91,6 +92,7 @@ public class Map { npcs = new LinkedList<>(); this.mapArray = mapArray; person = new Person(this); + canvasSize = new int[2]; } public static Map findMapByName(String nameOfMap, WindowOfFreeMove window) { @@ -114,6 +116,8 @@ public class Map { map.npcs = new LinkedList<>(); map.getImageCell().readMapFile(name + "-tileset"); LinkedList listOfMoveToCells = new LinkedList<>(); + map.canvasSize[0] = Integer.parseInt(size[0]); + map.canvasSize[1] = Integer.parseInt(size[1]); int y = 0; while (scanner.hasNextLine()) { loadMap(map, size, y, window); @@ -244,6 +248,8 @@ public class Map { return mapArray; } + public int[] getCanvasSize() { return canvasSize; } + public boolean isBusyCell(int x, int y) { for (Block cell : block) { diff --git a/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java b/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java index 7aa1e37..eab454a 100644 --- a/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java +++ b/src/main/java/ruscz/game/gameOfProd/Map/MapPainter.java @@ -23,9 +23,13 @@ public class MapPainter { public void setMap(Map map) { if (maps.contains(map)) { actualMap = map; + WindowOfFreeMove.setCanvasSize(map.getCanvasSize()); + resizeCanvas(map.getCanvasSize()); } else { maps.add(map); actualMap = map; + WindowOfFreeMove.setCanvasSize(map.getCanvasSize()); + resizeCanvas(map.getCanvasSize()); } } @@ -92,4 +96,11 @@ public class MapPainter { public void repaintCanvas() { canvas.repaint(); } + + public void resizeCanvas(int[] size) { + canvas.setPreferredSize(new Dimension( + CELL_SIZE * size[0], + CELL_SIZE * size[1])); + canvas.setBounds(0, 0, CELL_SIZE * size[0], CELL_SIZE * size[1]); + } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/Camera.java b/src/main/java/ruscz/game/gameOfProd/Windows/Camera.java index 2c099f4..ff84d68 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/Camera.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/Camera.java @@ -13,6 +13,7 @@ public class Camera { private int startY; private boolean isReturning; private static int count = 0; + private boolean isStopped; public Camera(WindowOfFreeMove window, WindowOfFreeMove.Canvas canvas) { this.window = window; @@ -25,7 +26,8 @@ public class Camera { moveBoundCamera(); } else { - moveFreeCamera(); + if (!isStopped) + moveFreeCamera(); } } @@ -76,6 +78,22 @@ public class Camera { public void moveFreeCamera() { int coordX = (int) (canvas.getLocation().getX()); int coordY = (int) (canvas.getLocation().getY()); + if (coordX > 0) { + endX = 0; + if (isReturning) boundCamera(); + } + else if (CELL_SIZE * WINDOW_WIDTH - coordX + 8 >= CELL_SIZE * CANVAS_WIDTH) { + if (isReturning) boundCamera(); + else isStopped = true; + } + if (coordY > 0) { + if (isReturning) boundCamera(); + else isStopped = true; + } + else if (CELL_SIZE * WINDOW_WIDTH - coordY >= CELL_SIZE * CANVAS_WIDTH) { + if (isReturning) boundCamera(); + else isStopped = true; + } if (coordX != endX || coordY != endY) { int distanceX = endX - startX; int distanceY = endY - startY; @@ -84,30 +102,15 @@ public class Camera { isReturning = false; return; } -// if (time >= 2) { - if (count >= time / 2) { - int stepX = (int) (distanceX / CELL_SIZE / (time / 2)); - int stepY = (int) (distanceY / CELL_SIZE / (time / 2)); - canvas.setLocation(coordX + stepX, coordY + stepY); - count = 0; - } -// } -// else { -// if (count >= time) { -// int stepX = distanceX / CELL_SIZE; -// int stepY = distanceY / CELL_SIZE; -// canvas.setLocation(coordX + stepX, coordY + stepY); -// count = 0; -// } -// } + if (count >= time / 2) { + int stepX = (int) (distanceX / CELL_SIZE / (time / 2)); + int stepY = (int) (distanceY / CELL_SIZE / (time / 2)); + canvas.setLocation(coordX + stepX, coordY + stepY); + count = 0; + } count++; } - if (coordX == endX && coordY == endY && isReturning) { - isBound = true; - isReturning = false; - window.setCanPersonMove(true); - window.getMapPainter().getActualMap().getPerson().setInEvent(false); - } + if (coordX == endX && coordY == endY && isReturning) boundCamera(); } public void returnFreeCamera() { @@ -130,6 +133,17 @@ public class Camera { isReturning = true; } + public void stopCamera() { + + } + + public void boundCamera() { + isBound = true; + isReturning = false; + window.setCanPersonMove(true); + window.getMapPainter().getActualMap().getPerson().setInEvent(false); + } + public void setCameraStart() { this.startX = (int) (canvas.getLocation().getX()); this.startY = (int) (canvas.getLocation().getY()); @@ -146,5 +160,7 @@ public class Camera { public void setCameraBound(boolean bound) { this.isBound = bound; } - public boolean getCameraBound() {return isBound; } + public boolean getCameraBound() { return isBound; } + + public void setCameraStopped(boolean stopped) { isStopped = stopped; } } diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index 705fc01..1b41b64 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -15,10 +15,10 @@ import java.util.LinkedList; public class WindowOfFreeMove extends JFrame { public static final int CELL_SIZE = 40; - public static final int CANVAS_WIDTH = 36; - public static final int CANVAS_HEIGHT = 36; - public static final int WINDOW_WIDTH = 11; - public static final int WINDOW_HEIGHT = 11; + public static int CANVAS_WIDTH = 50; + public static int CANVAS_HEIGHT = 50; + public static final int WINDOW_WIDTH = 9; + public static final int WINDOW_HEIGHT = 9; private static boolean canPersonMove; private static int nextMove; @@ -133,7 +133,7 @@ public class WindowOfFreeMove extends JFrame { setTitle("Ahoj"); setDefaultCloseOperation(EXIT_ON_CLOSE); - setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH + 15, CELL_SIZE * WINDOW_HEIGHT + 35); + setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH + 16, CELL_SIZE * WINDOW_HEIGHT + 39); setLocationRelativeTo(null); setResizable(false); setVisible(true); @@ -145,7 +145,7 @@ public class WindowOfFreeMove extends JFrame { int count = 0; for (int i = 0; i < rec.length / 4; i++) { for (int j = 0; j < rec.length / 3; j++) { - rec[count] = new Rectangle(40 * i, 40 * j, 40, 40); + rec[count] = new Rectangle(CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE, CELL_SIZE); count++; } } @@ -227,8 +227,8 @@ public class WindowOfFreeMove extends JFrame { Graphics2D g2D = (Graphics2D) g; g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - for (int i = 0; i < CANVAS_WIDTH; i++) { - for (int j = 0; j < CANVAS_HEIGHT; j++) { + for (int i = 0; i < mapPainter.getActualMap().getCanvasSize()[0]; i++) { + for (int j = 0; j < mapPainter.getActualMap().getCanvasSize()[1]; j++) { mapPainter.getActualMap().getImageCell().paintBackground(g2D, i, j); } } @@ -257,4 +257,9 @@ public class WindowOfFreeMove extends JFrame { } } + public static void setCanvasSize(int[] size) { + CANVAS_WIDTH = size[0]; + CANVAS_HEIGHT = size[1]; + } + } \ No newline at end of file diff --git a/src/main/resources/maps/m1.txt b/src/main/resources/maps/m1.txt index 05e150e..b444630 100644 --- a/src/main/resources/maps/m1.txt +++ b/src/main/resources/maps/m1.txt @@ -5,7 +5,7 @@ BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK EMPTY EMPTY INTER,first-dialog EMPTY INTER,dialog-test EMPTY EMPTY EMPTY BLOCK -BLOCK EMPTY EMPTY EMPTY TRIGGER,roses-are-red EMPTY EMPTY EMPTY EMPTY BLOCK +BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK MOVETO,5,1,m5 EMPTY MOVETO,6,1,m6 EMPTY EMPTY EMPTY EMPTY MOVETO,1,1,m2 BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK \ No newline at end of file diff --git a/src/main/resources/maps/m6.txt b/src/main/resources/maps/m6.txt index 11745d8..316673e 100644 --- a/src/main/resources/maps/m6.txt +++ b/src/main/resources/maps/m6.txt @@ -10,7 +10,7 @@ EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EM EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK -EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY +EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY TRIGGER,roses-are-red EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY diff --git a/src/main/resources/roses-are-red.txt b/src/main/resources/roses-are-red.txt index 9bf394a..8aea683 100644 --- a/src/main/resources/roses-are-red.txt +++ b/src/main/resources/roses-are-red.txt @@ -1,4 +1,4 @@ Roses are red
violets are blue - -240 -40 4 + -1200 -240 4 unexpected "{"
on line 32 \ No newline at end of file