| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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<InteractiveTile> 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.maxWorldCol && worldRow < gamePanel.maxWorldRow){
- int tileNum = mapTileNum[worldCol][worldRow];
- int worldX = worldCol * gamePanel.tileSize;
- int worldY = worldRow * gamePanel.tileSize;
- double screenX = worldX - gamePanel.camera.worldX + gamePanel.camera.screenX;
- double screenY = worldY - gamePanel.camera.worldY + gamePanel.camera.screenY;
- if(worldX + gamePanel.tileSize*mapTileOverflow > 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");
- }
- }
|