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 maps; private Map actualMap; private SaveManager saveManager; private WindowOfFreeMove window; private LoadManager loadManager; public MapManager(LinkedList 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 getMaps() { return maps; } public WindowOfFreeMove getWindow() { return window; } public LoadManager getLoadManager() { return loadManager; } public void createMap() { for (int i = 0; i < actualMap.getMapArray().length; i++) { for (int j = 0; j < actualMap.getMapArray().length; j++) { if (actualMap.getMapArray()[i][j] == TypeOfCell.BLOCK && !actualMap.isBusyCell(i, j)) { actualMap.getBlocks().add(new Block(i, j, actualMap.getImageCell(), 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().setSize(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().setSize(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().setSize(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().setSize(CELL_SIZE); actualMap.getPerson().setDirectionSight(1); break; default: actualMap.getPerson().set(-1, -1); break; } 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.setBounds(0, 0, CELL_SIZE * size[0], CELL_SIZE * size[1]); } }