DEADSOFTWARE

Fix NPE after loading game
[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 com.badlogic.gdx.utils.ArrayMap;
7 public class GameWorld {
9 private int WIDTH, HEIGHT;
11 public ArrayMap<String, Integer> metaMap;
12 private int[][] foreMap;
13 private int[][] backMap;
15 public int getWidth() {
16 return WIDTH;
17 }
19 public int getHeight() {
20 return HEIGHT;
21 }
23 public int[][] getFullForeMap() {
24 return foreMap;
25 }
27 public int[][] getFullBackMap() {
28 return backMap;
29 }
31 public int getForeMap(int x, int y) {
32 int map = 0;
33 try {
34 x = x%getWidth();
35 if (x<0) x=getWidth()-Math.abs(x);
36 map = foreMap[x][y];
37 } catch (ArrayIndexOutOfBoundsException e) {
38 Gdx.app.error("GameWorld",e.toString());
39 }
40 return map;
41 }
43 public void setForeMap(int x, int y, int value) {
44 try {
45 x = x%getWidth();
46 if (x<0) x=getWidth()-Math.abs(x);
47 foreMap[x][y] = value;
48 } catch (ArrayIndexOutOfBoundsException e) {
49 Gdx.app.error("GameWorld", e.toString());
50 }
51 }
53 public int getBackMap(int x, int y) {
54 int map = 0;
55 try {
56 x = x%getWidth();
57 if (x<0) x=getWidth()-Math.abs(x);
58 map = backMap[x][y];
59 } catch (ArrayIndexOutOfBoundsException e) {
60 Gdx.app.error("GameWorld",e.toString());
61 }
62 return map;
63 }
65 public void setBackMap(int x, int y, int value) {
66 try {
67 x = x%getWidth();
68 if (x<0) x=getWidth()-Math.abs(x);
69 backMap[x][y] = value;
70 } catch (ArrayIndexOutOfBoundsException e) {
71 Gdx.app.error("GameWorld", e.toString());
72 }
73 }
75 public int getMeta(int x, int y) {
76 if (metaMap.containsKey(x+"_"+y)) return metaMap.get(x+"_"+y);
77 else return 0;
78 }
80 public void setMeta(int x, int y, int value) {
81 if (metaMap.containsKey(x+"_"+y)) metaMap.removeKey(x+"_"+y);
82 metaMap.put(x+"_"+y, value);
83 }
85 public void placeToForeground(int x, int y, int value) {
86 if (getForeMap(x,y) == 0 || value == 0) {
87 setForeMap(x,y,value);
88 }
89 }
91 public void placeToBackground(int x, int y, int value) {
92 if (value==0 || (getBackMap(x,y) == 0 && !Items.BLOCKS.getValueAt(value).background)) {
93 setBackMap(x,y,value);
94 }
95 }
97 public Vector2 getSpawnPoint() {
98 int x=0,y=0;
99 while (true) {
100 y++;
101 if (getForeMap(x,y)>0 && Items.BLOCKS.getValueAt(getForeMap(x,y)).collision) break;
103 x = x*16 + 4;
104 y = y*16 - 32;
105 return new Vector2(x,y);
108 public void generate(int w, int h) {
109 WIDTH = w;
110 HEIGHT = h;
111 WorldGen.genWorld(WIDTH,HEIGHT);
112 foreMap = WorldGen.getForeMap();
113 backMap = WorldGen.getBackMap();
114 metaMap = new ArrayMap<String, Integer>();
115 WorldGen.clear();
118 public void setMaps(int[][] foreMap, int[][] backMap) {
119 this.foreMap = foreMap.clone();
120 this.backMap = backMap.clone();
121 WIDTH = foreMap.length;
122 HEIGHT = foreMap[0].length;