DEADSOFTWARE

Add world saving
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameWorld.java
index faf0322ed38491d995f717417069d4f46a291722..4ed875de4e4f87b705d7301f6c80645293948fe1 100644 (file)
@@ -1,22 +1,16 @@
 package ru.deadsoftware.cavecraft.game;
 
 import com.badlogic.gdx.Gdx;
+import com.badlogic.gdx.math.Vector2;
+import ru.deadsoftware.cavecraft.Items;
 
 public class GameWorld {
 
-    private final int WIDTH, HEIGHT;
+    private int WIDTH, HEIGHT;
+
     private int[][] foreMap;
     private int[][] backMap;
 
-    public GameWorld(int w, int h) {
-        WIDTH = w;
-        HEIGHT = h;
-        WorldGen.genWorld(WIDTH,HEIGHT);
-        foreMap = WorldGen.getForeMap();
-        backMap = WorldGen.getBackMap();
-        WorldGen.clear();
-    }
-
     public int getWidth() {
         return WIDTH;
     }
@@ -26,17 +20,29 @@ public class GameWorld {
     }
 
     public int getForeMap(int x, int y) {
-        int ret = 0;
+        int map = 0;
         try {
-            ret = foreMap[x][y];
+            if (x<0) {
+                x = x % (getWidth());
+                x = getWidth()- Math.abs(x);
+            } else if (x>0) {
+                x = x % (getWidth());
+            }
+            map = foreMap[x][y];
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld",e.toString());
         }
-        return ret;
+        return map;
     }
 
     public void setForeMap(int x, int y, int value) {
         try {
+            if (x<0) {
+                x = x % (getWidth());
+                x = getWidth()- Math.abs(x);
+            } else if (x>0) {
+                x = x % (getWidth());
+            }
             foreMap[x][y] = value;
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld", e.toString());
@@ -44,17 +50,29 @@ public class GameWorld {
     }
 
     public int getBackMap(int x, int y) {
-        int ret = 0;
+        int map = 0;
         try {
-            ret = backMap[x][y];
+            if (x<0) {
+                x = x % (getWidth());
+                x = getWidth()- Math.abs(x);
+            } else if (x>0) {
+                x = x % (getWidth());
+            }
+            map = backMap[x][y];
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld",e.toString());
         }
-        return ret;
+        return map;
     }
 
     public void setBackMap(int x, int y, int value) {
         try {
+            if (x<0) {
+                x = x % (getWidth());
+                x = getWidth()- Math.abs(x);
+            } else if (x>0) {
+                x = x % (getWidth());
+            }
             backMap[x][y] = value;
         } catch (ArrayIndexOutOfBoundsException e) {
             Gdx.app.error("GameWorld", e.toString());
@@ -62,11 +80,48 @@ public class GameWorld {
     }
 
     public void placeToForeground(int x, int y, int value) {
-        if (getForeMap(x,y) == 0 || value == 0) setForeMap(x,y,value);
+        if (getForeMap(x,y) == 0 || value == 0) {
+            setForeMap(x,y,value);
+        }
     }
 
     public void placeToBackground(int x, int y, int value) {
-        if (getBackMap(x,y) == 0 || value == 0) setBackMap(x,y,value);
+        if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
+            setBackMap(x,y,value);
+        }
+    }
+
+    public Vector2 getSpawnPoint(int x) {
+        int y=0;
+        while (true) {
+            y++;
+            if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
+        }
+        x = x*16 + 4;
+        y = y*16 - 32;
+        return new Vector2(x,y);
+    }
+
+    public void generate(int w, int h) {
+        WIDTH = w;
+        HEIGHT = h;
+        WorldGen.genWorld(WIDTH,HEIGHT);
+        foreMap = WorldGen.getForeMap();
+        backMap = WorldGen.getBackMap();
+        WorldGen.clear();
+        save();
+    }
+
+    public void save() {
+        WorldSaver.save(foreMap, backMap);
+    }
+
+    public void load() {
+        WorldSaver.load();
+        foreMap = WorldSaver.getLoadedForeMap();
+        backMap = WorldSaver.getLoadedBackMap();
+        WIDTH = foreMap.length;
+        HEIGHT = foreMap[0].length;
     }
 
 }