2020-03-03 11:47:22 +02:00

172 lines
5.0 KiB
Java

import java.util.LinkedList;
public class Map {
private String name;
private TypeOfCell[][] mapArray;
private Person person;
private LinkedList<EventMoveToCell> moveToCells;
private LinkedList<Block> block;
private boolean isBlocked;
private int idExit;
Map(int height, int width) {
name = "Spawn";
idExit = 0;
mapArray = new TypeOfCell[height][width];
person = new Person();
moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>();
isBlocked = false;
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.length; j++) {
mapArray[0][j] = type;
mapArray[mapArray.length - 1][j] = type;
}
mapArray[i][0] = type;
mapArray[i][mapArray.length - 1] = type;
}
}
Map(int height, int width, Boolean isBlocked) {
name = "Spawn";
idExit = 0;
person = new Person();
moveToCells = new LinkedList<EventMoveToCell>();
block = new LinkedList<Block>();
mapArray = new TypeOfCell[height][width];
this.isBlocked = isBlocked;
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;
}
}
public static Map findMapByName(String nameOfMap) {
return new Map(0, 0); //TODO finding map in resources and parsing
}
public void addMoveToEvent(EventMoveToCell moveToCell) {
mapArray[moveToCell.getX()][moveToCell.getY()] = TypeOfCell.MOVETO;
moveToCells.add(moveToCell);
}
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<Block> getBlocks() {
return block;
}
public LinkedList<EventMoveToCell> getMoveToCells() {
return moveToCells;
}
public void setIdExit(int id) {
this.idExit = id;
}
public void setSpawnPoint(int x, int y) {
mapArray[x][y] = TypeOfCell.SPAWNPOINT;
}
public void createMap() {
for (int i = 0; i < mapArray.length; i++) {
for (int j = 0; j < mapArray.length; j++) {
if (mapArray[i][j] == TypeOfCell.BLOCK) {
block.add(new Block(i, j));
}
if (mapArray[i][j] == TypeOfCell.SPAWNPOINT && idExit == 0) {
person.set(i, j);
}
}
}
setPersonByMoveToEvent();
}
private void setPersonByMoveToEvent() {
for (EventMoveToCell cell : moveToCells) {
if (this.idExit == cell.getId()) {
switch (cell.getDirectionOut()) {
case 1:
person.set(cell.getX(), cell.getY() - 1);
person.directionMoveLast = 0;
person.directionSight = 2;
break;
case 2:
person.set(cell.getX() + 1, cell.getY());
person.directionMoveLast = 0;
person.directionSight = 3;
break;
case 3:
person.set(cell.getX(), cell.getY() + 1);
person.directionMoveLast = 0;
person.directionSight = 0;
break;
case 4:
person.set(cell.getX() - 1, cell.getY());
person.directionMoveLast = 0;
person.directionSight = 1;
break;
default:
person.set(-1, -1);
break;
}
}
}
}
boolean isBusyCell(int x, int y) {
for (Block cell : block) {
if (cell.isInBlock(x, y))
return true;
}
return false;
}
boolean isCellMoveTo(int x, int y) {
for (EventMoveToCell cell : moveToCells)
if (x == cell.getX() && y == cell.getY() && cell.getClass().getName().equals("EventMoveToCell"))
return true;
return false;
}
EventMoveToCell findEventByXY(int x, int y) {
for (EventMoveToCell cell : moveToCells)
if (x == cell.getX() && y == cell.getY())
return cell;
return moveToCells.getFirst();
}
}