package view.tile; import controller.GameController; import model.Tile; import model.tiles.BackgroundTile; import view.GamePanel; import view.tile.interactive.InteractiveTile; import java.awt.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class TileManager { GamePanel gamePanel; public Tile[] tile; private int MaxTileNumber; public int[][] mapTileNum; public Tile[][] mapTiles; public int mapTileOverflow = 1; public ArrayList interactiveTiles = new ArrayList<>(); public TileManager(GamePanel gp){ this.gamePanel = gp; tile = new Tile[10]; mapTileNum = new int[gp.maxWorldCol][gp.maxWorldRow]; getTileImage(); loadMap("/maps/world01.txt"); } public void addInteractiveTile(InteractiveTile tile) { interactiveTiles.add(tile); } public void loadMap(String filePath){ try{ InputStream is = getClass().getResourceAsStream(filePath); BufferedReader br = new BufferedReader(new InputStreamReader(is)); int col = 0; int row = 0; while(col < gamePanel.maxWorldCol && row < gamePanel.maxWorldRow){ String line = br.readLine(); while(col < gamePanel.maxWorldRow){ String numbers[] = line.split(" "); int num = Integer.parseInt(numbers[col]); mapTileNum[col][row] = num; col++; } if(col == gamePanel.maxWorldCol) { col = 0; row++; } } br.close(); }catch (Exception e){ e.printStackTrace(); } } public void draw(Graphics2D g2){ int worldCol = 0; int worldRow = 0; while (worldCol gamePanel.camera.worldX - gamePanel.camera.screenX && worldX - gamePanel.tileSize*mapTileOverflow < gamePanel.camera.worldX + gamePanel.camera.screenX && worldY + gamePanel.tileSize*mapTileOverflow > gamePanel.camera.worldY - gamePanel.camera.screenY && worldY - gamePanel.tileSize*mapTileOverflow < gamePanel.camera.worldY + gamePanel.camera.screenY ) { g2.drawImage(tile[tileNum].image, (int) screenX, (int) screenY, gamePanel.tileSize, gamePanel.tileSize, null); } worldCol++; if(worldCol == gamePanel.maxWorldCol){ worldCol = 0; worldRow++; } } for (InteractiveTile tile : interactiveTiles) { tile.updateCoordinates(); tile.draw(g2, gamePanel.camera); } } public int worldColToScreenX(int worldCol) { double worldX = worldCol * gamePanel.tileSize; // Reactively use tileSize return (int) (worldX - gamePanel.camera.worldX + gamePanel.camera.screenX); } public int worldRowToScreenY(int worldRow) { double worldY = worldRow * gamePanel.tileSize; // Reactively use tileSize return (int) (worldY - gamePanel.camera.worldY + gamePanel.camera.screenY); } public void getTileImage(){ try{ setupTile(0, "grass"); setupTile(1, "wall"); setupTile(2, "water"); setupTile(3, "earth"); setupTile(4, "tree"); setupTile(5, "sand"); }catch (IOException e){ e.printStackTrace(); } } private void setupTile(int index, String path) throws IOException { tile[index] = new BackgroundTile(); tile[index].setImage("/tiles/" + path + ".png"); } private void setupInteractiveTile( int index, String path) throws IOException { tile[index] = new BackgroundTile(); tile[index].setImage("/tiles/" + path + ".png"); } }