From 8b809675555a2683528a4dd52f7dfb72cd0a465b Mon Sep 17 00:00:00 2001 From: musilto9 Date: Tue, 17 Mar 2020 12:19:59 +0100 Subject: [PATCH] made camera --- .../java/ruscz/game/gameOfProd/Cells/Npc.java | 49 +++++++++++++++++++ .../gameOfProd/Windows/WindowOfFreeMove.java | 15 ++++-- 2 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ruscz/game/gameOfProd/Cells/Npc.java diff --git a/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java new file mode 100644 index 0000000..a1aa659 --- /dev/null +++ b/src/main/java/ruscz/game/gameOfProd/Cells/Npc.java @@ -0,0 +1,49 @@ +package ruscz.game.gameOfProd.Cells; + +import ruscz.game.gameOfProd.Map.Map; + +public class Npc extends Person { + private int[] path; + int x; + int y; + + public Npc(int x, int y, Map map) { + super(x, y, map); + } + + 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++) { + moveX(); + } + for (int i = 0; i < Math.abs(path[1]); i++) { + moveY(); + } + } + + public void moveX() { + if (path[0] > 0) { + x++; + set(x, y); + } + else { + x--; + set(x,y); + } + } + + public void moveY() { + if (path[1] > 0) { + y--; + set(x, y); + } + else { + y--; + set(x,y); + } + } +} diff --git a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java index 745c59f..fd3bded 100644 --- a/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java +++ b/src/main/java/ruscz/game/gameOfProd/Windows/WindowOfFreeMove.java @@ -19,8 +19,10 @@ import java.util.LinkedList; public class WindowOfFreeMove extends JFrame { public static final int CELL_SIZE = 40; - public static final int CANVAS_WIDTH = 15; - public static final int CANVAS_HEIGHT = 15; + public static final int CANVAS_WIDTH = 10; + public static final int CANVAS_HEIGHT = 10; + public static final int WINDOW_WIDTH = 5; + public static final int WINDOW_HEIGHT = 5; private static boolean canPersonMove; private static int nextMove; @@ -84,7 +86,7 @@ public class WindowOfFreeMove extends JFrame { canvas.setBounds(0, 0, CELL_SIZE * CANVAS_HEIGHT, CELL_SIZE * CANVAS_WIDTH); dialogBar = new DialogBar(); - dialogBar.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.CANVAS_HEIGHT / 4); + dialogBar.setBounds(0, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT * 3 / 4, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_WIDTH, WindowOfFreeMove.CELL_SIZE * WindowOfFreeMove.WINDOW_HEIGHT / 4); dialogBar.setBackground(Color.BLACK); dialogBar.setVisible(false); dialogBar.setLayout(null); @@ -134,9 +136,9 @@ public class WindowOfFreeMove extends JFrame { setTitle("Ahoj"); setDefaultCloseOperation(EXIT_ON_CLOSE); - setBounds(0, 0, CELL_SIZE * CANVAS_WIDTH + 15, CELL_SIZE * CANVAS_HEIGHT + 35); + setBounds(0, 0, CELL_SIZE * WINDOW_WIDTH + 15, CELL_SIZE * WINDOW_HEIGHT + 35); setLocationRelativeTo(null); - setResizable(true); + setResizable(false); setVisible(true); } @@ -188,6 +190,8 @@ public class WindowOfFreeMove extends JFrame { setMap(m5); + canvas.setLocation((-1) * CELL_SIZE * (map.getActualMap().getPerson().getX() - WINDOW_WIDTH / 2), (-1) * CELL_SIZE * (map.getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2)); + map.createMap(); isReadyToPaint = true; canvas.repaint(); @@ -196,6 +200,7 @@ public class WindowOfFreeMove extends JFrame { @Override public void actionPerformed(ActionEvent actionEvent) { map.getActualMap().getPerson().move(); + canvas.setLocation((-1) * CELL_SIZE * (map.getActualMap().getPerson().getX() - WINDOW_WIDTH / 2), (-1) * CELL_SIZE * (map.getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2)); canvas.repaint(); } });