DEADSOFTWARE

Add world saving
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameWorld.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.math.Vector2;
5 import ru.deadsoftware.cavecraft.Items;
7 public class GameWorld {
9 private int WIDTH, HEIGHT;
11 private int[][] foreMap;
12 private int[][] backMap;
14 public int getWidth() {
15 return WIDTH;
16 }
18 public int getHeight() {
19 return HEIGHT;
20 }
22 public int getForeMap(int x, int y) {
23 int map = 0;
24 try {
25 if (x<0) {
26 x = x % (getWidth());
27 x = getWidth()- Math.abs(x);
28 } else if (x>0) {
29 x = x % (getWidth());
30 }
31 map = foreMap[x][y];
32 } catch (ArrayIndexOutOfBoundsException e) {
33 Gdx.app.error("GameWorld",e.toString());
34 }
35 return map;
36 }
38 public void setForeMap(int x, int y, int value) {
39 try {
40 if (x<0) {
41 x = x % (getWidth());
42 x = getWidth()- Math.abs(x);
43 } else if (x>0) {
44 x = x % (getWidth());
45 }
46 foreMap[x][y] = value;
47 } catch (ArrayIndexOutOfBoundsException e) {
48 Gdx.app.error("GameWorld", e.toString());
49 }
50 }
52 public int getBackMap(int x, int y) {
53 int map = 0;
54 try {
55 if (x<0) {
56 x = x % (getWidth());
57 x = getWidth()- Math.abs(x);
58 } else if (x>0) {
59 x = x % (getWidth());
60 }
61 map = backMap[x][y];
62 } catch (ArrayIndexOutOfBoundsException e) {
63 Gdx.app.error("GameWorld",e.toString());
64 }
65 return map;
66 }
68 public void setBackMap(int x, int y, int value) {
69 try {
70 if (x<0) {
71 x = x % (getWidth());
72 x = getWidth()- Math.abs(x);
73 } else if (x>0) {
74 x = x % (getWidth());
75 }
76 backMap[x][y] = value;
77 } catch (ArrayIndexOutOfBoundsException e) {
78 Gdx.app.error("GameWorld", e.toString());
79 }
80 }
82 public void placeToForeground(int x, int y, int value) {
83 if (getForeMap(x,y) == 0 || value == 0) {
84 setForeMap(x,y,value);
85 }
86 }
88 public void placeToBackground(int x, int y, int value) {
89 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).foreground)) {
90 setBackMap(x,y,value);
91 }
92 }
94 public Vector2 getSpawnPoint(int x) {
95 int y=0;
96 while (true) {
97 y++;
98 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
99 }
100 x = x*16 + 4;
101 y = y*16 - 32;
102 return new Vector2(x,y);
105 public void generate(int w, int h) {
106 WIDTH = w;
107 HEIGHT = h;
108 WorldGen.genWorld(WIDTH,HEIGHT);
109 foreMap = WorldGen.getForeMap();
110 backMap = WorldGen.getBackMap();
111 WorldGen.clear();
112 save();
115 public void save() {
116 WorldSaver.save(foreMap, backMap);
119 public void load() {
120 WorldSaver.load();
121 foreMap = WorldSaver.getLoadedForeMap();
122 backMap = WorldSaver.getLoadedBackMap();
123 WIDTH = foreMap.length;
124 HEIGHT = foreMap[0].length;