made camera

This commit is contained in:
musilto9 2020-03-17 12:19:59 +01:00
parent 09e1b8c799
commit 8b80967555
2 changed files with 59 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -19,8 +19,10 @@ import java.util.LinkedList;
public class WindowOfFreeMove extends JFrame { public class WindowOfFreeMove extends JFrame {
public static final int CELL_SIZE = 40; public static final int CELL_SIZE = 40;
public static final int CANVAS_WIDTH = 15; public static final int CANVAS_WIDTH = 10;
public static final int CANVAS_HEIGHT = 15; 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 boolean canPersonMove;
private static int nextMove; 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); canvas.setBounds(0, 0, CELL_SIZE * CANVAS_HEIGHT, CELL_SIZE * CANVAS_WIDTH);
dialogBar = new DialogBar(); 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.setBackground(Color.BLACK);
dialogBar.setVisible(false); dialogBar.setVisible(false);
dialogBar.setLayout(null); dialogBar.setLayout(null);
@ -134,9 +136,9 @@ public class WindowOfFreeMove extends JFrame {
setTitle("Ahoj"); setTitle("Ahoj");
setDefaultCloseOperation(EXIT_ON_CLOSE); 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); setLocationRelativeTo(null);
setResizable(true); setResizable(false);
setVisible(true); setVisible(true);
} }
@ -188,6 +190,8 @@ public class WindowOfFreeMove extends JFrame {
setMap(m5); 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(); map.createMap();
isReadyToPaint = true; isReadyToPaint = true;
canvas.repaint(); canvas.repaint();
@ -196,6 +200,7 @@ public class WindowOfFreeMove extends JFrame {
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
map.getActualMap().getPerson().move(); 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(); canvas.repaint();
} }
}); });