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.Windows.WindowOfFreeMove; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.lang.reflect.Type; 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 npcs; private boolean isBlocked; private int idExit; private ImageCell imageCell; private static Scanner scanner; public Map(int height, int width) { name = "Spawn"; idExit = 0; mapArray = new TypeOfCell[height][width]; moveToCells = new LinkedList(); interactionCells = new LinkedList(); triggerCells = new LinkedList(); npcs = new LinkedList<>(); block = new LinkedList(); isBlocked = false; imageCell = new ImageCell(); TypeOfCell type; type = TypeOfCell.EMPTY; for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray.length; j++) { mapArray[0][j] = type; mapArray[mapArray.length - 1][j] = type; } mapArray[i][0] = type; mapArray[i][mapArray.length - 1] = type; } person = new Person(this); } public Map(int height, int width, Boolean isBlocked) { name = "Spawn"; idExit = 0; moveToCells = new LinkedList(); block = new LinkedList(); interactionCells = new LinkedList(); triggerCells = new LinkedList(); mapArray = new TypeOfCell[height][width]; npcs = new LinkedList<>(); this.isBlocked = isBlocked; imageCell = new ImageCell(); TypeOfCell type; if (isBlocked) type = TypeOfCell.BLOCK; else type = TypeOfCell.EMPTY; for (int i = 0; i < mapArray.length; i++) { for (int j = 0; j < mapArray[0].length; j++) { mapArray[0][j] = type; mapArray[mapArray.length - 1][j] = type; } mapArray[i][0] = type; mapArray[i][mapArray[0].length - 1] = type; } person = new Person(this); } public Map(TypeOfCell[][] mapArray) { idExit = 0; moveToCells = new LinkedList(); interactionCells = new LinkedList(); triggerCells = new LinkedList(); block = new LinkedList(); isBlocked = false; imageCell = new ImageCell(); npcs = new LinkedList<>(); this.mapArray = mapArray; person = new Person(this); } 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 dialogFile = new File("src/main/resources/maps/" + name + ".txt"); scanner = new Scanner(dialogFile); 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 + "-tileset"); LinkedList listOfMoveToCells = new LinkedList<>(); 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 = null; if (line[x].equals("BLOCK")) { cellType = TypeOfCell.BLOCK; map.getMapArray()[x][y] = cellType; map.getBlocks().add(new Block(x, y, map.getImageCell())); } else if (line[x].equals("EMPTY")) { cellType = TypeOfCell.EMPTY; map.getMapArray()[x][y] = cellType; } else if (line[x].contains("INTER")) { ImageCell imageCell = new ImageCell(); String[] event = line[x].split(","); map.addInteractionEvent(new InteractionCell(x, y, event[1], new Dialog(window.getDialogBar()), map.getImageCell())); } else if (line[x].contains("TRIGGER")) { ImageCell imageCell = new ImageCell(); String[] trigger = line[x].split(","); map.addInteractionEvent(new TriggerCell(x, y, trigger[1], new Dialog(window.getDialogBar()), map.getImageCell())); } else if (line[x].equals("SPAWN")) { map.setSpawnPoint(x, y); } } } public void linkMaps(Map map, WindowOfFreeMove window) { try { File dialogFile = new File("src/main/resources/maps/" + 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")) { String[] moveTo = line[x].split(","); map.addMoveToEvent(new EventMoveToCell(Integer.parseInt(moveTo[1]), Integer.parseInt(moveTo[2]), x, y, findMapByName(moveTo[3], window), window)); } } 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 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 LinkedList getBlocks() { return block; } public LinkedList getMoveToCells() { return moveToCells; } public LinkedList getInteractionEventCells() { return interactionCells; } public void setIdExit(int id) { this.idExit = id; } public void setSpawnPoint(int x, int y) { mapArray[x][y] = TypeOfCell.SPAWNPOINT; } public ImageCell getImageCell() { return imageCell; } public TypeOfCell[][] getMapArray() { return mapArray; } public boolean isBusyCell(int x, int y) { // for (Block cell : block) { // if (cell.isInBlock(x, y)) // 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 EventMoveToCell findEventByXY(int x, int y) { for (EventMoveToCell cell : moveToCells) if (x == cell.getX() && y == cell.getY()) return cell; return moveToCells.getFirst(); } public InteractionCell findInteractionByXY(int x, int y, int directionSight) { switch (directionSight) { case 1: x--; break; case 3: x++; break; case 2: y--; break; case 0: y++; break; } for (InteractionCell cell : interactionCells) if (x == cell.getX() && y == cell.getY()) return cell; return interactionCells.getFirst(); } public TriggerCell findTriggerByXY(int x, int y) { for (TriggerCell cell : triggerCells) if (x == cell.getX() && y == cell.getY()) return cell; return triggerCells.getFirst(); } }