added map cell loading from file

This commit is contained in:
musilto9 2020-03-15 02:41:01 +01:00
parent ff4f6036a0
commit f011048ed6
13 changed files with 281 additions and 38 deletions

View File

@ -21,7 +21,7 @@ public class ImageCell {
try {
mapBackground = new int[WindowOfFreeMove.CANVAS_WIDTH * WindowOfFreeMove.CANVAS_HEIGHT];
int count = 0;
File mapFile = new File("src/main/resources/" + name + ".txt");
File mapFile = new File("src/main/resources/maps/" + name + ".txt");
scanner = new Scanner(mapFile);
while (scanner.hasNextLine()) {
String data = scanner.nextLine();

View File

@ -1,9 +1,16 @@
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;
@ -16,8 +23,10 @@ public class Map {
private boolean isBlocked;
private int idExit;
private ImageCell imageCell;
private static Scanner scanner;
private static LinkedList<String[]> listOfMoveToCells;
Map(int height, int width) {
public Map(int height, int width) {
name = "Spawn";
idExit = 0;
mapArray = new TypeOfCell[height][width];
@ -75,8 +84,103 @@ public class Map {
person = new Person(this);
}
public Map(TypeOfCell[][] mapArray) {
idExit = 0;
moveToCells = new LinkedList<EventMoveToCell>();
interactionCells = new LinkedList<InteractionCell>();
triggerCells = new LinkedList<TriggerCell>();
block = new LinkedList<Block>();
isBlocked = false;
imageCell = new ImageCell();
this.mapArray = mapArray;
person = new Person(this);
}
public static Map findMapByName(String nameOfMap) {
return new Map(0, 0); //TODO finding map in resources and parsing
LinkedList<Map> mapLinkedList = WindowOfFreeMove.getMapLinkedList();
for (Map map : mapLinkedList) {
if (map.getName().equals(nameOfMap))
return map;
}
return mapLinkedList.getFirst();
}
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.getImageCell().readMapFile(name + "-tileset");
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(WindowOfFreeMove.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(WindowOfFreeMove.getDialogBar()), map.getImageCell()));
}
else if (line[x].equals("SPAWN")) {
map.setSpawnPoint(x, y);
}
}
}
public void linkMaps(Map map, 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(" ");
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));
}
}
y++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void addMoveToEvent(EventMoveToCell moveToCell) {

View File

@ -32,11 +32,12 @@ public class WindowOfFreeMove extends JFrame {
public static final int KEY_EVENT = 69;
private Canvas canvas;
private DialogBar dialogBar;
private static DialogBar dialogBar;
private JLayeredPane lp;
private MapPainter map;
private Timer timer;
private Timer inputTimer;
private static LinkedList<Map> mapLinkedList;
public MapPainter getMap() {
return map;
@ -51,7 +52,9 @@ public class WindowOfFreeMove extends JFrame {
}
public void setMap(Map map) {
this.map.setMap(map);
; this.
map.
setMap(map);
}
public void paintMapOnWindow() {
@ -60,6 +63,10 @@ public class WindowOfFreeMove extends JFrame {
public static void setNextMove(int nextMove) { WindowOfFreeMove.nextMove = nextMove; }
public static DialogBar getDialogBar() { return WindowOfFreeMove.dialogBar; }
public static LinkedList<Map> getMapLinkedList() { return mapLinkedList; }
public WindowOfFreeMove() {
isReadyToPaint = false;
canPersonMove = true;
@ -140,52 +147,81 @@ public class WindowOfFreeMove extends JFrame {
}
}
Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
// Map m1 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
// m1.getPerson().setImagePerson("mainpng.png", rec);
// m1.setName("1");
// m1.setSpawnPoint(1, 1);
// Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
// m2.getPerson().setImagePerson("mainpng.png", rec);
// m2.setName("2");
// Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
// m3.getPerson().setImagePerson("mainpng.png", rec);
// m3.setName("3");
// Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
// m4.getPerson().setImagePerson("mainpng.png", rec);
// m4.setName("4");
//
// m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
// m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this));
// m1.addMoveToEvent(new EventMoveToCell(5, 1, 1, 8, m5, this));
// m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar), m1.getImageCell()));
// m1.addInteractionEvent(new InteractionCell(5, 5, "dialog-test", new Dialog(dialogBar), m1.getImageCell()));
// m1.addInteractionEvent(new TriggerCell(4,6,"roses-are-red", new Dialog(dialogBar), m1.getImageCell()));
//
// m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
// m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this));
// m2.addInteractionEvent(new TriggerCell(7,2,"qa-tester", new Dialog(dialogBar), m1.getImageCell()));
//
// m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4, this));
// m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2, this));
// m3.addInteractionEvent(new TriggerCell(2,5,"boobs", new Dialog(dialogBar), m1.getImageCell()));
//
// m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1, this));
// m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this));
// m4.addInteractionEvent(new TriggerCell(3,3,"javascript", new Dialog(dialogBar), m1.getImageCell()));
Map m1 = Map.loadMapFromFile("m1", this);
m1.getPerson().setImagePerson("mainpng.png", rec);
m1.setName("1");
m1.setSpawnPoint(1, 1);
Map m2 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m1.setName("m1");
Map m2 = Map.loadMapFromFile("m2", this);
m2.getPerson().setImagePerson("mainpng.png", rec);
m2.setName("2");
Map m3 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, true);
m2.setName("m2");
Map m3 = Map.loadMapFromFile("m3", this);
m3.getPerson().setImagePerson("mainpng.png", rec);
m3.setName("3");
Map m4 = new Map(CANVAS_HEIGHT, CANVAS_WIDTH, false);
m3.setName("m3");
Map m4 = Map.loadMapFromFile("m4", this);
m4.getPerson().setImagePerson("mainpng.png", rec);
m4.setName("4");
m4.setName("m4");
m1.addMoveToEvent(new EventMoveToCell(1, 1, 8, 8, m2, this));
m1.addMoveToEvent(new EventMoveToCell(4, 3, 8, 1, m4, this));
m1.addInteractionEvent(new InteractionCell(3,5,"first-dialog", new Dialog(dialogBar), m1.getImageCell()));
m1.addInteractionEvent(new InteractionCell(5, 5, "dialog-test", new Dialog(dialogBar), m1.getImageCell()));
m1.addInteractionEvent(new TriggerCell(4,6,"roses-are-red", new Dialog(dialogBar), m1.getImageCell()));
Map m5 = Map.loadMapFromFile("m5", this);
m5.getPerson().setImagePerson("mainpng.png", rec);
m5.setName("m5");
m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this));
m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this));
m2.addInteractionEvent(new TriggerCell(7,2,"qa-tester", new Dialog(dialogBar), m1.getImageCell()));
m3.addMoveToEvent(new EventMoveToCell(3, 1, 8, 8, m4, this));
m3.addMoveToEvent(new EventMoveToCell(2, 3, 8, 1, m2, this));
m3.addInteractionEvent(new TriggerCell(2,5,"boobs", new Dialog(dialogBar), m1.getImageCell()));
m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1, this));
m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this));
m4.addInteractionEvent(new TriggerCell(3,3,"javascript", new Dialog(dialogBar), m1.getImageCell()));
m1.getImageCell().readMapFile("m1-tileset");
m2.getImageCell().readMapFile("m1-tileset");
m3.getImageCell().readMapFile("m1-tileset");
m4.getImageCell().readMapFile("m1-tileset");
LinkedList<Map> mapLinkedList = new LinkedList<>();
mapLinkedList = new LinkedList<>();
mapLinkedList.add(m1);
mapLinkedList.add(m2);
mapLinkedList.add(m3);
mapLinkedList.add(m4);
mapLinkedList.add(m5);
// m1.getImageCell().readMapFile("m1-tileset");
// m2.getImageCell().readMapFile("m1-tileset");
// m3.getImageCell().readMapFile("m1-tileset");
// m4.getImageCell().readMapFile("m1-tileset");
m1.linkMaps(m1, "m1", this);
m2.linkMaps(m2, "m2", this);
m3.linkMaps(m3, "m3", this);
m4.linkMaps(m4, "m4", this);
m5.linkMaps(m5, "m5", this);
map = new MapPainter(mapLinkedList);
setMap(m1);
setMap(m5);
map.createMap();
isReadyToPaint = true;
canvas.repaint();

View File

@ -0,0 +1,11 @@
10 10
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,4,3,m4 BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY INTER,first-dialog EMPTY INTER,dialog-test EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY TRIGGER,roses-are-red EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK MOVETO,5,1,m5 EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,1,1,m2 BLOCK
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK

View File

@ -0,0 +1,12 @@
<tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 82 94 0 0 82 94 0 0
0 0 83 95 0 0 83 95 0 0
0 0 0 0 0 0 0 0 0 0
0 0 153 0 0 0 0 153 0 0
0 0 0 153 153 153 153 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

View File

@ -0,0 +1,11 @@
10 10
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,1,3,m1 EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY TRIGGER,qa-tester EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,2,1,m3 EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY

View File

@ -0,0 +1,12 @@
<tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 82 94 0 0 82 94 0 0
0 0 83 95 0 0 83 95 0 0
0 0 0 0 0 0 0 0 0 0
0 0 153 0 0 0 0 153 0 0
0 0 0 153 153 153 153 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

View File

@ -0,0 +1,11 @@
10 10
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,2,3,m2 BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY TRIGGER,boobs EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY BLOCK
BLOCK EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,3,1,m4 BLOCK
BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK BLOCK

View File

@ -0,0 +1,12 @@
<tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 82 94 0 0 82 94 0 0
0 0 83 95 0 0 83 95 0 0
0 0 0 0 0 0 0 0 0 0
0 0 153 0 0 0 0 153 0 0
0 0 0 153 153 153 153 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

View File

@ -0,0 +1,11 @@
10 10
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,3,3,m3 EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY TRIGGER,javascript EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY MOVETO,4,1,m1 EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY

View File

@ -0,0 +1,12 @@
<tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 82 94 0 0 82 94 0 0
0 0 83 95 0 0 83 95 0 0
0 0 0 0 0 0 0 0 0 0
0 0 153 0 0 0 0 153 0 0
0 0 0 153 153 153 153 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

View File

@ -0,0 +1,11 @@
10 10
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY BLOCK MOVETO,5,3,m1 BLOCK EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY BLOCK SPAWN BLOCK EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY BLOCK BLOCK BLOCK EMPTY EMPTY EMPTY EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY
BLOCK EMPTY EMPTY BLOCK BLOCK BLOCK EMPTY BLOCK EMPTY EMPTY
BLOCK EMPTY EMPTY BLOCK EMPTY BLOCK EMPTY BLOCK EMPTY EMPTY
BLOCK BLOCK EMPTY BLOCK BLOCK BLOCK EMPTY BLOCK BLOCK EMPTY
EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY EMPTY