1 package ru
.deadsoftware
.cavedroid
.game
;
3 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingGravel
;
4 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingSand
;
5 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
6 import ru
.deadsoftware
.cavedroid
.game
.objects
.Block
;
7 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
9 import javax
.inject
.Inject
;
12 public class GameWorld
{
14 private static final int DEFAULT_WIDTH
= 1024;
15 private static final int DEFAULT_HEIGHT
= 256;
16 private static final int UPDATE_RANGE
= 16;
18 private final DropController mDropController
;
19 private final MobsController mMobsController
;
20 private final GameFluidsThread mGameFluidsThread
;
22 private final int mWidth
;
23 private final int mHeight
;
24 private final int[][] mForeMap
;
25 private final int[][] mBackMap
;
27 private boolean mShouldUpdate
;
32 GameWorld(DropController dropController
,
33 MobsController mobsController
) {
34 mDropController
= dropController
;
35 mMobsController
= mobsController
;
37 mWidth
= DEFAULT_WIDTH
;
38 mHeight
= DEFAULT_HEIGHT
;
39 WorldGen
.genWorld(mWidth
, mHeight
);
40 mForeMap
= WorldGen
.getForeMap();
41 mBackMap
= WorldGen
.getBackMap();
44 mGameFluidsThread
= new GameFluidsThread(this, mMobsController
, Thread
.currentThread());
47 // GameWorld(int[][] foreMap, int[][] backMap) {
48 // this.foreMap = foreMap.clone();
49 // this.backMap = backMap.clone();
50 // WIDTH = foreMap.length;
51 // HEIGHT = foreMap[0].length;
54 public int getWidth() {
58 public int getHeight() {
62 public float getWidthPx() {
66 public float getHeightPx() {
70 int[][] getFullForeMap() {
74 int[][] getFullBackMap() {
78 private int transformX(int x
) {
81 x
= getWidth() - Math
.abs(x
);
86 private int getMap(int x
, int y
, int layer
) {
90 map
= (layer
== 0) ? mForeMap
[x
][y
] : mBackMap
[x
][y
];
91 } catch (ArrayIndexOutOfBoundsException ignored
) {
96 private void setMap(int x
, int y
, int layer
, int value
) {
100 mForeMap
[x
][y
] = value
;
102 mBackMap
[x
][y
] = value
;
104 } catch (ArrayIndexOutOfBoundsException ignored
) {
108 public boolean hasForeAt(int x
, int y
) {
109 return getMap(x
, y
, 0) != 0;
112 public boolean hasBackAt(int x
, int y
) {
113 return getMap(x
, y
, 1) != 0;
116 public int getForeMap(int x
, int y
) {
117 return getMap(x
, y
, 0);
120 public Block
getForeMapBlock(int x
, int y
) {
121 return GameItems
.getBlock(getMap(x
, y
, 0));
124 public void setForeMap(int x
, int y
, int id
) {
128 public int getBackMap(int x
, int y
) {
129 return getMap(x
, y
, 1);
132 public Block
getBackMapBlock(int x
, int y
) {
133 return GameItems
.getBlock(getMap(x
, y
, 1));
136 public void setBackMap(int x
, int y
, int id
) {
140 private void placeSlab(int x
, int y
, int value
) {
143 setForeMap(x
, y
, 52);
146 setForeMap(x
, y
, 21);
155 setForeMap(x
, y
, 28);
158 setForeMap(x
, y
, 57);
163 public void placeToForeground(int x
, int y
, int value
) {
164 if (!hasForeAt(x
, y
) || value
== 0 || !GameItems
.getBlock(getForeMap(x
, y
)).hasCollision()) {
165 setForeMap(x
, y
, value
);
166 } else if (GameItems
.isSlab(value
) && getForeMap(x
, y
) == value
) {
167 placeSlab(x
, y
, value
);
171 mShouldUpdate
= true;
174 public void placeToBackground(int x
, int y
, int value
) {
175 if (value
== 0 || (getBackMap(x
, y
) == 0 && GameItems
.getBlock(value
).hasCollision()) &&
176 (!GameItems
.getBlock(value
).isTransparent() || value
== 18)) {
177 setBackMap(x
, y
, value
);
181 public void destroyForeMap(int x
, int y
) {
182 Block block
= GameItems
.getBlock(getForeMap(x
, y
));
183 if (block
.hasDrop()) {
184 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
186 placeToForeground(x
, y
, 0);
189 public void destroyBackMap(int x
, int y
) {
190 Block block
= GameItems
.getBlock(getBackMap(x
, y
));
191 if (block
.hasDrop()) {
192 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
194 placeToBackground(x
, y
, 0);
197 private void updateBlock(int x
, int y
) {
198 if (getForeMap(x
, y
) == 10) {
199 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
201 mMobsController
.addMob(FallingSand
.class, x
* 16, y
* 16);
202 updateBlock(x
, y
- 1);
206 if (getForeMap(x
, y
) == 11) {
207 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
209 mMobsController
.addMob(FallingGravel
.class, x
* 16, y
* 16);
210 updateBlock(x
, y
- 1);
214 if (hasForeAt(x
, y
) && getForeMapBlock(x
, y
).requiresBlock()) {
215 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
216 destroyForeMap(x
, y
);
217 updateBlock(x
, y
- 1);
221 if (getForeMap(x
, y
) == 2) {
222 if (hasForeAt(x
, y
- 1) && (getForeMapBlock(x
, y
- 1).hasCollision() ||
223 GameItems
.isFluid(getForeMap(x
, y
- 1)))) {
229 public void update() {
231 for (int y
= mUpdateY
; y
< mUpdateY
+ UPDATE_RANGE
; y
++) {
232 for (int x
= mUpdateX
; x
< mUpdateX
+ UPDATE_RANGE
; x
++) {
236 mShouldUpdate
= false;
239 if (!mGameFluidsThread
.isAlive()) {
240 mGameFluidsThread
.start();
244 public void startFluidsThread() {
245 mGameFluidsThread
.start();