DEADSOFTWARE

Fix codestyle
[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) {
59 x = getWidth() - Math.abs(x);
60 }
61 return x;
62 }
64 private int getMap(int x, int y, int layer) {
65 int map = 0;
66 try {
67 x = transformX(x);
68 map = (layer == 0) ? foreMap[x][y] : backMap[x][y];
69 } catch (ArrayIndexOutOfBoundsException ignored) {
70 }
71 return map;
72 }
74 private void setMap(int x, int y, int layer, int value) {
75 try {
76 x = transformX(x);
77 if (layer == 0) {
78 foreMap[x][y] = value;
79 } else {
80 backMap[x][y] = value;
81 }
82 } catch (ArrayIndexOutOfBoundsException ignored) {
83 }
84 }
86 public boolean hasForeAt(int x, int y) {
87 return getMap(x, y, 0) != 0;
88 }
90 public boolean hasBackAt(int x, int y) {
91 return getMap(x, y, 1) != 0;
92 }
94 public int getForeMap(int x, int y) {
95 return getMap(x, y, 0);
96 }
98 public Block getForeMapBlock(int x, int y) {
99 return GameItems.getBlock(getMap(x, y, 0));
102 public void setForeMap(int x, int y, int id) {
103 setMap(x, y, 0, id);
106 public int getBackMap(int x, int y) {
107 return getMap(x, y, 1);
110 public Block getBackMapBlock(int x, int y) {
111 return GameItems.getBlock(getMap(x, y, 1));
114 public void setBackMap(int x, int y, int id) {
115 setMap(x, y, 1, id);
118 private void placeSlab(int x, int y, int value) {
119 switch (value) {
120 case 51:
121 setForeMap(x, y, 52);
122 break;
123 case 53:
124 setForeMap(x, y, 21);
125 break;
126 case 54:
127 setForeMap(x, y, 5);
128 break;
129 case 55:
130 setForeMap(x, y, 4);
131 break;
132 case 56:
133 setForeMap(x, y, 28);
134 break;
135 case 58:
136 setForeMap(x, y, 57);
137 break;
141 public void placeToForeground(int x, int y, int value) {
142 if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
143 setForeMap(x, y, value);
144 } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
145 placeSlab(x, y, value);
147 GameProc.UPD_X = x - 8;
148 GameProc.UPD_Y = y - 8;
149 GameProc.DO_UPD = true;
152 public void placeToBackground(int x, int y, int value) {
153 if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
154 (!GameItems.getBlock(value).isTransparent() || value == 18)) {
155 setBackMap(x, y, value);
159 public void destroyForeMap(int x, int y) {
160 if (GameItems.getBlock(getForeMap(x, y)).hasDrop()) {
161 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
162 GameItems.getItemId(GameItems.getBlock(getForeMap(x, y)).getDrop())));
164 placeToForeground(x, y, 0);
167 public void destroyBackMap(int x, int y) {
168 if (GameItems.getBlock(getBackMap(x, y)).hasDrop()) {
169 GP.drops.add(new Drop(transformX(x) * 16 + 4, y * 16 + 4,
170 GameItems.getItemId(GameItems.getBlock(getBackMap(x, y)).getDrop())));
172 placeToBackground(x, y, 0);