MoveCamera in MapPainter
This commit is contained in:
parent
76b7d7c8f1
commit
f1d32226bd
@ -7,11 +7,15 @@ import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
|||||||
|
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.*;
|
||||||
|
|
||||||
public class MapPainter {
|
public class MapPainter {
|
||||||
|
private WindowOfFreeMove.Canvas canvas;
|
||||||
private LinkedList<Map> maps;
|
private LinkedList<Map> maps;
|
||||||
private Map actualMap;
|
private Map actualMap;
|
||||||
|
|
||||||
public MapPainter(LinkedList<Map> maps) {
|
public MapPainter(LinkedList<Map> maps, WindowOfFreeMove.Canvas canvas) {
|
||||||
|
this.canvas = canvas;
|
||||||
this.maps = maps;
|
this.maps = maps;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,28 +56,28 @@ public class MapPainter {
|
|||||||
actualMap.getPerson().set(cell.getX(), cell.getY() - 1);
|
actualMap.getPerson().set(cell.getX(), cell.getY() - 1);
|
||||||
actualMap.getPerson().setIsMoving(true);
|
actualMap.getPerson().setIsMoving(true);
|
||||||
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_UP);
|
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_UP);
|
||||||
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
|
actualMap.getPerson().setSize(CELL_SIZE);
|
||||||
actualMap.getPerson().setDirectionSight(2);
|
actualMap.getPerson().setDirectionSight(2);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
actualMap.getPerson().set(cell.getX() + 1, cell.getY());
|
actualMap.getPerson().set(cell.getX() + 1, cell.getY());
|
||||||
actualMap.getPerson().setIsMoving(true);
|
actualMap.getPerson().setIsMoving(true);
|
||||||
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT);
|
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT);
|
||||||
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
|
actualMap.getPerson().setSize(CELL_SIZE);
|
||||||
actualMap.getPerson().setDirectionSight(3);
|
actualMap.getPerson().setDirectionSight(3);
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
actualMap.getPerson().set(cell.getX(), cell.getY() + 1);
|
actualMap.getPerson().set(cell.getX(), cell.getY() + 1);
|
||||||
actualMap.getPerson().setIsMoving(true);
|
actualMap.getPerson().setIsMoving(true);
|
||||||
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN);
|
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN);
|
||||||
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
|
actualMap.getPerson().setSize(CELL_SIZE);
|
||||||
actualMap.getPerson().setDirectionSight(0);
|
actualMap.getPerson().setDirectionSight(0);
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
actualMap.getPerson().set(cell.getX() - 1, cell.getY());
|
actualMap.getPerson().set(cell.getX() - 1, cell.getY());
|
||||||
actualMap.getPerson().setIsMoving(true);
|
actualMap.getPerson().setIsMoving(true);
|
||||||
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT);
|
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT);
|
||||||
actualMap.getPerson().setSize(WindowOfFreeMove.CELL_SIZE);
|
actualMap.getPerson().setSize(CELL_SIZE);
|
||||||
actualMap.getPerson().setDirectionSight(1);
|
actualMap.getPerson().setDirectionSight(1);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -83,4 +87,48 @@ public class MapPainter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void moveCamera() {
|
||||||
|
int cameraOffsetX = (-1) * CELL_SIZE * (getActualMap().getPerson().getX() - WINDOW_WIDTH / 2);
|
||||||
|
int cameraOffsetY = (-1) * CELL_SIZE * (getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2);
|
||||||
|
int direction = getActualMap().getPerson().getDirectionMoveLast();
|
||||||
|
int size = -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 (!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 || cameraOffsetY > 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -186,11 +186,11 @@ public class WindowOfFreeMove extends JFrame {
|
|||||||
m4.linkMaps(m4, this);
|
m4.linkMaps(m4, this);
|
||||||
m5.linkMaps(m5, this);
|
m5.linkMaps(m5, this);
|
||||||
|
|
||||||
map = new MapPainter(mapLinkedList);
|
map = new MapPainter(mapLinkedList, canvas);
|
||||||
|
|
||||||
setMap(m5);
|
setMap(m5);
|
||||||
|
|
||||||
moveCamera();
|
map.moveCamera();
|
||||||
|
|
||||||
map.createMap();
|
map.createMap();
|
||||||
isReadyToPaint = true;
|
isReadyToPaint = true;
|
||||||
@ -200,14 +200,14 @@ 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();
|
||||||
moveCamera();
|
map.moveCamera();
|
||||||
canvas.repaint();
|
canvas.repaint();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
timer.start();
|
timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
class Canvas extends JPanel {
|
public class Canvas extends JPanel {
|
||||||
@Override
|
@Override
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
if (isReadyToPaint) {
|
if (isReadyToPaint) {
|
||||||
@ -234,50 +234,6 @@ 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 || cameraOffsetY > 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 {
|
||||||
Thread.sleep(tm);
|
Thread.sleep(tm);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user