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 public class GameWorld
{
10 private int WIDTH
, HEIGHT
;
11 private int[][] foreMap
;
12 private int[][] backMap
;
14 public int getWidth() {
18 public int getHeight() {
22 public float getWidthPx() {
26 public float getHeightPx() {
30 int[][] getFullForeMap() {
34 int[][] getFullBackMap() {
38 private int transformX(int x
) {
40 if (x
< 0) x
= getWidth() - Math
.abs(x
);
44 private int getMap(int x
, int y
, int layer
) {
48 map
= (layer
== 0) ? foreMap
[x
][y
] : backMap
[x
][y
];
49 } catch (ArrayIndexOutOfBoundsException ignored
) {
54 private void setMap(int x
, int y
, int layer
, int value
) {
57 if (layer
== 0) foreMap
[x
][y
] = value
;
58 else backMap
[x
][y
] = value
;
59 } catch (ArrayIndexOutOfBoundsException ignored
) {
63 public boolean hasForeAt(int x
, int y
) {
64 return getMap(x
, y
, 0) != 0;
67 public boolean hasBackAt(int x
, int y
) {
68 return getMap(x
, y
, 1) != 0;
71 public int getForeMap(int x
, int y
) {
72 return getMap(x
, y
, 0);
75 public Block
getForeMapBlock(int x
, int y
) {
76 return GameItems
.getBlock(getMap(x
, y
, 0));
79 public void setForeMap(int x
, int y
, int id
) {
83 public int getBackMap(int x
, int y
) {
84 return getMap(x
, y
, 1);
87 public Block
getBackMapBlock(int x
, int y
) {
88 return GameItems
.getBlock(getMap(x
, y
, 1));
91 public void setBackMap(int x
, int y
, int id
) {
95 private void placeSlab(int x
, int y
, int value
) {
101 setForeMap(x
, y
, 21);
110 setForeMap(x
, y
, 28);
113 setForeMap(x
, y
, 57);
118 public void placeToForeground(int x
, int y
, int value
) {
119 if (!hasForeAt(x
, y
) || value
== 0 || !GameItems
.getBlock(getForeMap(x
, y
)).hasCollision()) {
120 setForeMap(x
, y
, value
);
121 } else if (GameItems
.isSlab(value
) && getForeMap(x
, y
) == value
) {
122 placeSlab(x
, y
, value
);
124 GameProc
.UPD_X
= x
- 8;
125 GameProc
.UPD_Y
= y
- 8;
126 GameProc
.DO_UPD
= true;
129 public void placeToBackground(int x
, int y
, int value
) {
130 if (value
== 0 || (getBackMap(x
, y
) == 0 && GameItems
.getBlock(value
).hasCollision()) &&
131 (!GameItems
.getBlock(value
).isTransparent() || value
== 18)) {
132 setBackMap(x
, y
, value
);
136 public void destroyForeMap(int x
, int y
) {
137 if (GameItems
.getBlock(getForeMap(x
, y
)).hasDrop())
138 GP
.drops
.add(new Drop(transformX(x
) * 16 + 4, y
* 16 + 4,
139 GameItems
.getItemId(GameItems
.getBlock(getForeMap(x
, y
)).getDrop())));
140 placeToForeground(x
, y
, 0);
143 public void destroyBackMap(int x
, int y
) {
144 if (GameItems
.getBlock(getBackMap(x
, y
)).hasDrop())
145 GP
.drops
.add(new Drop(transformX(x
) * 16 + 4, y
* 16 + 4,
146 GameItems
.getItemId(GameItems
.getBlock(getBackMap(x
, y
)).getDrop())));
147 placeToBackground(x
, y
, 0);
150 public void generate(int w
, int h
) {
153 WorldGen
.genWorld(WIDTH
, HEIGHT
);
154 foreMap
= WorldGen
.getForeMap();
155 backMap
= WorldGen
.getBackMap();
159 void setMaps(int[][] foreMap
, int[][] backMap
) {
160 this.foreMap
= foreMap
.clone();
161 this.backMap
= backMap
.clone();
162 WIDTH
= foreMap
.length
;
163 HEIGHT
= foreMap
[0].length
;