DEADSOFTWARE

Inspect code
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameWorld.java
1 package ru.deadsoftware.cavedroid.game;
3 import org.jetbrains.annotations.NotNull;
4 import ru.deadsoftware.cavedroid.game.objects.Block;
5 import ru.deadsoftware.cavedroid.game.objects.Drop;
7 import static ru.deadsoftware.cavedroid.GameScreen.GP;
9 @SuppressWarnings("WeakerAccess")
10 public class GameWorld {
12 private final int WIDTH;
13 private final int HEIGHT;
14 private final int[][] foreMap;
15 private final int[][] backMap;
17 GameWorld(int width, int height) {
18 WIDTH = width;
19 HEIGHT = height;
20 WorldGen.genWorld(WIDTH, HEIGHT);
21 foreMap = WorldGen.getForeMap();
22 backMap = WorldGen.getBackMap();
23 WorldGen.clear();
24 }
26 GameWorld(@NotNull int[][] foreMap, @NotNull int[][] backMap) {
27 this.foreMap = foreMap.clone();
28 this.backMap = backMap.clone();
29 WIDTH = foreMap.length;
30 HEIGHT = foreMap[0].length;
31 }
33 public int getWidth() {
34 return WIDTH;
35 }
37 public int getHeight() {
38 return HEIGHT;
39 }
41 public float getWidthPx() {
42 return WIDTH * 16f;
43 }
45 public float getHeightPx() {
46 return HEIGHT * 16f;
47 }
49 int[][] getFullForeMap() {
50 return foreMap;
51 }
53 int[][] getFullBackMap() {
54 return backMap;
55 }
57 private int transformX(int x) {
58 x = x % getWidth();
59 if (x < 0) x = getWidth() - Math.abs(x);
60 return x;
61 }
63 private int getMap(int x, int y, int layer) {
64 int map = 0;
65 try {
66 x = transformX(x);
67 map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
68 } catch (ArrayIndexOutOfBoundsException ignored) {
69 }
70 return map;
71 }
73 private void setMap(int x, int y, int layer, int value) {
74 try {
75 x = transformX(x);
76 if (layer == 0) foreMap[x][y] = value;
77 else backMap[x][y] = value;
78 } catch (ArrayIndexOutOfBoundsException ignored) {
79 }
80 }
82 public boolean hasForeAt(int x, int y) {
83 return getMap(x, y, 0) != 0;
84 }
86 public boolean hasBackAt(int x, int y) {
87 return getMap(x, y, 1) != 0;
88 }
90 public int getForeMap(int x, int y) {
91 return getMap(x, y, 0);
92 }
94 public Block getForeMapBlock(int x, int y) {
95 return GameItems.getBlock(getMap(x, y, 0));
96 }
98 public void setForeMap(int x, int y, int id) {
99 setMap(x, y, 0, id);
102 public int getBackMap(int x, int y) {
103 return getMap(x, y, 1);
106 public Block getBackMapBlock(int x, int y) {
107 return GameItems.getBlock(getMap(x, y, 1));
110 public void setBackMap(int x, int y, int id) {
111 setMap(x, y, 1, id);
114 private void placeSlab(int x, int y, int value) {
115 switch (value) {
116 case 51:
117 setForeMap(x, y, 52);
118 break;
119 case 53:
120 setForeMap(x, y, 21);
121 break;
122 case 54:
123 setForeMap(x, y, 5);
124 break;
125 case 55:
126 setForeMap(x, y, 4);
127 break;
128 case 56:
129 setForeMap(x, y, 28);
130 break;
131 case 58:
132 setForeMap(x, y, 57);
133 break;
137 public void placeToForeground(int x, int y, int value) {
138 if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
139 setForeMap(x, y, value);
140 } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
141 placeSlab(x, y, value);
143 GameProc.UPD_X = x - 8;
144 GameProc.UPD_Y = y - 8;
145 GameProc.DO_UPD = true;
148 public void placeToBackground(int x, int y, int value) {
149 if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
150 (!GameItems.getBlock(value).isTransparent() || value == 18)) {
151 setBackMap(x, y, value);
155 public void destroyForeMap(int x, int y) {
156 if (GameItems.getBlock(getForeMap(x, y)).hasDrop())
157 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
158 GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
159 placeToForeground(x, y, 0);
162 public void destroyBackMap(int x, int y) {
163 if (GameItems.getBlock(getBackMap(x, y)).hasDrop())
164 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
165 GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
166 placeToBackground(x, y, 0);