Minor improvments
This commit is contained in:
@ -16,41 +16,43 @@ public class GameMap {
|
|||||||
public static final int MAP_ROW_SIZE = 30;
|
public static final int MAP_ROW_SIZE = 30;
|
||||||
public static final int OFFSET_Y = 7 * MAP_TILESIZE; // 160px from top
|
public static final int OFFSET_Y = 7 * MAP_TILESIZE; // 160px from top
|
||||||
public static final int OFFSET_X = MAP_TILESIZE; // 16px from left
|
public static final int OFFSET_X = MAP_TILESIZE; // 16px from left
|
||||||
private final BufferedImage[][] images = new BufferedImage[13][19];
|
private final BufferedImage[][] mapSpriteBuffer;
|
||||||
private final MapTile[][] mapData;
|
private final MapTile[][] mapData;
|
||||||
|
private final BufferedImage[][] mapItemSpriteBuffer;
|
||||||
|
|
||||||
public GameMap() {
|
public GameMap() {
|
||||||
loadSprites();
|
this.mapSpriteBuffer = LoadSave.loadSprites("sprites/PacMan-custom-spritemap-0-3.png", 5, 11, MAP_TILESIZE);
|
||||||
mapData = loadMap("maps/map1.csv");
|
this.mapItemSpriteBuffer = LoadSave.loadSprites("sprites/PacManAssets-Items.png", 2, 8, MAP_TILESIZE);
|
||||||
|
this.mapData = loadMap("maps/map1.csv", MAP_ROW_SIZE, MAP_COL_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MapTile[][] loadMap(String path) {
|
private MapTile[][] loadMap(String path, int mapRowSize, int mapColSize) {
|
||||||
MapTile[][] data = new MapTile[MAP_ROW_SIZE][MAP_COL_SIZE];
|
MapTile[][] data = new MapTile[mapRowSize][mapColSize];
|
||||||
|
|
||||||
try (InputStream is = getClass().getClassLoader().getResourceAsStream(path);
|
try (InputStream is = getClass().getClassLoader().getResourceAsStream(path);
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
|
BufferedReader br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(is)))) {
|
||||||
|
|
||||||
String line;
|
String line;
|
||||||
int rowIndex = 0;
|
int rowIndex = 0;
|
||||||
while ((line = br.readLine()) != null && rowIndex < MAP_ROW_SIZE) {
|
while ((line = br.readLine()) != null && rowIndex < mapRowSize) {
|
||||||
String[] tokens = line.split(",");
|
String[] tokens = line.split(",");
|
||||||
if (tokens.length != MAP_COL_SIZE) {
|
if (tokens.length != mapColSize) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Invalid map format: row " + rowIndex + " has " + tokens.length +
|
"Invalid map format: row " + rowIndex + " has " + tokens.length +
|
||||||
" columns, expected " + MAP_COL_SIZE
|
" columns, expected " + mapColSize
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int col = 0; col < MAP_COL_SIZE; col++) {
|
for (int col = 0; col < mapColSize; col++) {
|
||||||
int value = Integer.parseInt(tokens[col].trim());
|
int value = Integer.parseInt(tokens[col].trim());
|
||||||
data[rowIndex][col] = new MapTile(getSprite(value), value);
|
data[rowIndex][col] = new MapTile(getSprite(value), value);
|
||||||
}
|
}
|
||||||
rowIndex++;
|
rowIndex++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rowIndex != MAP_ROW_SIZE) {
|
if (rowIndex != mapRowSize) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"Invalid map format: found " + rowIndex + " rows, expected " + MAP_ROW_SIZE
|
"Invalid map format: found " + rowIndex + " rows, expected " + mapRowSize
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,76 +66,67 @@ public class GameMap {
|
|||||||
|
|
||||||
private BufferedImage getSprite(int value) {
|
private BufferedImage getSprite(int value) {
|
||||||
return switch (value){
|
return switch (value){
|
||||||
case 0 -> images[1][1];
|
case 0 -> mapSpriteBuffer[1][1];
|
||||||
case 1 -> images[0][0];
|
case 1 -> mapSpriteBuffer[0][0];
|
||||||
case 2 -> images[0][1];
|
case 2 -> mapSpriteBuffer[0][1];
|
||||||
case 3 -> images[0][2];
|
case 3 -> mapSpriteBuffer[0][2];
|
||||||
case 4 -> images[0][3];
|
case 4 -> mapSpriteBuffer[0][3];
|
||||||
case 5 -> images[0][4];
|
case 5 -> mapSpriteBuffer[0][4];
|
||||||
case 6 -> images[0][5];
|
case 6 -> mapSpriteBuffer[0][5];
|
||||||
case 7 -> images[0][6];
|
case 7 -> mapSpriteBuffer[0][6];
|
||||||
case 8 -> images[0][7];
|
case 8 -> mapSpriteBuffer[0][7];
|
||||||
case 9 -> images[0][8];
|
case 9 -> mapSpriteBuffer[0][8];
|
||||||
case 10 -> images[0][9];
|
case 10 -> mapSpriteBuffer[0][9];
|
||||||
case 11 -> images[0][10];
|
case 11 -> mapSpriteBuffer[0][10];
|
||||||
case 12 -> images[1][0];
|
case 12 -> mapSpriteBuffer[1][0];
|
||||||
case 13 -> images[1][1];
|
case 13 -> mapSpriteBuffer[1][1];
|
||||||
case 14 -> images[1][2];
|
case 14 -> mapSpriteBuffer[1][2];
|
||||||
case 15 -> images[1][3];
|
case 15 -> mapSpriteBuffer[1][3];
|
||||||
case 16 -> images[1][4];
|
case 16 -> mapSpriteBuffer[1][4];
|
||||||
case 17 -> images[1][5];
|
case 17 -> mapSpriteBuffer[1][5];
|
||||||
case 18 -> images[1][6];
|
case 18 -> mapSpriteBuffer[1][6];
|
||||||
case 19 -> images[1][7];
|
case 19 -> mapSpriteBuffer[1][7];
|
||||||
case 20 -> images[1][8];
|
case 20 -> mapSpriteBuffer[1][8];
|
||||||
case 21 -> images[1][9];
|
case 21 -> mapSpriteBuffer[1][9];
|
||||||
case 22 -> images[1][10];
|
case 22 -> mapSpriteBuffer[1][10];
|
||||||
case 23 -> images[2][0];
|
case 23 -> mapSpriteBuffer[2][0];
|
||||||
case 24 -> images[2][1];
|
case 24 -> mapSpriteBuffer[2][1];
|
||||||
case 25 -> images[2][2];
|
case 25 -> mapSpriteBuffer[2][2];
|
||||||
case 26 -> images[2][3];
|
case 26 -> mapSpriteBuffer[2][3];
|
||||||
case 27 -> images[2][4];
|
case 27 -> mapSpriteBuffer[2][4];
|
||||||
case 28 -> images[2][5];
|
case 28 -> mapSpriteBuffer[2][5];
|
||||||
case 29 -> images[2][6];
|
case 29 -> mapSpriteBuffer[2][6];
|
||||||
case 30 -> images[2][7];
|
case 30 -> mapSpriteBuffer[2][7];
|
||||||
case 31 -> images[2][8];
|
case 31 -> mapSpriteBuffer[2][8];
|
||||||
case 32 -> images[2][9];
|
case 32 -> mapSpriteBuffer[2][9];
|
||||||
case 33 -> images[2][10];
|
case 33 -> mapSpriteBuffer[2][10];
|
||||||
case 34 -> images[3][0];
|
case 34 -> mapSpriteBuffer[3][0];
|
||||||
case 35 -> images[3][1];
|
case 35 -> mapSpriteBuffer[3][1];
|
||||||
case 36 -> images[3][2];
|
case 36 -> mapSpriteBuffer[3][2];
|
||||||
case 37 -> images[3][3];
|
case 37 -> mapSpriteBuffer[3][3];
|
||||||
case 38 -> images[3][4];
|
case 38 -> mapSpriteBuffer[3][4];
|
||||||
case 39 -> images[3][5];
|
case 39 -> mapSpriteBuffer[3][5];
|
||||||
case 40 -> images[3][6];
|
case 40 -> mapSpriteBuffer[3][6];
|
||||||
case 41 -> images[3][7];
|
case 41 -> mapSpriteBuffer[3][7];
|
||||||
case 42 -> images[3][8];
|
case 42 -> mapSpriteBuffer[3][8];
|
||||||
case 43 -> images[3][9];
|
case 43 -> mapSpriteBuffer[3][9];
|
||||||
case 44 -> images[3][10];
|
case 44 -> mapSpriteBuffer[3][10];
|
||||||
case 45 -> images[4][0];
|
case 45 -> mapSpriteBuffer[4][0];
|
||||||
case 46 -> images[4][1];
|
case 46 -> mapSpriteBuffer[4][1];
|
||||||
case 47 -> images[4][2];
|
case 47 -> mapSpriteBuffer[4][2];
|
||||||
case 48 -> images[4][3];
|
case 48 -> mapSpriteBuffer[4][3];
|
||||||
case 49 -> images[4][4];
|
case 49 -> mapSpriteBuffer[4][4];
|
||||||
case 50 -> images[4][5];
|
case 50 -> mapSpriteBuffer[4][5];
|
||||||
case 51 -> images[4][6];
|
case 51 -> mapSpriteBuffer[4][6];
|
||||||
case 52 -> images[4][7];
|
case 52 -> mapSpriteBuffer[4][7];
|
||||||
case 53 -> images[4][8];
|
case 53 -> mapSpriteBuffer[4][8];
|
||||||
case 54 -> images[4][9];
|
case 54 -> mapSpriteBuffer[4][9];
|
||||||
case 55 -> images[4][10];
|
case 55 -> mapSpriteBuffer[4][10];
|
||||||
|
|
||||||
default -> null;
|
default -> null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadSprites() {
|
|
||||||
BufferedImage img = LoadSave.GetSpriteAtlas("sprites/PacMan-custom-spritemap-0-3.png");//473B78
|
|
||||||
for (int row = 0; row < 5; row++) {
|
|
||||||
for (int col = 0; col < 11; col++) {
|
|
||||||
images[row][col] = img.getSubimage(MAP_TILESIZE * col, MAP_TILESIZE * row, MAP_TILESIZE, MAP_TILESIZE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void draw(Graphics g) {
|
public void draw(Graphics g) {
|
||||||
for (int row = 0; row < mapData.length; row++) {
|
for (int row = 0; row < mapData.length; row++) {
|
||||||
for (int col = 0; col < mapData[row].length; col++) {
|
for (int col = 0; col < mapData[row].length; col++) {
|
||||||
|
|||||||
@ -9,6 +9,21 @@ import java.io.InputStream;
|
|||||||
|
|
||||||
public class LoadSave {
|
public class LoadSave {
|
||||||
|
|
||||||
|
|
||||||
|
public static BufferedImage[][] loadSprites(final String fileName, final int rows, final int columns, int mapTilesize) {
|
||||||
|
BufferedImage spriteSheet = LoadSave.GetSpriteAtlas(fileName);
|
||||||
|
if(spriteSheet.getHeight() != rows * mapTilesize) throw new IllegalStateException("Wrong sprite row size");
|
||||||
|
if(spriteSheet.getWidth() != columns * mapTilesize) throw new IllegalStateException("Wrong sprite colum size");
|
||||||
|
BufferedImage[][] spriteBuffer = new BufferedImage[rows][columns];
|
||||||
|
|
||||||
|
for (int row = 0; row < rows; row++) {
|
||||||
|
for (int col = 0; col < columns; col++) {
|
||||||
|
spriteBuffer[row][col] = spriteSheet.getSubimage(mapTilesize * col, mapTilesize * row, mapTilesize, mapTilesize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return spriteBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
public static BufferedImage GetSpriteAtlas(String fileName) {
|
public static BufferedImage GetSpriteAtlas(String fileName) {
|
||||||
BufferedImage img = null;
|
BufferedImage img = null;
|
||||||
InputStream is = LoadSave.class.getResourceAsStream("/" + fileName);
|
InputStream is = LoadSave.class.getResourceAsStream("/" + fileName);
|
||||||
|
|||||||
Reference in New Issue
Block a user