added free camera movement

This commit is contained in:
musilto9 2020-03-24 15:25:51 +01:00
parent 65b28ffc62
commit e49432cbed
5 changed files with 67 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import ruscz.game.gameOfProd.Dialog.Dialog;
import ruscz.game.gameOfProd.Image.ImageCell; import ruscz.game.gameOfProd.Image.ImageCell;
import ruscz.game.gameOfProd.Image.ImageDialog; import ruscz.game.gameOfProd.Image.ImageDialog;
import ruscz.game.gameOfProd.Map.Map; import ruscz.game.gameOfProd.Map.Map;
import ruscz.game.gameOfProd.Map.MapPainter;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.awt.*; import java.awt.*;
@ -16,12 +17,14 @@ public class InteractionCell extends Cell {
private ruscz.game.gameOfProd.Dialog.Dialog dialog; private ruscz.game.gameOfProd.Dialog.Dialog dialog;
private Scanner scanner; private Scanner scanner;
private ImageCell imageCell; private ImageCell imageCell;
private WindowOfFreeMove window;
public InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell) { public InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window) {
super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE);
this.message = message; this.message = message;
this.dialog = dialog; this.dialog = dialog;
this.imageCell = imageCell; this.imageCell = imageCell;
this.window = window;
} }
public void startEvent() { public void startEvent() {
@ -43,13 +46,22 @@ public class InteractionCell extends Cell {
public void readNextLine() { public void readNextLine() {
if (scanner.hasNextLine() && !dialog.getTimerIsRunning()) { if (scanner.hasNextLine() && !dialog.getTimerIsRunning()) {
String data = scanner.nextLine(); String data = scanner.nextLine();
if (data.equals("<end>")) if (data.equals("<end>")) {
window.getMapPainter().setCameraBound(true);
endEvent(); endEvent();
}
else if (data.contains("<image>")) { else if (data.contains("<image>")) {
String[] imageName = data.split(" "); String[] imageName = data.split(" ");
dialog.setImageDialog(new ImageDialog(imageName[1])); dialog.setImageDialog(new ImageDialog(imageName[1]));
readNextLine(); readNextLine();
} }
else if (data.contains("<cameraFree>")) {
String[] cameraInfo = data.split(" ");
window.getMapPainter().setCameraStart();
window.getMapPainter().setCameraBound(false);
window.getMapPainter().setCameraEnd(Integer.parseInt(cameraInfo[1]), Integer.parseInt(cameraInfo[2]));
window.getMapPainter().setCameraSpeed(Integer.parseInt(cameraInfo[3]));
}
else { else {
if (data.contains("<br/>")) if (data.contains("<br/>"))
data = "<html>" + data + "</html>"; data = "<html>" + data + "</html>";

View File

@ -9,8 +9,8 @@ import java.awt.*;
public class TriggerCell extends InteractionCell { public class TriggerCell extends InteractionCell {
private boolean isActive; private boolean isActive;
public TriggerCell(int x, int y, String message, Dialog dialog, ImageCell imageCell) { public TriggerCell(int x, int y, String message, Dialog dialog, ImageCell imageCell, WindowOfFreeMove window) {
super(x, y, message, dialog, imageCell); super(x, y, message, dialog, imageCell, window);
this.isActive = true; this.isActive = true;
} }

View File

@ -141,11 +141,11 @@ public class Map {
} else if (line[x].contains("INTER")) { } else if (line[x].contains("INTER")) {
ImageCell imageCell = new ImageCell(); ImageCell imageCell = new ImageCell();
String[] event = line[x].split(","); String[] event = line[x].split(",");
map.addInteractionEvent(new InteractionCell(x, y, event[1], new Dialog(window.getDialogBar()), map.getImageCell())); map.addInteractionEvent(new InteractionCell(x, y, event[1], new Dialog(window.getDialogBar()), map.getImageCell(), window));
} else if (line[x].contains("TRIGGER")) { } else if (line[x].contains("TRIGGER")) {
ImageCell imageCell = new ImageCell(); ImageCell imageCell = new ImageCell();
String[] trigger = line[x].split(","); String[] trigger = line[x].split(",");
map.addInteractionEvent(new TriggerCell(x, y, trigger[1], new Dialog(window.getDialogBar()), map.getImageCell())); map.addInteractionEvent(new TriggerCell(x, y, trigger[1], new Dialog(window.getDialogBar()), map.getImageCell(), window));
} else if (line[x].equals("SPAWN")) { } else if (line[x].equals("SPAWN")) {
map.setSpawnPoint(x, y); map.setSpawnPoint(x, y);
} }

View File

@ -14,10 +14,17 @@ public class MapPainter {
private WindowOfFreeMove.Canvas canvas; private WindowOfFreeMove.Canvas canvas;
private LinkedList<Map> maps; private LinkedList<Map> maps;
private Map actualMap; private Map actualMap;
private boolean isCameraBound;
private int cameraEndX;
private int cameraEndY;
private int cameraSpeed;
private int cameraStartX;
private int cameraStartY;
public MapPainter(LinkedList<Map> maps, WindowOfFreeMove.Canvas canvas) { public MapPainter(LinkedList<Map> maps, WindowOfFreeMove.Canvas canvas) {
this.canvas = canvas; this.canvas = canvas;
this.maps = maps; this.maps = maps;
this.isCameraBound = true;
} }
public void setMap(Map map) { public void setMap(Map map) {
@ -90,6 +97,15 @@ public class MapPainter {
} }
public void moveCamera() { public void moveCamera() {
if (isCameraBound) {
moveBoundCamera();
}
else {
moveFreeCamera();
}
}
public void moveBoundCamera() {
int cameraOffsetX = (-1) * CELL_SIZE * (getActualMap().getPerson().getX() - WINDOW_WIDTH / 2); int cameraOffsetX = (-1) * CELL_SIZE * (getActualMap().getPerson().getX() - WINDOW_WIDTH / 2);
int cameraOffsetY = (-1) * CELL_SIZE * (getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2); int cameraOffsetY = (-1) * CELL_SIZE * (getActualMap().getPerson().getY() - WINDOW_HEIGHT / 2);
int direction = getActualMap().getPerson().getDirectionMoveLast(); int direction = getActualMap().getPerson().getDirectionMoveLast();
@ -133,7 +149,39 @@ public class MapPainter {
canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize); canvas.setLocation(cameraOffsetX + Xsize, cameraOffsetY + Ysize);
} }
public void moveFreeCamera() {
int coordX = (int) (canvas.getLocation().getX());
int coordY = (int) (canvas.getLocation().getY());
if (coordX != cameraEndX || coordY != cameraEndY) {
int distanceX = cameraEndX - cameraStartX;
int distanceY = cameraEndY - cameraStartY;
int stepX = distanceX * cameraSpeed / CELL_SIZE;
int stepY = distanceY * cameraSpeed / CELL_SIZE;
canvas.setLocation(coordX + stepX, coordY + stepY);
// System.out.println("start: " + cameraStartX + " " + cameraStartY + ", end: " + cameraEndX + " " + cameraEndY);
System.out.println("distanceX: " + distanceX + ", distanceY: " + distanceY);
System.out.println("speed: " + cameraSpeed);
System.out.println("stepX: " + stepX + ", stepY: " + stepY);
}
}
public void repaintCanvas() { public void repaintCanvas() {
canvas.repaint(); canvas.repaint();
} }
public void setCameraStart() {
this.cameraStartX = (int) (canvas.getLocation().getX());
this.cameraStartY = (int) (canvas.getLocation().getY());
}
public void setCameraEnd(int x, int y) {
this.cameraEndX = x;
this.cameraEndY = y;
}
public void setCameraSpeed(int speed) {
this.cameraSpeed = speed;
}
public void setCameraBound(boolean bound) { this.isCameraBound = bound; }
} }

View File

@ -1,3 +1,4 @@
Roses are red<br/>violets are blue Roses are red<br/>violets are blue
<cameraFree> -40 0 1
unexpected "{"<br/>on line 32 unexpected "{"<br/>on line 32
<end> <end>