DEADSOFTWARE

Add more GameWorld methods
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameWorld.java
1 package ru.deadsoftware.cavedroid.game;
3 import ru.deadsoftware.cavedroid.game.objects.Block;
4 import ru.deadsoftware.cavedroid.game.objects.Drop;
6 import static ru.deadsoftware.cavedroid.GameScreen.GP;
8 public class GameWorld {
10 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 float getWidthPx() {
23 return WIDTH * 16f;
24 }
26 public float getHeightPx() {
27 return HEIGHT * 16f;
28 }
30 int[][] getFullForeMap() {
31 return foreMap;
32 }
34 int[][] getFullBackMap() {
35 return backMap;
36 }
38 private int transformX(int x) {
39 x = x % getWidth();
40 if (x < 0) x = getWidth() - Math.abs(x);
41 return x;
42 }
44 private int getMap(int x, int y, int layer) {
45 int map = 0;
46 try {
47 x = transformX(x);
48 map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
49 } catch (ArrayIndexOutOfBoundsException ignored) {
50 }
51 return map;
52 }
54 private void setMap(int x, int y, int layer, int value) {
55 try {
56 x = transformX(x);
57 if (layer == 0) foreMap[x][y] = value;
58 else backMap[x][y] = value;
59 } catch (ArrayIndexOutOfBoundsException ignored) {
60 }
61 }
63 public boolean hasForeAt(int x, int y) {
64 return getMap(x, y, 0) != 0;
65 }
67 public boolean hasBackAt(int x, int y) {
68 return getMap(x, y, 1) != 0;
69 }
71 public int getForeMap(int x, int y) {
72 return getMap(x, y, 0);
73 }
75 public Block getForeMapBlock(int x, int y) {
76 return GameItems.getBlock(getMap(x, y, 0));
77 }
79 public void setForeMap(int x, int y, int id) {
80 setMap(x, y, 0, id);
81 }
83 public int getBackMap(int x, int y) {
84 return getMap(x, y, 1);
85 }
87 public Block getBackMapBlock(int x, int y) {
88 return GameItems.getBlock(getMap(x, y, 1));
89 }
91 public void setBackMap(int x, int y, int id) {
92 setMap(x, y, 1, id);
93 }
95 private void placeSlab(int x, int y, int value) {
96 switch (value) {
97 case 51:
98 setForeMap(x, y, 52);
99 break;
100 case 53:
101 setForeMap(x, y, 21);
102 break;
103 case 54:
104 setForeMap(x, y, 5);
105 break;
106 case 55:
107 setForeMap(x, y, 4);
108 break;
109 case 56:
110 setForeMap(x, y, 28);
111 break;
112 case 58:
113 setForeMap(x, y, 57);
114 break;
118 public void placeToForeground(int x, int y, int value) {
119 if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
120 setForeMap(x, y, value);
121 } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
122 placeSlab(x, y, value);
124 GameProc.UPD_X = x - 8;
125 GameProc.UPD_Y = y - 8;
126 GameProc.DO_UPD = true;
129 public void placeToBackground(int x, int y, int value) {
130 if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
131 (!GameItems.getBlock(value).isTransparent() || value == 18)) {
132 setBackMap(x, y, value);
136 public void destroyForeMap(int x, int y) {
137 if (GameItems.getBlock(getForeMap(x, y)).hasDrop())
138 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
139 GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
140 placeToForeground(x, y, 0);
143 public void destroyBackMap(int x, int y) {
144 if (GameItems.getBlock(getBackMap(x, y)).hasDrop())
145 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
146 GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
147 placeToBackground(x, y, 0);
150 public void generate(int w, int h) {
151 WIDTH = w;
152 HEIGHT = h;
153 WorldGen.genWorld(WIDTH, HEIGHT);
154 foreMap = WorldGen.getForeMap();
155 backMap = WorldGen.getBackMap();
156 WorldGen.clear();
159 void setMaps(int[][] foreMap, int[][] backMap) {
160 this.foreMap = foreMap.clone();
161 this.backMap = backMap.clone();
162 WIDTH = foreMap.length;
163 HEIGHT = foreMap[0].length;