2020-06-04 12:37:57 +02:00

127 lines
4.9 KiB
Java

package ruscz.game.gameOfProd.Map;
import ruscz.game.gameOfProd.Cells.Block;
import ruscz.game.gameOfProd.Cells.EventMoveToCell;
import ruscz.game.gameOfProd.Cells.TypeOfCell;
import ruscz.game.gameOfProd.Save.LoadManager;
import ruscz.game.gameOfProd.Save.SaveManager;
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
import java.awt.*;
import java.util.LinkedList;
import static ruscz.game.gameOfProd.Windows.WindowOfFreeMove.*;
public class MapManager {
private WindowOfFreeMove.Canvas canvas;
private LinkedList<Map> maps;
private Map actualMap;
private SaveManager saveManager;
private WindowOfFreeMove window;
private LoadManager loadManager;
public MapManager(LinkedList<Map> maps, WindowOfFreeMove.Canvas canvas, WindowOfFreeMove window) {
this.canvas = canvas;
this.maps = maps;
this.window = window;
this.saveManager = new SaveManager(this);
this.loadManager = new LoadManager(this);
}
public void setMap(Map map) {
if (maps.contains(map)) {
actualMap = map;
WindowOfFreeMove.setCanvasSize(map.getCanvasSize());
resizeCanvas(map.getCanvasSize());
} else {
maps.add(map);
actualMap = map;
WindowOfFreeMove.setCanvasSize(map.getCanvasSize());
resizeCanvas(map.getCanvasSize());
}
}
public Map getActualMap() {
return actualMap;
}
public LinkedList<Map> getMaps() { return maps; }
public WindowOfFreeMove getWindow() { return window; }
public LoadManager getLoadManager() { return loadManager; }
public SaveManager getSaveManager() {
return saveManager;
}
public void createMap() {
for (int i = 0; i < actualMap.getMapArray().length; i++) {
for (int j = 0; j < actualMap.getMapArray()[0].length; j++) {
if (actualMap.getMapArray()[i][j] == TypeOfCell.BLOCK && !actualMap.isBusyCell(i, j)) {
actualMap.getBlocks().add(new Block(i, j, actualMap, true));
}
if (actualMap.getMapArray()[i][j] == TypeOfCell.SPAWNPOINT && actualMap.getIdExit() == 0) {
actualMap.getPerson().set(i, j);
}
}
}
setPersonByMoveToEvent();
}
private void setPersonByMoveToEvent() {
for (EventMoveToCell cell : actualMap.getMoveToCells()) {
if (this.actualMap.getIdExit() == cell.getId()) {
switch (cell.getDirectionOut()) {
case 1:
actualMap.getPerson().set(cell.getX(), cell.getY() - 1);
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_UP);
actualMap.getPerson().setDistance(CELL_SIZE);
actualMap.getPerson().setDirectionSight(2);
break;
case 2:
actualMap.getPerson().set(cell.getX() + 1, cell.getY());
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_RIGHT);
actualMap.getPerson().setDistance(CELL_SIZE);
actualMap.getPerson().setDirectionSight(3);
break;
case 3:
actualMap.getPerson().set(cell.getX(), cell.getY() + 1);
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_DOWN);
actualMap.getPerson().setDistance(CELL_SIZE);
actualMap.getPerson().setDirectionSight(0);
break;
case 4:
actualMap.getPerson().set(cell.getX() - 1, cell.getY());
actualMap.getPerson().setIsMoving(true);
actualMap.getPerson().setDirectionMoveLast(WindowOfFreeMove.KEY_LEFT);
actualMap.getPerson().setDistance(CELL_SIZE);
actualMap.getPerson().setDirectionSight(1);
break;
default:
actualMap.getPerson().set(-1, -1);
break;
}
if (actualMap.isDoor(cell.getX(), cell.getY()))
actualMap.findDoorByXY(cell.getX(), cell.getY()).setOpen(true);
saveManager.saveGame();
}
}
}
public void repaintCanvas() {
canvas.repaint();
}
public void resizeCanvas(int[] size) {
canvas.setPreferredSize(new Dimension(
CELL_SIZE * size[0],
CELL_SIZE * size[1]));
canvas.setSize(CELL_SIZE * size[0], CELL_SIZE * size[1]);
}
}