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
) {
19 WorldGen
.genWorld(WIDTH
, HEIGHT
);
20 foreMap
= WorldGen
.getForeMap();
21 backMap
= WorldGen
.getBackMap();
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
;
32 public int getWidth() {
36 public int getHeight() {
40 public float getWidthPx() {
44 public float getHeightPx() {
48 int[][] getFullForeMap() {
52 int[][] getFullBackMap() {
56 private int transformX(int x
) {
59 x
= getWidth() - Math
.abs(x
);
64 private int getMap(int x
, int y
, int layer
) {
68 map
= (layer
== 0) ? foreMap
[x
][y
] : backMap
[x
][y
];
69 } catch (ArrayIndexOutOfBoundsException ignored
) {
74 private void setMap(int x
, int y
, int layer
, int value
) {
78 foreMap
[x
][y
] = value
;
80 backMap
[x
][y
] = value
;
82 } catch (ArrayIndexOutOfBoundsException ignored
) {
86 public boolean hasForeAt(int x
, int y
) {
87 return getMap(x
, y
, 0) != 0;
90 public boolean hasBackAt(int x
, int y
) {
91 return getMap(x
, y
, 1) != 0;
94 public int getForeMap(int x
, int y
) {
95 return getMap(x
, y
, 0);
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
) {
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
) {
118 private void placeSlab(int x
, int y
, int value
) {
121 setForeMap(x
, y
, 52);
124 setForeMap(x
, y
, 21);
133 setForeMap(x
, y
, 28);
136 setForeMap(x
, y
, 57);
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);