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 int WIDTH
, HEIGHT
;
13 private int[][] foreMap
;
14 private int[][] backMap
;
16 GameWorld(int width
, int height
) {
19 WorldGen
.genWorld(WIDTH
, HEIGHT
);
20 foreMap
= WorldGen
.getForeMap();
21 backMap
= WorldGen
.getBackMap();
25 GameWorld(@NotNull int[][] foreMap
, @NotNull 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
) {
58 if (x
< 0) x
= getWidth() - Math
.abs(x
);
62 private int getMap(int x
, int y
, int layer
) {
66 map
= (layer
== 0) ? foreMap
[x
][y
] : backMap
[x
][y
];
67 } catch (ArrayIndexOutOfBoundsException ignored
) {
72 private void setMap(int x
, int y
, int layer
, int value
) {
75 if (layer
== 0) foreMap
[x
][y
] = value
;
76 else backMap
[x
][y
] = value
;
77 } catch (ArrayIndexOutOfBoundsException ignored
) {
81 public boolean hasForeAt(int x
, int y
) {
82 return getMap(x
, y
, 0) != 0;
85 public boolean hasBackAt(int x
, int y
) {
86 return getMap(x
, y
, 1) != 0;
89 public int getForeMap(int x
, int y
) {
90 return getMap(x
, y
, 0);
93 public Block
getForeMapBlock(int x
, int y
) {
94 return GameItems
.getBlock(getMap(x
, y
, 0));
97 public void setForeMap(int x
, int y
, int id
) {
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
) {
113 private void placeSlab(int x
, int y
, int value
) {
116 setForeMap(x
, y
, 52);
119 setForeMap(x
, y
, 21);
128 setForeMap(x
, y
, 28);
131 setForeMap(x
, y
, 57);
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);