added map drawing system

This commit is contained in:
2025-11-26 21:50:46 +01:00
parent 9e6834b304
commit e60f04b8b6
3 changed files with 61 additions and 9 deletions

View File

@@ -4,18 +4,20 @@ import de.miaurizius.jgame2d.core.GamePanel;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
public class TileManager {
GamePanel panel;
Tile[] tile;
int[][] mapTileNum;
public TileManager(GamePanel panel) {
this.panel = panel;
tile = new Tile[10];
mapTileNum = new int[panel.maxScreenCol][panel.maxScreenRow];
getTileImage();
loadMap("testmap");
}
public void getTileImage() {
@@ -27,17 +29,55 @@ public class TileManager {
tile[1].image = ImageIO.read(new FileInputStream("assets/tiles/wall.png"));
tile[2] = new Tile();
tile[2].image = ImageIO.read(new FileInputStream("assets/tiles/grass.png"));
tile[3] = new Tile();
tile[3].image = ImageIO.read(new FileInputStream("assets/tiles/water.png"));
tile[2].image = ImageIO.read(new FileInputStream("assets/tiles/water.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadMap(String map) {
try {
InputStream stream = new FileInputStream("assets/maps/"+map+".map");
BufferedReader bReader = new BufferedReader(new InputStreamReader(stream));
int col = 0;
int row = 0;
while(col < panel.maxScreenCol && row < panel.maxScreenRow) {
String line = bReader.readLine();
while(col < panel.maxScreenCol) {
String[] numbers = line.split(" ");
int num = Integer.parseInt(numbers[col]);
mapTileNum[col][row] = num;
col++;
}
if(col == panel.maxScreenCol) {
col = 0;
row++;
}
}
bReader.close();
} catch(Exception e) {
e.printStackTrace();
}
}
public void draw(Graphics2D graphics2D) {
graphics2D.drawImage(tile[0].image, 0, 0, panel.tileSize, panel.tileSize, null);
int col = 0;
int row = 0;
int x = 0;
int y = 0;
while(col < panel.maxScreenCol && row < panel.maxScreenRow) {
int tileNum = mapTileNum[col][row];
graphics2D.drawImage(tile[tileNum].image, x, y, panel.tileSize, panel.tileSize, null);
col++;
x += panel.tileSize;
if(col == panel.maxScreenCol) {
col = 0;
x = 0;
row++;
y += panel.tileSize;
}
}
}
}