now multiple tilesets can be loaded to one map

This commit is contained in:
musilto9 2020-03-12 22:15:06 +01:00
parent 8f1a12e6e3
commit 44c47a01d4
7 changed files with 34 additions and 28 deletions

View File

@ -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);
}
}

View File

@ -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<BufferedImage> 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("<tileset>")) {
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<BufferedImage>();
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<BufferedImage> getImagesOfTiles() {
return imagesOfTiles;
}
}

View File

@ -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);
}
}

View File

@ -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();

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -0,0 +1,2 @@
<tileset> A2_Ground.png 512 384 32
<tileset> D_OutDoor.png 512 512 32

View File

@ -1 +0,0 @@
A2_Ground.png 512 384 32