diff --git a/src/main/java/Block.java b/src/main/java/Block.java index ba7e83b..310b36a 100644 --- a/src/main/java/Block.java +++ b/src/main/java/Block.java @@ -21,7 +21,7 @@ public class Block extends Cell { public void paint(Graphics2D g) { super.paint(g); g.fillOval(getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE); - g.drawImage(imageCell.getImagesOfTiles()[102], getX() * WindowOfFreeMove.CELL_SIZE, + g.drawImage(imageCell.getImagesOfTiles().get(102), getX() * WindowOfFreeMove.CELL_SIZE, getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } } \ No newline at end of file diff --git a/src/main/java/ImageCell.java b/src/main/java/ImageCell.java index f09584a..b8cb271 100644 --- a/src/main/java/ImageCell.java +++ b/src/main/java/ImageCell.java @@ -4,42 +4,52 @@ import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.LinkedList; import java.util.Scanner; public class ImageCell { private static BufferedImage image; - private static BufferedImage[] imagesOfTiles; + private static LinkedList imagesOfTiles; public void readMapFile(String name) { try { File mapFile = new File("src/main/resources/" + name + ".txt"); Scanner scanner = new Scanner(mapFile); - String firstLine = scanner.nextLine(); - String[] mapInfo = firstLine.split(" "); - ImageTileset(mapInfo); + while (scanner.hasNextLine()) { + String data = scanner.nextLine(); + if (data.contains("")) { + String[] mapInfo = data.split(" "); + ImageTileset(mapInfo); + } + } } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); } } + public void readMapFileDirect(String[] mapInfo) { + ImageTileset(mapInfo); + } + private void ImageTileset(String[] mapInfo) { try { - image = ImageIO.read(getClass().getResource(mapInfo[0])); + image = ImageIO.read(getClass().getResource(mapInfo[1])); } catch (IOException e) { e.printStackTrace(); } - int width = Integer.parseInt(mapInfo[1]); - int height = Integer.parseInt(mapInfo[2]); - int tileSize = Integer.parseInt(mapInfo[3]); + int width = Integer.parseInt(mapInfo[2]); + int height = Integer.parseInt(mapInfo[3]); + int tileSize = Integer.parseInt(mapInfo[4]); int Xtiles = width / tileSize; int Ytiles = height / tileSize; int tiles = Xtiles * Ytiles; - imagesOfTiles = new BufferedImage[tiles]; + if (imagesOfTiles == null) + imagesOfTiles = new LinkedList(); Rectangle[] rectangles = new Rectangle[tiles]; int count = 0; @@ -50,8 +60,8 @@ public class ImageCell { } } - for (int i = 0; i < imagesOfTiles.length; i++) { - imagesOfTiles[i] = image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width); + for (int i = 0; i < tiles; i++) { + imagesOfTiles.add(image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width)); } // while (scanner.hasNextLine()) { @@ -60,7 +70,7 @@ public class ImageCell { // } } - public BufferedImage[] getImagesOfTiles() { + public LinkedList getImagesOfTiles() { return imagesOfTiles; } } diff --git a/src/main/java/InteractionCell.java b/src/main/java/InteractionCell.java index b30d30b..bc9985e 100644 --- a/src/main/java/InteractionCell.java +++ b/src/main/java/InteractionCell.java @@ -7,20 +7,13 @@ public class InteractionCell extends Cell { private String message; private Dialog dialog; private Scanner scanner; + private ImageCell imageCell; - InteractionCell(){ - super(-1,-1, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); - } - - InteractionCell(int x, int y, String message, Dialog dialog) { - super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); + InteractionCell(int x, int y, String message, Dialog dialog, ImageCell imageCell) { + super(x, y, WindowOfFreeMove.CELL_SIZE, Color.WHITE); this.message = message; this.dialog = dialog; - } - - InteractionCell(int x, int y, Dialog dialog) { - super(x, y, WindowOfFreeMove.CELL_SIZE, Color.ORANGE); - this.dialog = dialog; + this.imageCell = imageCell; } public void startEvent() { @@ -69,5 +62,7 @@ public class InteractionCell extends Cell { @Override public void paint(Graphics2D g) { super.paint(g); + g.drawImage(imageCell.getImagesOfTiles().get(334), getX() * WindowOfFreeMove.CELL_SIZE, + getY() * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null); } } diff --git a/src/main/java/WindowOfFreeMove.java b/src/main/java/WindowOfFreeMove.java index ea9054e..5367b7c 100644 --- a/src/main/java/WindowOfFreeMove.java +++ b/src/main/java/WindowOfFreeMove.java @@ -143,8 +143,8 @@ public class WindowOfFreeMove extends JFrame { 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.addInteractionEvent(new InteractionCell(5,5,"dialog-test", new Dialog(dialogBar))); + 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())); m2.addMoveToEvent(new EventMoveToCell(2, 1, 8, 8, m3, this)); m2.addMoveToEvent(new EventMoveToCell(1, 3, 8, 1, m1, this)); @@ -155,7 +155,7 @@ public class WindowOfFreeMove extends JFrame { m4.addMoveToEvent(new EventMoveToCell(4, 1, 8, 8, m1, this)); m4.addMoveToEvent(new EventMoveToCell(3, 3, 8, 1, m3, this)); - m1.getImageCell().readMapFile("map-test"); + m1.getImageCell().readMapFile("m1-tileset"); setMap(m1); map.createMap(); diff --git a/src/main/resources/D_OutDoor.png b/src/main/resources/D_OutDoor.png new file mode 100644 index 0000000..20386a6 Binary files /dev/null and b/src/main/resources/D_OutDoor.png differ diff --git a/src/main/resources/m1-tileset.txt b/src/main/resources/m1-tileset.txt new file mode 100644 index 0000000..cb91cdd --- /dev/null +++ b/src/main/resources/m1-tileset.txt @@ -0,0 +1,2 @@ + A2_Ground.png 512 384 32 + D_OutDoor.png 512 512 32 \ No newline at end of file diff --git a/src/main/resources/map-test.txt b/src/main/resources/map-test.txt deleted file mode 100644 index e192c44..0000000 --- a/src/main/resources/map-test.txt +++ /dev/null @@ -1 +0,0 @@ -A2_Ground.png 512 384 32 \ No newline at end of file