From e60f04b8b6181f3f4ec6f31bcfe5a49361c969d2 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 26 Nov 2025 21:50:46 +0100 Subject: [PATCH] added map drawing system --- assets/maps/testmap.map | 12 +++++ src/de/miaurizius/jgame2d/core/GamePanel.java | 4 +- .../miaurizius/jgame2d/tile/TileManager.java | 54 ++++++++++++++++--- 3 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 assets/maps/testmap.map diff --git a/assets/maps/testmap.map b/assets/maps/testmap.map new file mode 100644 index 0000000..a691f9f --- /dev/null +++ b/assets/maps/testmap.map @@ -0,0 +1,12 @@ +1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 +1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1 +1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1 +1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1 +1 0 0 0 0 0 2 2 2 2 0 0 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 \ No newline at end of file diff --git a/src/de/miaurizius/jgame2d/core/GamePanel.java b/src/de/miaurizius/jgame2d/core/GamePanel.java index dc3a506..b72d3c4 100644 --- a/src/de/miaurizius/jgame2d/core/GamePanel.java +++ b/src/de/miaurizius/jgame2d/core/GamePanel.java @@ -14,8 +14,8 @@ public class GamePanel extends JPanel implements Runnable { public final int tileSize = originalTileSize * scale; //48x48 tile // 4:3 ratio - final int maxScreenCol = 16; - final int maxScreenRow = 12; + public final int maxScreenCol = 16; + public final int maxScreenRow = 12; final int screenWidth = tileSize * maxScreenCol; // 768 pixels final int screenHeight = tileSize * maxScreenRow; // 576 pixels diff --git a/src/de/miaurizius/jgame2d/tile/TileManager.java b/src/de/miaurizius/jgame2d/tile/TileManager.java index 9da4ab7..be9d455 100644 --- a/src/de/miaurizius/jgame2d/tile/TileManager.java +++ b/src/de/miaurizius/jgame2d/tile/TileManager.java @@ -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; + } + } } }