2020-04-03 16:05:37 +02:00

171 lines
6.0 KiB
Java

package ruscz.game.gameOfProd.Windows;
import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.*;
public class Camera {
private WindowOfFreeMove window;
private WindowOfFreeMove.Canvas canvas;
private boolean isBound;
private int endX;
private int endY;
private double time;
private int startX;
private int startY;
private boolean isReturning;
private static int count = 0;
private boolean isStopped;
public Camera(WindowOfFreeMove window, WindowOfFreeMove.Canvas canvas) {
this.window = window;
this.canvas = canvas;
this.isBound = true;
}
public void moveCamera() {
if (isBound) {
moveBoundCamera();
}
else {
if (!isStopped)
moveFreeCamera();
}
}
public void moveBoundCamera() {
int cameraOffsetX = (-1) * CELL_SIZE * (window.getMapManager().getActualMap().getPerson().getX() - WINDOW_WIDTH / 2);
int cameraOffsetY = (-1) * CELL_SIZE * (window.getMapManager().getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2);
int direction = window.getMapManager().getActualMap().getPerson().getDirectionMoveLast();
int size = (-1) * window.getMapManager().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 (!window.getMapManager().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;
}
if (WINDOW_WIDTH > CANVAS_WIDTH) {
cameraOffsetX = (WINDOW_WIDTH - CANVAS_WIDTH) * CELL_SIZE / 2;
Xsize = 0;
}
if (WINDOW_HEIGHT > CANVAS_HEIGHT) {
cameraOffsetY = (WINDOW_HEIGHT - CANVAS_HEIGHT) * CELL_SIZE / 2;
Ysize = 0;
}
canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize);
}
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;
if (Math.abs(distanceX) < CELL_SIZE && Math.abs(distanceY) < CELL_SIZE) {
isBound = true;
isReturning = false;
return;
}
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) boundCamera();
}
public void returnFreeCamera() {
setCameraStart();
endX = (-1) * CELL_SIZE * (window.getMapManager().getActualMap().getPerson().getX() - WINDOW_WIDTH / 2);
endY = (-1) * CELL_SIZE * (window.getMapManager().getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2);
if (endX > 0) {
endX = 0;
}
else if (CELL_SIZE * WINDOW_WIDTH - endX > CELL_SIZE * CANVAS_WIDTH) {
endX = (-1) * (CELL_SIZE * CANVAS_WIDTH - CELL_SIZE * WINDOW_WIDTH);
}
if (endY > 0) {
endY = 0;
}
else if (CELL_SIZE * WINDOW_WIDTH - endY > CELL_SIZE * CANVAS_WIDTH) {
endY = (-1) * (CELL_SIZE * CANVAS_HEIGHT - CELL_SIZE * WINDOW_HEIGHT);
}
time = 1;
isReturning = true;
}
public void boundCamera() {
isBound = true;
isReturning = false;
window.setCanPersonMove(true);
window.getMapManager().getActualMap().getPerson().setInEvent(false);
}
public void setCameraStart() {
this.startX = (int) (canvas.getLocation().getX());
this.startY = (int) (canvas.getLocation().getY());
}
public void setCameraEnd(int x, int y) {
this.endX = x;
this.endY = y;
}
public void setCameraTime(double time) {
this.time = time;
}
public void setCameraBound(boolean bound) { this.isBound = bound; }
public boolean getCameraBound() { return isBound; }
public void setCameraStopped(boolean stopped) { isStopped = stopped; }
}