183 lines
6.4 KiB
Java
183 lines
6.4 KiB
Java
package ruscz.game.gameOfProd.Image;
|
|
|
|
import ruscz.game.gameOfProd.Windows.WindowOfFreeMove;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.util.HashMap;
|
|
import java.util.LinkedList;
|
|
import java.util.Scanner;
|
|
|
|
public class ImageCell {
|
|
private static BufferedImage image;
|
|
private LinkedList<BufferedImage> imagesOfTiles;
|
|
private HashMap<String, Integer> cellMap;
|
|
private ImageDoor imageDoor;
|
|
private LinkedList<BufferedImage> imagesOfItems;
|
|
private int[][] mapBackground;
|
|
private int[][] mapForeground;
|
|
|
|
public void readMapFile(String name) {
|
|
try {
|
|
mapBackground = new int[WindowOfFreeMove.CANVAS_WIDTH][WindowOfFreeMove.CANVAS_HEIGHT];
|
|
mapForeground = new int[WindowOfFreeMove.CANVAS_WIDTH][WindowOfFreeMove.CANVAS_HEIGHT];
|
|
File mapFile = new File("src/main/resources/maps/" + name + "-tileset.txt");
|
|
Scanner scanner = new Scanner(mapFile);
|
|
int j = 0;
|
|
while (scanner.hasNextLine()) {
|
|
String data = scanner.nextLine();
|
|
if (data.contains("<tileset>")) {
|
|
String[] mapInfo = data.split(" ");
|
|
imageTileset(mapInfo);
|
|
}
|
|
else if (data.contains("<items>")) {
|
|
String[] mapInfo = data.split(" ");
|
|
imageItem(mapInfo);
|
|
}
|
|
else if (data.contains("<cellmap>")) {
|
|
String[] mapInfo = data.split(" ");
|
|
loadCellMap(mapInfo);
|
|
}
|
|
else if (!data.contains("<soundmap>")) {
|
|
String[] mapColumn = data.split(" ");
|
|
for (int i = 0; i < mapColumn.length; i++) {
|
|
mapBackground[i][j] = Integer.parseInt(mapColumn[i]);
|
|
}
|
|
j++;
|
|
}
|
|
}
|
|
imageObjectset(name);
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println("An error occurred.");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void imageTileset(String[] mapInfo) {
|
|
try {
|
|
image = ImageIO.read(new File("src/main/resources/" + mapInfo[1]));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
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;
|
|
|
|
if (imagesOfTiles == null)
|
|
imagesOfTiles = new LinkedList<BufferedImage>();
|
|
|
|
Rectangle[] rectangles = new Rectangle[tiles];
|
|
int count = 0;
|
|
for (int i = 0; i < rectangles.length / Ytiles; i++) {
|
|
for (int j = 0; j < rectangles.length / Xtiles; j++) {
|
|
rectangles[count] = new Rectangle(tileSize * i, tileSize * j, tileSize, tileSize);
|
|
count++;
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < tiles; i++) {
|
|
imagesOfTiles.add(image.getSubimage(rectangles[i].x, rectangles[i].y, rectangles[i].height, rectangles[i].width));
|
|
}
|
|
}
|
|
|
|
public void imageObjectset(String file) {
|
|
try {
|
|
File objectFile = new File("src/main/resources/maps/" + file + "-objectset.txt");
|
|
Scanner scanner = new Scanner(objectFile);
|
|
int j = 0;
|
|
while (scanner.hasNextLine()) {
|
|
String[] objects = scanner.nextLine().split(" ");
|
|
for (int i = 0; i < objects.length; i++) {
|
|
mapForeground[i][j] = Integer.parseInt(objects[i]);
|
|
}
|
|
j++;
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public void loadCellMap(String[] cellInfo) {
|
|
if (cellMap == null)
|
|
cellMap = new HashMap<String, Integer>();
|
|
|
|
if (cellInfo[1].equals("DOOR")) {
|
|
Rectangle[] rec = new Rectangle[12];
|
|
int CELL_SIZE = WindowOfFreeMove.CELL_SIZE;
|
|
int count = 0;
|
|
for (int i = 0; i < rec.length / 4; i++) {
|
|
for (int j = 0; j < rec.length / 3; j++) {
|
|
rec[count] = new Rectangle(CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE, CELL_SIZE);
|
|
count++;
|
|
}
|
|
}
|
|
imageDoor = new ImageDoor(cellInfo[2], rec);
|
|
}
|
|
else
|
|
cellMap.put(cellInfo[1], Integer.parseInt(cellInfo[2]));
|
|
}
|
|
|
|
public void imageItem(String[] itemInfo) {
|
|
int imageWidth = 0;
|
|
int imageHeight = 0;
|
|
int size = 0;
|
|
try {
|
|
image = ImageIO.read(new File("src/main/resources/items/" + itemInfo[1]));
|
|
imageWidth = Integer.parseInt(itemInfo[2]);
|
|
imageHeight = Integer.parseInt(itemInfo[3]);
|
|
size = Integer.parseInt(itemInfo[4]);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
int Xtiles = imageWidth / size;
|
|
int Ytiles = imageHeight / size;
|
|
int tiles = Xtiles * Ytiles;
|
|
|
|
if (imagesOfItems == null)
|
|
imagesOfItems = new LinkedList<BufferedImage>();
|
|
|
|
for (int i = 0; i < Xtiles; i++) {
|
|
for (int j = 0; j < Ytiles; j++) {
|
|
BufferedImage img = image.getSubimage(size * i, size * j, size, size);
|
|
imagesOfItems.add(img);
|
|
}
|
|
}
|
|
}
|
|
|
|
public LinkedList<BufferedImage> getImagesOfItems() {
|
|
return imagesOfItems;
|
|
}
|
|
|
|
public int getImageFromCellMap(String cell) {
|
|
return cellMap.get(cell);
|
|
}
|
|
|
|
public LinkedList<BufferedImage> getImagesOfTiles() {
|
|
return imagesOfTiles;
|
|
}
|
|
|
|
public ImageDoor getImageDoor() { return imageDoor; }
|
|
|
|
public void paintBackground(Graphics g, int x, int y) {
|
|
g.drawImage(imagesOfTiles.get(mapBackground[x][y]), x * WindowOfFreeMove.CELL_SIZE,
|
|
y * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
|
}
|
|
|
|
public void paintForeground(Graphics g, int x, int y) {
|
|
int imageNum = mapForeground[x][y];
|
|
if (imageNum > 0)
|
|
g.drawImage(imagesOfTiles.get(imageNum), x * WindowOfFreeMove.CELL_SIZE,
|
|
y * WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, WindowOfFreeMove.CELL_SIZE, null);
|
|
}
|
|
}
|