DEADSOFTWARE

Nonnull by default
[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 @SuppressWarnings("WeakerAccess")
9 public class GameWorld {
11 private final int WIDTH;
12 private final int HEIGHT;
13 private final int[][] foreMap;
14 private final int[][] backMap;
16 GameWorld(int width, int height) {
17 WIDTH = width;
18 HEIGHT = height;
19 WorldGen.genWorld(WIDTH, HEIGHT);
20 foreMap = WorldGen.getForeMap();
21 backMap = WorldGen.getBackMap();
22 WorldGen.clear();
23 }
25 GameWorld(int[][] foreMap, int[][] backMap) {
26 this.foreMap = foreMap.clone();
27 this.backMap = backMap.clone();
28 WIDTH = foreMap.length;
29 HEIGHT = foreMap[0].length;
30 }
32 public int getWidth() {
33 return WIDTH;
34 }
36 public int getHeight() {
37 return HEIGHT;
38 }
40 public float getWidthPx() {
41 return WIDTH * 16f;
42 }
44 public float getHeightPx() {
45 return HEIGHT * 16f;
46 }
48 int[][] getFullForeMap() {
49 return foreMap;
50 }
52 int[][] getFullBackMap() {
53 return backMap;
54 }
56 private int transformX(int x) {
57 x = x % getWidth();
58 if (x < 0) x = getWidth() - Math.abs(x);
59 return x;
60 }
62 private int getMap(int x, int y, int layer) {
63 int map = 0;
64 try {
65 x = transformX(x);
66 map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
67 } catch (ArrayIndexOutOfBoundsException ignored) {
68 }
69 return map;
70 }
72 private void setMap(int x, int y, int layer, int value) {
73 try {
74 x = transformX(x);
75 if (layer == 0) foreMap[x][y] = value;
76 else backMap[x][y] = value;
77 } catch (ArrayIndexOutOfBoundsException ignored) {
78 }
79 }
81 public boolean hasForeAt(int x, int y) {
82 return getMap(x, y, 0) != 0;
83 }
85 public boolean hasBackAt(int x, int y) {
86 return getMap(x, y, 1) != 0;
87 }
89 public int getForeMap(int x, int y) {
90 return getMap(x, y, 0);
91 }
93 public Block getForeMapBlock(int x, int y) {
94 return GameItems.getBlock(getMap(x, y, 0));
95 }
97 public void setForeMap(int x, int y, int id) {
98 setMap(x, y, 0, id);
99 }
101 public int getBackMap(int x, int y) {
102 return getMap(x, y, 1);
105 public Block getBackMapBlock(int x, int y) {
106 return GameItems.getBlock(getMap(x, y, 1));
109 public void setBackMap(int x, int y, int id) {
110 setMap(x, y, 1, id);
113 private void placeSlab(int x, int y, int value) {
114 switch (value) {
115 case 51:
116 setForeMap(x, y, 52);
117 break;
118 case 53:
119 setForeMap(x, y, 21);
120 break;
121 case 54:
122 setForeMap(x, y, 5);
123 break;
124 case 55:
125 setForeMap(x, y, 4);
126 break;
127 case 56:
128 setForeMap(x, y, 28);
129 break;
130 case 58:
131 setForeMap(x, y, 57);
132 break;
136 public void placeToForeground(int x, int y, int value) {
137 if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
138 setForeMap(x, y, value);
139 } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
140 placeSlab(x, y, value);
142 GameProc.UPD_X = x - 8;
143 GameProc.UPD_Y = y - 8;
144 GameProc.DO_UPD = true;
147 public void placeToBackground(int x, int y, int value) {
148 if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
149 (!GameItems.getBlock(value).isTransparent() || value == 18)) {
150 setBackMap(x, y, value);
154 public void destroyForeMap(int x, int y) {
155 if (GameItems.getBlock(getForeMap(x, y)).hasDrop())
156 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
157 GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
158 placeToForeground(x, y, 0);
161 public void destroyBackMap(int x, int y) {
162 if (GameItems.getBlock(getBackMap(x, y)).hasDrop())
163 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
164 GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
165 placeToBackground(x, y, 0);