TileManager.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package view.tile;
  2. import controller.GameController;
  3. import model.Tile;
  4. import model.tiles.BackgroundTile;
  5. import view.GamePanel;
  6. import view.tile.interactive.InteractiveTile;
  7. import java.awt.*;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.util.ArrayList;
  13. public class TileManager {
  14. GamePanel gamePanel;
  15. public Tile[] tile;
  16. private int MaxTileNumber;
  17. public int[][] mapTileNum;
  18. public Tile[][] mapTiles;
  19. public int mapTileOverflow = 1;
  20. public ArrayList<InteractiveTile> interactiveTiles = new ArrayList<>();
  21. public TileManager(GamePanel gp){
  22. this.gamePanel = gp;
  23. tile = new Tile[10];
  24. mapTileNum = new int[gp.maxWorldCol][gp.maxWorldRow];
  25. getTileImage();
  26. loadMap("/maps/world01.txt");
  27. }
  28. public void addInteractiveTile(InteractiveTile tile) {
  29. interactiveTiles.add(tile);
  30. }
  31. public void loadMap(String filePath){
  32. try{
  33. InputStream is = getClass().getResourceAsStream(filePath);
  34. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  35. int col = 0;
  36. int row = 0;
  37. while(col < gamePanel.maxWorldCol && row < gamePanel.maxWorldRow){
  38. String line = br.readLine();
  39. while(col < gamePanel.maxWorldRow){
  40. String numbers[] = line.split(" ");
  41. int num = Integer.parseInt(numbers[col]);
  42. mapTileNum[col][row] = num;
  43. col++;
  44. }
  45. if(col == gamePanel.maxWorldCol) {
  46. col = 0;
  47. row++;
  48. }
  49. }
  50. br.close();
  51. }catch (Exception e){
  52. e.printStackTrace();
  53. }
  54. }
  55. public void draw(Graphics2D g2){
  56. int worldCol = 0;
  57. int worldRow = 0;
  58. while (worldCol<gamePanel.maxWorldCol && worldRow < gamePanel.maxWorldRow){
  59. int tileNum = mapTileNum[worldCol][worldRow];
  60. int worldX = worldCol * gamePanel.tileSize;
  61. int worldY = worldRow * gamePanel.tileSize;
  62. double screenX = worldX - gamePanel.camera.worldX + gamePanel.camera.screenX;
  63. double screenY = worldY - gamePanel.camera.worldY + gamePanel.camera.screenY;
  64. if(worldX + gamePanel.tileSize*mapTileOverflow > gamePanel.camera.worldX - gamePanel.camera.screenX &&
  65. worldX - gamePanel.tileSize*mapTileOverflow < gamePanel.camera.worldX + gamePanel.camera.screenX &&
  66. worldY + gamePanel.tileSize*mapTileOverflow > gamePanel.camera.worldY - gamePanel.camera.screenY &&
  67. worldY - gamePanel.tileSize*mapTileOverflow < gamePanel.camera.worldY + gamePanel.camera.screenY ) {
  68. g2.drawImage(tile[tileNum].image, (int) screenX, (int) screenY, gamePanel.tileSize, gamePanel.tileSize, null);
  69. }
  70. worldCol++;
  71. if(worldCol == gamePanel.maxWorldCol){
  72. worldCol = 0;
  73. worldRow++;
  74. }
  75. }
  76. for (InteractiveTile tile : interactiveTiles) {
  77. tile.updateCoordinates();
  78. tile.draw(g2, gamePanel.camera);
  79. }
  80. }
  81. public int worldColToScreenX(int worldCol) {
  82. double worldX = worldCol * gamePanel.tileSize; // Reactively use tileSize
  83. return (int) (worldX - gamePanel.camera.worldX + gamePanel.camera.screenX);
  84. }
  85. public int worldRowToScreenY(int worldRow) {
  86. double worldY = worldRow * gamePanel.tileSize; // Reactively use tileSize
  87. return (int) (worldY - gamePanel.camera.worldY + gamePanel.camera.screenY);
  88. }
  89. public void getTileImage(){
  90. try{
  91. setupTile(0, "grass");
  92. setupTile(1, "wall");
  93. setupTile(2, "water");
  94. setupTile(3, "earth");
  95. setupTile(4, "tree");
  96. setupTile(5, "sand");
  97. }catch (IOException e){
  98. e.printStackTrace();
  99. }
  100. }
  101. private void setupTile(int index, String path) throws IOException {
  102. tile[index] = new BackgroundTile();
  103. tile[index].setImage("/tiles/" + path + ".png");
  104. }
  105. private void setupInteractiveTile( int index, String path) throws IOException {
  106. tile[index] = new BackgroundTile();
  107. tile[index].setImage("/tiles/" + path + ".png");
  108. }
  109. }