2
0
Эх сурвалжийг харах

Schönere Collision Detection beim Ziehen von Tiles (+ BG Tile Check)

Jan 5 сар өмнө
parent
commit
29bc8514c1

+ 12 - 0
src/main/java/model/tiles/BackgroundTile.java

@@ -3,5 +3,17 @@ package model.tiles;
 import model.Tile;
 
 public class BackgroundTile extends Tile {
+    private boolean solid = true;
 
+    /**
+     *
+     * @return True if something can be placed on this Tile
+     */
+    public boolean isSolid(){
+        return solid;
+    }
+
+    public void setSolid(boolean solid){
+        this.solid = solid;
+    }
 }

+ 16 - 7
src/main/java/view/tile/TileManager.java

@@ -16,7 +16,7 @@ import java.io.*;
 public class TileManager implements RenderingManager {
 
     private GamePanel gamePanel;
-    private Tile[] tile;
+    private BackgroundTile[] tile;
     private int[][] mapTileNum;
     private int mapTileOverflow = 1;
     private InteractiveTileController draggingTile;
@@ -24,7 +24,7 @@ public class TileManager implements RenderingManager {
 
     public TileManager(GamePanel gp){
         this.gamePanel = gp;
-        tile = new Tile[10];
+        tile = new BackgroundTile[10];
         mapTileNum = new int[GamePanel.maxWorldCol][GamePanel.maxWorldRow];
         getTileImage();
     }
@@ -149,10 +149,10 @@ public class TileManager implements RenderingManager {
         try{
             setupTile(0, "grass");
             setupTile(1, "wall");
-            setupTile(2, "water");
+            setupTile(2, "water", false);
             setupTile(3, "earth");
 
-            setupTile(4, "tree");
+            setupTile(4, "tree", false);
 
             setupTile(5, "sand");
 
@@ -160,14 +160,16 @@ public class TileManager implements RenderingManager {
             e.printStackTrace();
         }
     }
+
     private void setupTile(int index, String path) throws IOException {
         tile[index] = new BackgroundTile();
         tile[index].setImage("/tiles/background/" + path + ".png");
     }
 
-    private void setupInteractiveTile( int index, String path) throws IOException {
+    private void setupTile(int index, String path, boolean solid) throws IOException {
         tile[index] = new BackgroundTile();
-        tile[index].setImage("/tiles/" + path + ".png");
+        tile[index].setImage("/tiles/background/" + path + ".png");
+        tile[index].setSolid(solid);
     }
 
     public void generateNewWorld() {
@@ -257,13 +259,20 @@ public class TileManager implements RenderingManager {
         for (int x = newX; x < newX + width; x++) {
             for (int y = newY; y < newY + height; y++) {
                 InteractiveTileController other = getTileAt(x, y);
-                if (other != null && other != draggingTile) {
+                BackgroundTile bg = getBackgroundTileAt(x,y);
+                if ((other != null && other != draggingTile) || !bg.isSolid()) {
                     return false;
                 }
             }
         }
         return true;
     }
+
+    private BackgroundTile getBackgroundTileAt(int newX, int newY) {
+        int tileNum = mapTileNum[newX][newY];
+        return tile[tileNum];
+    }
+
     /**
      * Returns the InteractiveTileController at the specified grid coordinates, or null if none.
      *