DEADSOFTWARE

Nonnull by default
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameWorld.java
index 6aefd8945b760f3b25f8c92f56fa7904122712fc..01ddee9c111de4c81be866d4293cb7497d77b503 100644 (file)
@@ -1,12 +1,33 @@
 package ru.deadsoftware.cavedroid.game;
 
+import ru.deadsoftware.cavedroid.game.objects.Block;
 import ru.deadsoftware.cavedroid.game.objects.Drop;
 
+import static ru.deadsoftware.cavedroid.GameScreen.GP;
+
+@SuppressWarnings("WeakerAccess")
 public class GameWorld {
 
-    private int WIDTH, HEIGHT;
-    private int[][] foreMap;
-    private int[][] backMap;
+    private final int WIDTH;
+    private final int HEIGHT;
+    private final int[][] foreMap;
+    private final int[][] backMap;
+
+    GameWorld(int width, int height) {
+        WIDTH = width;
+        HEIGHT = height;
+        WorldGen.genWorld(WIDTH, HEIGHT);
+        foreMap = WorldGen.getForeMap();
+        backMap = WorldGen.getBackMap();
+        WorldGen.clear();
+    }
+
+    GameWorld(int[][] foreMap, int[][] backMap) {
+        this.foreMap = foreMap.clone();
+        this.backMap = backMap.clone();
+        WIDTH = foreMap.length;
+        HEIGHT = foreMap[0].length;
+    }
 
     public int getWidth() {
         return WIDTH;
@@ -24,11 +45,11 @@ public class GameWorld {
         return HEIGHT * 16f;
     }
 
-    public int[][] getFullForeMap() {
+    int[][] getFullForeMap() {
         return foreMap;
     }
 
-    public int[][] getFullBackMap() {
+    int[][] getFullBackMap() {
         return backMap;
     }
 
@@ -43,7 +64,7 @@ public class GameWorld {
         try {
             x = transformX(x);
             map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
-        } catch (ArrayIndexOutOfBoundsException e) {
+        } catch (ArrayIndexOutOfBoundsException ignored) {
         }
         return map;
     }
@@ -53,24 +74,40 @@ public class GameWorld {
             x = transformX(x);
             if (layer == 0) foreMap[x][y] = value;
             else backMap[x][y] = value;
-        } catch (ArrayIndexOutOfBoundsException e) {
+        } catch (ArrayIndexOutOfBoundsException ignored) {
         }
     }
 
+    public boolean hasForeAt(int x, int y) {
+        return getMap(x, y, 0) != 0;
+    }
+
+    public boolean hasBackAt(int x, int y) {
+        return getMap(x, y, 1) != 0;
+    }
+
     public int getForeMap(int x, int y) {
         return getMap(x, y, 0);
     }
 
-    public void setForeMap(int x, int y, int value) {
-        setMap(x, y, 0, value);
+    public Block getForeMapBlock(int x, int y) {
+        return GameItems.getBlock(getMap(x, y, 0));
+    }
+
+    public void setForeMap(int x, int y, int id) {
+        setMap(x, y, 0, id);
     }
 
     public int getBackMap(int x, int y) {
         return getMap(x, y, 1);
     }
 
-    public void setBackMap(int x, int y, int value) {
-        setMap(x, y, 1, value);
+    public Block getBackMapBlock(int x, int y) {
+        return GameItems.getBlock(getMap(x, y, 1));
+    }
+
+    public void setBackMap(int x, int y, int id) {
+        setMap(x, y, 1, id);
     }
 
     private void placeSlab(int x, int y, int value) {
@@ -97,7 +134,7 @@ public class GameWorld {
     }
 
     public void placeToForeground(int x, int y, int value) {
-        if (getForeMap(x, y) == 0 || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
+        if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
             setForeMap(x, y, value);
         } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
             placeSlab(x, y, value);
@@ -114,32 +151,18 @@ public class GameWorld {
         }
     }
 
-    public void destroyForeMap(int x, int y, GameProc gp) {
-        if (GameItems.getBlock(getForeMap(x, y)).getDrop() > 0)
-            gp.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getBlock(getForeMap(x, y)).getDrop()));
+    public void destroyForeMap(int x, int y) {
+        if (GameItems.getBlock(getForeMap(x, y)).hasDrop())
+            GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
+                    GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
         placeToForeground(x, y, 0);
     }
 
-    public void destroyBackMap(int x, int y, GameProc gp) {
-        if (GameItems.getBlock(getBackMap(x, y)).getDrop() > 0)
-            gp.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getBlock(getBackMap(x, y)).getDrop()));
+    public void destroyBackMap(int x, int y) {
+        if (GameItems.getBlock(getBackMap(x, y)).hasDrop())
+            GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
+                    GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
         placeToBackground(x, y, 0);
     }
 
-    public void generate(int w, int h) {
-        WIDTH = w;
-        HEIGHT = h;
-        WorldGen.genWorld(WIDTH, HEIGHT);
-        foreMap = WorldGen.getForeMap();
-        backMap = WorldGen.getBackMap();
-        WorldGen.clear();
-    }
-
-    public void setMaps(int[][] foreMap, int[][] backMap) {
-        this.foreMap = foreMap.clone();
-        this.backMap = backMap.clone();
-        WIDTH = foreMap.length;
-        HEIGHT = foreMap[0].length;
-    }
-
-}
+}
\ No newline at end of file