1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.utils
.Disposable
;
4 import com
.badlogic
.gdx
.utils
.TimeUtils
;
6 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingGravel
;
7 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingSand
;
8 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.Block
;
10 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
12 import javax
.annotation
.CheckForNull
;
13 import javax
.inject
.Inject
;
16 public class GameWorld
implements Disposable
{
18 private static final int DEFAULT_WIDTH
= 1024;
19 private static final int DEFAULT_HEIGHT
= 256;
20 private static final int UPDATE_RANGE
= 16;
22 private final DropController mDropController
;
23 private final MobsController mMobsController
;
24 private final GameFluidsThread mGameFluidsThread
;
26 private final int mWidth
;
27 private final int mHeight
;
28 private final int[][] mForeMap
;
29 private final int[][] mBackMap
;
31 private boolean mShouldUpdate
;
36 public GameWorld(DropController dropController
,
37 MobsController mobsController
,
38 @CheckForNull int[][] foreMap
,
39 @CheckForNull int[][] backMap
) {
40 mDropController
= dropController
;
41 mMobsController
= mobsController
;
43 boolean isNewGame
= foreMap
== null || backMap
== null;
46 mWidth
= DEFAULT_WIDTH
;
47 mHeight
= DEFAULT_HEIGHT
;
48 Pair
<int[][], int[][]> maps
= GameWorldGeneratorKt
.generate(mWidth
, mHeight
, TimeUtils
.millis());
49 mForeMap
= maps
.getFirst();
50 mBackMap
= maps
.getSecond();
51 mMobsController
.getPlayer().respawn(this);
55 mWidth
= mForeMap
.length
;
56 mHeight
= mForeMap
[0].length
;
59 mGameFluidsThread
= new GameFluidsThread(this, mMobsController
, Thread
.currentThread());
62 public int getWidth() {
66 public int getHeight() {
70 public float getWidthPx() {
74 public float getHeightPx() {
78 int[][] getFullForeMap() {
82 int[][] getFullBackMap() {
86 private int transformX(int x
) {
89 x
= getWidth() - Math
.abs(x
);
94 private int getMap(int x
, int y
, int layer
) {
98 map
= (layer
== 0) ? mForeMap
[x
][y
] : mBackMap
[x
][y
];
99 } catch (ArrayIndexOutOfBoundsException ignored
) {
104 private void setMap(int x
, int y
, int layer
, int value
) {
108 mForeMap
[x
][y
] = value
;
110 mBackMap
[x
][y
] = value
;
112 } catch (ArrayIndexOutOfBoundsException ignored
) {
116 public boolean hasForeAt(int x
, int y
) {
117 return getMap(x
, y
, 0) != 0;
120 public boolean hasBackAt(int x
, int y
) {
121 return getMap(x
, y
, 1) != 0;
124 public int getForeMap(int x
, int y
) {
125 return getMap(x
, y
, 0);
128 public Block
getForeMapBlock(int x
, int y
) {
129 return GameItems
.getBlock(getMap(x
, y
, 0));
132 public void setForeMap(int x
, int y
, int id
) {
136 public int getBackMap(int x
, int y
) {
137 return getMap(x
, y
, 1);
140 public Block
getBackMapBlock(int x
, int y
) {
141 return GameItems
.getBlock(getMap(x
, y
, 1));
144 public void setBackMap(int x
, int y
, int id
) {
148 private void placeSlab(int x
, int y
, int value
) {
151 setForeMap(x
, y
, 52);
154 setForeMap(x
, y
, 21);
163 setForeMap(x
, y
, 28);
166 setForeMap(x
, y
, 57);
171 public void placeToForeground(int x
, int y
, int value
) {
172 if (!hasForeAt(x
, y
) || value
== 0 || !GameItems
.getBlock(getForeMap(x
, y
)).hasCollision()) {
173 setForeMap(x
, y
, value
);
174 } else if (GameItems
.isSlab(value
) && getForeMap(x
, y
) == value
) {
175 placeSlab(x
, y
, value
);
179 mShouldUpdate
= true;
182 public void placeToBackground(int x
, int y
, int value
) {
183 if (value
== 0 || (getBackMap(x
, y
) == 0 && GameItems
.getBlock(value
).hasCollision()) &&
184 (!GameItems
.getBlock(value
).isTransparent() || value
== 18)) {
185 setBackMap(x
, y
, value
);
189 public void destroyForeMap(int x
, int y
) {
190 Block block
= GameItems
.getBlock(getForeMap(x
, y
));
191 if (block
.hasDrop()) {
192 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
194 placeToForeground(x
, y
, 0);
197 public void destroyBackMap(int x
, int y
) {
198 Block block
= GameItems
.getBlock(getBackMap(x
, y
));
199 if (block
.hasDrop()) {
200 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
202 placeToBackground(x
, y
, 0);
205 private void updateBlock(int x
, int y
) {
206 if (getForeMap(x
, y
) == 10) {
207 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
209 mMobsController
.addMob(FallingSand
.class, x
* 16, y
* 16);
210 updateBlock(x
, y
- 1);
214 if (getForeMap(x
, y
) == 11) {
215 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
217 mMobsController
.addMob(FallingGravel
.class, x
* 16, y
* 16);
218 updateBlock(x
, y
- 1);
222 if (hasForeAt(x
, y
) && getForeMapBlock(x
, y
).requiresBlock()) {
223 if (!hasForeAt(x
, y
+ 1) || !getForeMapBlock(x
, y
+ 1).hasCollision()) {
224 destroyForeMap(x
, y
);
225 updateBlock(x
, y
- 1);
229 if (getForeMap(x
, y
) == 2) {
230 if (hasForeAt(x
, y
- 1) && (getForeMapBlock(x
, y
- 1).hasCollision() ||
231 GameItems
.isFluid(getForeMap(x
, y
- 1)))) {
237 public void update() {
239 for (int y
= mUpdateY
; y
< mUpdateY
+ UPDATE_RANGE
; y
++) {
240 for (int x
= mUpdateX
; x
< mUpdateX
+ UPDATE_RANGE
; x
++) {
244 mShouldUpdate
= false;
247 if (!mGameFluidsThread
.isAlive()) {
248 mGameFluidsThread
.start();
252 public void startFluidsThread() {
253 mGameFluidsThread
.start();
257 public void dispose() {
258 mGameFluidsThread
.interrupt();