144 lines
5.4 KiB
Java
144 lines
5.4 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.*;
|
|
|
|
/**
|
|
* This class manages all currently loaded maps and transfers hero between maps
|
|
*/
|
|
public class MapManager {
|
|
private final WindowOfFreeMove.Canvas canvas;
|
|
private final LinkedList<Map> maps;
|
|
private Map actualMap;
|
|
private final SaveManager saveManager;
|
|
private final WindowOfFreeMove window;
|
|
private final 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);
|
|
}
|
|
|
|
/**
|
|
* @param map is map to be set
|
|
* Sets map as the one that is currently used, if that map is not present in map list, the map is added
|
|
*/
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Sets person on a new map based on SPAWN cell
|
|
*/
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* Sets person on a new map based on MOVETO or DOORTO cell
|
|
*/
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* @param size is array of new sizes for canvas
|
|
* Sets canvas size based on current map
|
|
*/
|
|
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]);
|
|
}
|
|
}
|