animated camera movement

This commit is contained in:
musilto9 2020-03-17 14:14:38 +01:00
parent 8b80967555
commit 6667781bb3
3 changed files with 74 additions and 17 deletions

View File

@ -232,6 +232,10 @@ public class Person extends Cell {
this.size = size; this.size = size;
} }
public int getSize() { return size; }
public int getDirectionMoveLast() {return directionMoveLast; }
public static void setInEvent(boolean isInEvent) { public static void setInEvent(boolean isInEvent) {
Person.isInEvent = isInEvent; Person.isInEvent = isInEvent;
} }

View File

@ -19,10 +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 = 10; public static final int CANVAS_WIDTH = 20;
public static final int CANVAS_HEIGHT = 10; public static final int CANVAS_HEIGHT = 20;
public static final int WINDOW_WIDTH = 5; public static final int WINDOW_WIDTH = 10;
public static final int WINDOW_HEIGHT = 5; public static final int WINDOW_HEIGHT = 10;
private static boolean canPersonMove; private static boolean canPersonMove;
private static int nextMove; private static int nextMove;
@ -79,7 +79,7 @@ public class WindowOfFreeMove extends JFrame {
JLayeredPane lp = getLayeredPane(); JLayeredPane lp = getLayeredPane();
canvas = new Canvas(); canvas = new Canvas();
canvas.setBackground(Color.white); canvas.setBackground(Color.black);
canvas.setPreferredSize(new Dimension( canvas.setPreferredSize(new Dimension(
CELL_SIZE * CANVAS_WIDTH, CELL_SIZE * CANVAS_WIDTH,
CELL_SIZE * CANVAS_HEIGHT)); CELL_SIZE * CANVAS_HEIGHT));
@ -190,7 +190,7 @@ 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)); moveCamera();
map.createMap(); map.createMap();
isReadyToPaint = true; isReadyToPaint = true;
@ -200,7 +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)); moveCamera();
canvas.repaint(); canvas.repaint();
} }
}); });
@ -234,6 +234,49 @@ public class WindowOfFreeMove extends JFrame {
} }
} }
public void moveCamera() {
int cameraOffsetX = (-1) * CELL_SIZE * (map.getActualMap().getPerson().getX() - WINDOW_WIDTH / 2);
int cameraOffsetY = (-1) * CELL_SIZE * (map.getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2);
int direction = map.getActualMap().getPerson().getDirectionMoveLast();
int size = -map.getActualMap().getPerson().getSize();
int Ysize = 0;
int Xsize = 0;
switch (direction) {
case WindowOfFreeMove.KEY_LEFT:
Xsize = size;
break;
case WindowOfFreeMove.KEY_RIGHT:
Xsize -= size;
break;
case WindowOfFreeMove.KEY_UP:
Ysize = size;
break;
case WindowOfFreeMove.KEY_DOWN:
Ysize -= size;
break;
}
if (!map.getActualMap().getPerson().getIsMoving()) {
Xsize = 0;
Ysize = 0;
}
if (cameraOffsetX + Xsize >= 0 || cameraOffsetX > 0) {
cameraOffsetX = 0;
Xsize = 0;
}
else if (CELL_SIZE * WINDOW_WIDTH - (cameraOffsetX + Xsize) >= CELL_SIZE * CANVAS_WIDTH || CELL_SIZE * WINDOW_WIDTH - cameraOffsetX > CELL_SIZE * CANVAS_WIDTH) {
cameraOffsetX = (-1) * (CELL_SIZE * CANVAS_WIDTH - CELL_SIZE * WINDOW_WIDTH);
Xsize = 0;
}
if (cameraOffsetY + Ysize >= 0 || cameraOffsetX > 0) {
cameraOffsetY = 0;
Ysize = 0;
}
else if (CELL_SIZE * WINDOW_HEIGHT - (cameraOffsetY + Ysize) >= CELL_SIZE * CANVAS_HEIGHT || CELL_SIZE * WINDOW_HEIGHT - cameraOffsetY > CELL_SIZE * CANVAS_HEIGHT) {
cameraOffsetY = (-1) * (CELL_SIZE * CANVAS_HEIGHT - CELL_SIZE * WINDOW_HEIGHT);
Ysize = 0;
}
canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize);
}
protected static void sleep(long tm) { protected static void sleep(long tm) {
try { try {

View File

@ -1,12 +1,22 @@
<tileset> A2_Ground.png 512 384 32 <tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32 <tileset> D_OutDoor.png 512 512 32
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 82 94 1 1 82 94 1 1 1 1 82 94 1 1 82 94 1 1 1 1 82 94 1 1 82 94 1 1
1 1 83 95 1 1 83 95 1 1 1 1 83 95 1 1 83 95 1 1 1 1 83 95 1 1 83 95 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 153 1 1 1 1 153 1 1 1 1 153 1 1 1 1 153 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 153 153 153 153 1 1 1 1 1 1 153 153 153 153 1 1 1 1 1 1 153 153 153 153 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 153 1 1 1 1 153 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 82 94 1 1 82 94 1 1 1 1 82 94 1 1 82 94 1 1
1 1 83 95 1 1 83 95 1 1 1 1 83 95 1 1 83 95 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 153 1 1 1 1 153 1 1 1 1 1 1 1
1 1 1 153 153 153 153 1 1 1 1 1 1 153 153 153 153 1 1 1
1 1 153 1 1 1 1 1 1 1 1 1 1 1 1 1 1 153 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1