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
) {
20 WorldGen
.genWorld(WIDTH
, HEIGHT
);
21 foreMap
= WorldGen
.getForeMap();
22 backMap
= WorldGen
.getBackMap();
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
;
33 public int getWidth() {
37 public int getHeight() {
41 public float getWidthPx() {
45 public float getHeightPx() {
49 int[][] getFullForeMap() {
53 int[][] getFullBackMap() {
57 private int transformX(int x
) {
59 if (x
< 0) x
= getWidth() - Math
.abs(x
);
63 private int getMap(int x
, int y
, int layer
) {
67 map
= (layer
== 0) ? foreMap
[x
][y
] : backMap
[x
][y
];
68 } catch (ArrayIndexOutOfBoundsException ignored
) {
73 private void setMap(int x
, int y
, int layer
, int value
) {
76 if (layer
== 0) foreMap
[x
][y
] = value
;
77 else backMap
[x
][y
] = value
;
78 } catch (ArrayIndexOutOfBoundsException ignored
) {
82 public boolean hasForeAt(int x
, int y
) {
83 return getMap(x
, y
, 0) != 0;
86 public boolean hasBackAt(int x
, int y
) {
87 return getMap(x
, y
, 1) != 0;
90 public int getForeMap(int x
, int y
) {
91 return getMap(x
, y
, 0);
94 public Block
getForeMapBlock(int x
, int y
) {
95 return GameItems
.getBlock(getMap(x
, y
, 0));
98 public void setForeMap(int x
, int y
, int 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
) {
114 private void placeSlab(int x
, int y
, int value
) {
117 setForeMap(x
, y
, 52);
120 setForeMap(x
, y
, 21);
129 setForeMap(x
, y
, 28);
132 setForeMap(x
, y
, 57);
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);