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 public int[][] getFullForeMap() {
34 public 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 hasBlockAt(int x
, int y
) {
64 return getMap(x
, y
, 0) != 0;
67 public int getForeMap(int x
, int y
) {
68 return getMap(x
, y
, 0);
71 public Block
getForeMapBlock(int x
, int y
) {
72 return GameItems
.getBlock(getMap(x
, y
, 0));
75 public void setForeMap(int x
, int y
, int id
) {
79 public int getBackMap(int x
, int y
) {
80 return getMap(x
, y
, 1);
83 public Block
getBackMapBlock(int x
, int y
) {
84 return GameItems
.getBlock(getMap(x
, y
, 1));
87 public void setBackMap(int x
, int y
, int id
) {
91 private void placeSlab(int x
, int y
, int value
) {
106 setForeMap(x
, y
, 28);
109 setForeMap(x
, y
, 57);
114 public void placeToForeground(int x
, int y
, int value
) {
115 if (!hasBlockAt(x
, y
) || value
== 0 || !GameItems
.getBlock(getForeMap(x
, y
)).hasCollision()) {
116 setForeMap(x
, y
, value
);
117 } else if (GameItems
.isSlab(value
) && getForeMap(x
, y
) == value
) {
118 placeSlab(x
, y
, value
);
120 GameProc
.UPD_X
= x
- 8;
121 GameProc
.UPD_Y
= y
- 8;
122 GameProc
.DO_UPD
= true;
125 public void placeToBackground(int x
, int y
, int value
) {
126 if (value
== 0 || (getBackMap(x
, y
) == 0 && GameItems
.getBlock(value
).hasCollision()) &&
127 (!GameItems
.getBlock(value
).isTransparent() || value
== 18)) {
128 setBackMap(x
, y
, value
);
132 public void destroyForeMap(int x
, int y
) {
133 if (GameItems
.getBlock(getForeMap(x
, y
)).hasDrop())
134 GP
.drops
.add(new Drop(transformX(x
) * 16 + 4, y
* 16 + 4,
135 GameItems
.getItemId(GameItems
.getBlock(getForeMap(x
, y
)).getDrop())));
136 placeToForeground(x
, y
, 0);
139 public void destroyBackMap(int x
, int y
) {
140 if (GameItems
.getBlock(getBackMap(x
, y
)).hasDrop())
141 GP
.drops
.add(new Drop(transformX(x
) * 16 + 4, y
* 16 + 4,
142 GameItems
.getItemId(GameItems
.getBlock(getBackMap(x
, y
)).getDrop())));
143 placeToBackground(x
, y
, 0);
146 public void generate(int w
, int h
) {
149 WorldGen
.genWorld(WIDTH
, HEIGHT
);
150 foreMap
= WorldGen
.getForeMap();
151 backMap
= WorldGen
.getBackMap();
155 void setMaps(int[][] foreMap
, int[][] backMap
) {
156 this.foreMap
= foreMap
.clone();
157 this.backMap
= backMap
.clone();
158 WIDTH
= foreMap
.length
;
159 HEIGHT
= foreMap
[0].length
;