517 lines
19 KiB
Java
517 lines
19 KiB
Java
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;
|
|
|
|
/**
|
|
* This class hold all information about map that is loaded into application
|
|
*/
|
|
public class Map {
|
|
private String name;
|
|
private final TypeOfCell[][] mapArray;
|
|
private final Person person;
|
|
private final LinkedList<EventMoveToCell> moveToCells;
|
|
private final LinkedList<Block> block;
|
|
private final LinkedList<InteractionCell> interactionCells;
|
|
private final LinkedList<TriggerCell> triggerCells;
|
|
private final LinkedList<DoorCell> doorCells;
|
|
private final LinkedList<ChestCell> chestCells;
|
|
private LinkedList<Npc> npcs;
|
|
private int idExit;
|
|
private final ImageCell imageCell;
|
|
private int[] canvasSize;
|
|
private final 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();
|
|
}
|
|
|
|
/**
|
|
* @param nameOfMap is map name that is being searched for
|
|
* @param window is reference for WindowOfFreeMove
|
|
* @return found map
|
|
* Finds map by name in a list of loaded maps
|
|
*/
|
|
public static Map findMapByName(String nameOfMap, WindowOfFreeMove window) {
|
|
LinkedList<Map> mapLinkedList = window.getMapLinkedList();
|
|
for (Map map : mapLinkedList) {
|
|
if (map.getName().equals(nameOfMap))
|
|
return map;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param name is map file name
|
|
* @param window is reference for WindowOfFreeMove
|
|
* @return loaded map
|
|
* Creates map and prepares all fields, then passes map on loadMap
|
|
*/
|
|
public static Map loadMapFromFile(String name, WindowOfFreeMove window) {
|
|
try {
|
|
File file = new File("src/main/resources/maps/" + WindowOfFreeMove.getCHAPTER() + "/" + 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()) {
|
|
String data = scanner.nextLine();
|
|
loadMap(map, data, size, y, window);
|
|
y++;
|
|
}
|
|
return map;
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* @param map is map being loaded
|
|
* @param data is line of map file to be parsed
|
|
* @param size is array of map's size in cells
|
|
* @param y is current Y value of line being parsed
|
|
* @param window is reference for WindowOfFreeMove
|
|
* Parses line of text into corresponding cells with attributes and populates corresponding lists wit them
|
|
*/
|
|
public static void loadMap(Map map, String data, String[] size, int y, WindowOfFreeMove window) {
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @param map is map being linked
|
|
* @param window is reference on WindowOfFreeMove
|
|
* Parses MOVETO and DOORTO cells, which link different maps together
|
|
*/
|
|
public void linkMaps(Map map, WindowOfFreeMove window) {
|
|
try {
|
|
File dialogFile = new File("src/main/resources/maps/" + WindowOfFreeMove.getCHAPTER() + "/" + 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();
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* @param moveToCell is MOVETO cell being added
|
|
* Updates map's cell matrix and adds cell to corresponding list
|
|
*/
|
|
public void addMoveToEvent(EventMoveToCell moveToCell) {
|
|
mapArray[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
|
|
moveToCells.add(moveToCell);
|
|
}
|
|
|
|
/**
|
|
* @param interactionCell is INTER cell being added
|
|
* Updates map's cell matrix and adds cell to corresponding list
|
|
*/
|
|
public void addInteractionEvent(InteractionCell interactionCell) {
|
|
mapArray[interactionCell.getX()][interactionCell.getY()] = TypeOfCell.EVENT;
|
|
interactionCells.add(interactionCell);
|
|
}
|
|
|
|
/**
|
|
* @param triggerCell is TRIGGER cell being added
|
|
* Updates map's cell matrix and adds cell to corresponding list
|
|
*/
|
|
public void addInteractionEvent(TriggerCell triggerCell) {
|
|
mapArray[triggerCell.getX()][triggerCell.getY()] = TypeOfCell.EVENT;
|
|
triggerCells.add(triggerCell);
|
|
}
|
|
|
|
/**
|
|
* @param doorCell is DOOR(TO) cell being added
|
|
* Updates map's cell matrix and adds cell to corresponding list
|
|
*/
|
|
public void addInteractionEvent(DoorCell doorCell) {
|
|
mapArray[doorCell.getX()][doorCell.getY()] = TypeOfCell.EVENT;
|
|
doorCells.add(doorCell);
|
|
}
|
|
|
|
/**
|
|
* @param chestCell is CHEST cell being added
|
|
* Updates map's cell matrix and adds cell to corresponding list
|
|
*/
|
|
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<Npc> 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<Block> getBlocks() {
|
|
return block;
|
|
}
|
|
|
|
public LinkedList<EventMoveToCell> getMoveToCells() {
|
|
return moveToCells;
|
|
}
|
|
|
|
public LinkedList<InteractionCell> getInteractionEventCells() {
|
|
return interactionCells;
|
|
}
|
|
|
|
public LinkedList<TriggerCell> getTriggerCells() { return triggerCells; }
|
|
|
|
public LinkedList<DoorCell> getDoorCells() { return doorCells; }
|
|
|
|
public LinkedList<ChestCell> 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 void setCanvasSize(int[] size) { canvasSize = size; }
|
|
|
|
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 isTriggerCell(int x, int y) {
|
|
for (TriggerCell cell : triggerCells)
|
|
if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("ruscz.game.gameOfProd.Cells.TriggerCell"))
|
|
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 EventMoveToCell findMoveToByXY(int x, int y) {
|
|
for (EventMoveToCell cell : moveToCells)
|
|
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 Cell findCellByXY(int x, int y) {
|
|
Cell cell;
|
|
cell = findBlockByXY(x, y);
|
|
if (cell == null) cell = findInteractionByXY(x, y);
|
|
if (cell == null) cell = findTriggerByXY(x, y);
|
|
if (cell == null) cell = findChestByXY(x, y);
|
|
if (cell == null) cell = findDoorByXY(x, y);
|
|
if (cell == null) cell = findMoveToByXY(x, y);
|
|
return cell;
|
|
}
|
|
|
|
/**
|
|
* @param x is starting X value
|
|
* @param y is starting Y value
|
|
* @param directionSight is direction of hero's sight
|
|
* @return array of new X & Y coordinates
|
|
* Translates direction of sight into new coords
|
|
*/
|
|
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;
|
|
}
|
|
}
|