package ruscz.game.gameOfProd.Map; import ruscz.game.gameOfProd.Cells.*; import ruscz.game.gameOfProd.Dialog.Dialog; import ruscz.game.gameOfProd.Image.ImageCell; import ruscz.game.gameOfProd.Sounds.SoundManager; import ruscz.game.gameOfProd.Windows.WindowOfFreeMove; import java.io.File; import java.io.FileNotFoundException; import java.util.LinkedList; import java.util.Scanner; public class Map { private String name; private TypeOfCell[][] mapArray; private Person person; private LinkedList moveToCells; private LinkedList block; private LinkedList interactionCells; private LinkedList triggerCells; private LinkedList doorCells; private LinkedList chestCells; private LinkedList npcs; private int idExit; private ImageCell imageCell; private int[] canvasSize; private SoundManager soundManager; private static Scanner scanner; private static String mapWithSpawn = ""; public Map(TypeOfCell[][] mapArray) { idExit = -1; moveToCells = new LinkedList<>(); interactionCells = new LinkedList<>(); triggerCells = new LinkedList<>(); doorCells = new LinkedList<>(); block = new LinkedList<>(); imageCell = new ImageCell(); chestCells = new LinkedList<>(); npcs = new LinkedList<>(); this.mapArray = mapArray; person = new Person(this); canvasSize = new int[2]; soundManager = new SoundManager(); } public static Map findMapByName(String nameOfMap, WindowOfFreeMove window) { LinkedList mapLinkedList = window.getMapLinkedList(); for (Map map : mapLinkedList) { if (map.getName().equals(nameOfMap)) return map; } return null; } public static Map loadMapFromFile(String name, WindowOfFreeMove window) { try { File file = new File("src/main/resources/maps/" + WindowOfFreeMove.CHAPTER + "/" + name + ".txt"); scanner = new Scanner(file); String firstLine = scanner.nextLine(); String[] size = firstLine.split(" "); TypeOfCell[][] mapArray = new TypeOfCell[Integer.parseInt(size[0])][Integer.parseInt(size[1])]; Map map = new Map(mapArray); map.setName(name); map.npcs = new LinkedList<>(); map.getImageCell().readMapFile(name); map.getSoundManager().loadSoundMap(name); map.canvasSize[0] = Integer.parseInt(size[0]); map.canvasSize[1] = Integer.parseInt(size[1]); int y = 0; while (scanner.hasNextLine()) { loadMap(map, size, y, window); y++; } return map; } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } public static void loadMap(Map map, String[] size, int y, WindowOfFreeMove window) { String data = scanner.nextLine(); String[] line = data.split(" "); for (int x = 0; x < Integer.parseInt(size[0]); x++) { TypeOfCell cellType; if (line[x].contains("BLOCK") || line[x].equals("block")) { cellType = TypeOfCell.BLOCK; map.getMapArray()[x][y] = cellType; String[] block = line[x].split(","); if (block[0].equals("BLOCK")) if (block.length == 2) { map.getBlocks().add(new Block(x, y, map, block[1], true)); } else { map.getBlocks().add(new Block(x, y, map, true)); } else { map.getBlocks().add(new Block(x, y, map, false)); } } else if (line[x].equals("EMPTY")) { cellType = TypeOfCell.EMPTY; map.getMapArray()[x][y] = cellType; } else if (line[x].contains("INTER") || line[x].contains("inter")) { String[] event = line[x].split(","); if (line[x].contains("INTER")) map.addInteractionEvent(new InteractionCell(x, y, event[1], new Dialog(window.getDialogBar()), map, window, true)); else map.addInteractionEvent(new InteractionCell(x, y, event[1], new Dialog(window.getDialogBar()), map, window, false)); } else if (line[x].contains("TRIGGER")) { String[] trigger = line[x].split(","); if (trigger.length == 2) map.addInteractionEvent(new TriggerCell(x, y, trigger[1], new Dialog(window.getDialogBar()), map, window)); else map.addInteractionEvent(new TriggerCell(x, y, trigger[1], new Dialog(window.getDialogBar()), map, window, Boolean.parseBoolean(trigger[2]))); } else if (line[x].equals("SPAWN")) { map.setIdExit(0); map.setSpawnPoint(x, y); mapWithSpawn = map.getName(); } else if (line[x].contains("DOOR") && !line[x].contains("DOORTO")) { String[] door = line[x].split(","); if (door.length == 2) map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(door[1]), map, window)); else map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(door[1]), door[2], new Dialog(window.getDialogBar()), map, window)); } else if (line[x].contains("CHEST")) { String[] chest = line[x].split(","); if (chest.length == 3) map.addInteractionEvent(new ChestCell(x, y, chest[1], new Dialog(window.getDialogBar()), map, window, Boolean.parseBoolean(chest[2]))); else map.addInteractionEvent(new ChestCell(x, y, chest[1], new Dialog(window.getDialogBar()), map, window)); } } } public void linkMaps(Map map, WindowOfFreeMove window) { try { File dialogFile = new File("src/main/resources/maps/" + WindowOfFreeMove.CHAPTER + "/" + map.getName() + ".txt"); scanner = new Scanner(dialogFile); String firstLine = scanner.nextLine(); String[] size = firstLine.split(" "); int y = 0; while (scanner.hasNextLine()) { String data = scanner.nextLine(); String[] line = data.split(" "); for (int x = 0; x < Integer.parseInt(size[0]); x++) { if (line[x].contains("MOVETO") || line[x].contains("moveto")) { String[] moveTo = line[x].split(","); if (line[x].contains("MOVETO")) map.addMoveToEvent(new EventMoveToCell(Integer.parseInt(moveTo[1]), Integer.parseInt(moveTo[2]), x, y, findMapByName(moveTo[3], window), window, true)); else map.addMoveToEvent(new EventMoveToCell(Integer.parseInt(moveTo[1]), Integer.parseInt(moveTo[2]), x, y, findMapByName(moveTo[3], window), window, false)); } else if (line[x].contains("DOORTO")) { String[] doorTo = line[x].split(","); if (doorTo[1].equals("locked")) { map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(doorTo[2]), doorTo[3], new Dialog(window.getDialogBar()), map, window)); map.addMoveToEvent(new EventMoveToCell(Integer.parseInt(doorTo[4]), Integer.parseInt(doorTo[5]), x, y, findMapByName(doorTo[6], window), window, false)); } else { map.addInteractionEvent(new DoorCell(x, y, Boolean.parseBoolean(doorTo[1]), map, window)); map.addMoveToEvent(new EventMoveToCell(Integer.parseInt(doorTo[2]), Integer.parseInt(doorTo[3]), x, y, findMapByName(doorTo[4], window), window, false)); } } } y++; } } catch (FileNotFoundException e) { e.printStackTrace(); } } public void addMoveToEvent(EventMoveToCell moveToCell) { mapArray[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO; moveToCells.add(moveToCell); } public void addInteractionEvent(InteractionCell interactionCell) { mapArray[interactionCell.getX()][interactionCell.getY()] = TypeOfCell.EVENT; interactionCells.add(interactionCell); } public void addInteractionEvent(TriggerCell triggerCell) { mapArray[triggerCell.getX()][triggerCell.getY()] = TypeOfCell.EVENT; triggerCells.add(triggerCell); } public void addInteractionEvent(DoorCell doorCell) { mapArray[doorCell.getX()][doorCell.getY()] = TypeOfCell.EVENT; doorCells.add(doorCell); } public void addInteractionEvent(ChestCell chestCell) { mapArray[chestCell.getX()][chestCell.getY()] = TypeOfCell.EVENT; chestCells.add(chestCell); } public void setNpcs(Npc npc){ npcs.add(npc); } public LinkedList getNpcs(){ return npcs; } public void setName(String name) { this.name = name; } public String getName() { return name; } public Person getPerson() { return person; } public int getIdExit() { return idExit; } public static String getMapWithSpawn() { return mapWithSpawn; } public LinkedList getBlocks() { return block; } public LinkedList getMoveToCells() { return moveToCells; } public LinkedList getInteractionEventCells() { return interactionCells; } public LinkedList getTriggerCells() { return triggerCells; } public LinkedList getDoorCells() { return doorCells; } public LinkedList getChestCells() { return chestCells; } public void setIdExit(int id) { this.idExit = id; } public void setSpawnPoint(int x, int y) { mapArray[x][y] = TypeOfCell.SPAWNPOINT; } public int[] getSpawnPoint() { for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray.length; j++) { if (mapArray[i][j] == TypeOfCell.SPAWNPOINT) { int[] coords = {i, j}; return coords; } } } return null; } public ImageCell getImageCell() { return imageCell; } public TypeOfCell[][] getMapArray() { return mapArray; } public int[] getCanvasSize() { return canvasSize; } public SoundManager getSoundManager() { return soundManager; } public boolean isBusyCell(int x, int y) { for (Block cell : block) { if (cell.isInBlock(x, y)) return true; } return false; } public boolean isBusyCell(int x, int y, boolean half) { for (Block cell : block) { if (cell.isInBlock(x, y, half)) return true; } return false; } public boolean isCellMoveTo(int x, int y) { for (EventMoveToCell cell : moveToCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.EventMoveToCell")) return true; return false; } public boolean isInteractionCell(int x, int y) { for (InteractionCell cell : interactionCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.InteractionCell")) return true; return false; } public boolean isActiveTriggerCell(int x, int y) { for (TriggerCell cell : triggerCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.TriggerCell") && cell.getActive()) return true; return false; } public boolean isDoor(int x, int y) { for (DoorCell cell : doorCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.DoorCell")) { return true; } return false; } public boolean isClosedDoor(int x, int y) { for (DoorCell cell : doorCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.DoorCell") && !cell.isOpen()) return true; return false; } public boolean isActiveChest(int x, int y) { for (ChestCell cell : chestCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.ChestCell") && cell.isActive()) return true; return false; } public boolean isChest(int x, int y) { for (ChestCell cell : chestCells) if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.ChestCell")) return true; return false; } public EventMoveToCell findEventByXY(int x, int y) { for (EventMoveToCell cell : moveToCells) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public InteractionCell findInteractionByXY(int x, int y, int directionSight) { int[] coords = directionSightSwitch(x, y, directionSight); for (InteractionCell cell : interactionCells) if (coords[0] == cell.getX() && coords[1] == cell.getY()) return cell; return null; } public InteractionCell findInteractionByXY(int x, int y) { for (InteractionCell cell : interactionCells) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public TriggerCell findTriggerByXY(int x, int y) { for (TriggerCell cell : triggerCells) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public DoorCell findDoorByXY(int x, int y, int directionSight) { int[] coords = directionSightSwitch(x, y, directionSight); for (DoorCell cell : doorCells) if (coords[0] == cell.getX() && coords[1] == cell.getY()) return cell; return null; } public DoorCell findDoorByXY(int x, int y) { for (DoorCell cell : doorCells) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public Block findBlockByXY(int x, int y) { for (Block cell : block) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public ChestCell findChestByXY(int x, int y, int directionSight) { int[] coords = directionSightSwitch(x, y, directionSight); for (ChestCell cell : chestCells) if (coords[0] == cell.getX() && coords[1] == cell.getY()) return cell; return null; } public ChestCell findChestByXY(int x, int y) { for (ChestCell cell : chestCells) if (x == cell.getX() && y == cell.getY()) return cell; return null; } public Npc findNpcByName(String name) { for (Npc npc : npcs) { if (npc.getName().equals(name)) return npc; } return null; } public int[] directionSightSwitch(int x, int y, int directionSight) { switch (directionSight) { case 1: x--; break; case 3: x++; break; case 2: y--; break; case 0: y++; break; } int[] coords = {x, y}; return coords; } }