1 package ru
.deadsoftware
.cavedroid
.game
.world
;
4 import ru
.deadsoftware
.cavedroid
.game
.GameItemsHolder
;
5 import ru
.deadsoftware
.cavedroid
.game
.GameScope
;
6 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
7 import ru
.deadsoftware
.cavedroid
.game
.model
.block
.Block
;
8 import ru
.deadsoftware
.cavedroid
.game
.model
.world
.generator
.WorldGeneratorConfig
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
10 import ru
.deadsoftware
.cavedroid
.misc
.utils
.MeasureUnitsUtilsKt
;
12 import javax
.annotation
.CheckForNull
;
13 import javax
.inject
.Inject
;
16 public class GameWorld
{
18 private final DropController mDropController
;
19 private final MobsController mMobsController
;
20 private final GameItemsHolder mGameItemsHolder
;
22 private final int mWidth
;
23 private final int mHeight
;
24 private final Block
[][] mForeMap
;
25 private final Block
[][] mBackMap
;
28 public GameWorld(DropController dropController
,
29 MobsController mobsController
,
30 GameItemsHolder gameItemsHolder
,
31 @CheckForNull Block
[][] foreMap
,
32 @CheckForNull Block
[][] backMap
) {
33 mDropController
= dropController
;
34 mMobsController
= mobsController
;
35 mGameItemsHolder
= gameItemsHolder
;
37 boolean isNewGame
= foreMap
== null || backMap
== null;
40 final WorldGeneratorConfig config
= WorldGeneratorConfig
.Companion
.getDefault();
41 mWidth
= config
.getWidth();
42 mHeight
= config
.getHeight();
43 Pair
<Block
[][], Block
[][]> maps
= new GameWorldGenerator(config
, mGameItemsHolder
).generate();
44 mForeMap
= maps
.getFirst();
45 mBackMap
= maps
.getSecond();
46 mMobsController
.getPlayer().respawn(this, mGameItemsHolder
);
50 mWidth
= mForeMap
.length
;
51 mHeight
= mForeMap
[0].length
;
55 public int getWidth() {
59 public int getHeight() {
64 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
67 public float getWidthPx() {
68 return MeasureUnitsUtilsKt
.getPx(mWidth
);
72 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
75 public float getHeightPx() {
76 return MeasureUnitsUtilsKt
.getPx(mHeight
);
79 public Block
[][] getFullForeMap() {
83 public Block
[][] getFullBackMap() {
87 private int transformX(int x
) {
90 x
= getWidth() - Math
.abs(x
);
95 private Block
getMap(int x
, int y
, int layer
) {
96 Block map
= mGameItemsHolder
.getFallbackBlock();
99 map
= (layer
== 0) ? mForeMap
[x
][y
] : mBackMap
[x
][y
];
100 } catch (ArrayIndexOutOfBoundsException ignored
) {
105 private void setMap(int x
, int y
, int layer
, Block value
) {
109 mForeMap
[x
][y
] = value
;
111 mBackMap
[x
][y
] = value
;
113 } catch (ArrayIndexOutOfBoundsException ignored
) {
117 public boolean hasForeAt(int x
, int y
) {
118 return getMap(x
, y
, 0) != mGameItemsHolder
.getFallbackBlock();
121 public boolean hasBackAt(int x
, int y
) {
122 return getMap(x
, y
, 1) != mGameItemsHolder
.getFallbackBlock();
125 public Block
getForeMap(int x
, int y
) {
126 return getMap(x
, y
, 0);
129 public void setForeMap(int x
, int y
, Block block
) {
130 setMap(x
, y
, 0, block
);
133 public void resetForeMap(int x
, int y
) {
134 setForeMap(x
, y
, mGameItemsHolder
.getFallbackBlock());
137 public Block
getBackMap(int x
, int y
) {
138 return getMap(x
, y
, 1);
141 public void setBackMap(int x
, int y
, Block block
) {
142 setMap(x
, y
, 1, block
);
145 public void placeToForeground(int x
, int y
, Block value
) {
146 if (!hasForeAt(x
, y
) || value
== mGameItemsHolder
.getFallbackBlock() || !getForeMap(x
, y
).hasCollision()) {
147 setForeMap(x
, y
, value
);
148 } else if (value
instanceof Block
.Slab
&& getForeMap(x
, y
) == value
) {
149 setForeMap(x
, y
, mGameItemsHolder
.getBlock(((Block
.Slab
) value
).getFullBlockKey()));
153 public void placeToBackground(int x
, int y
, Block value
) {
154 if (value
== mGameItemsHolder
.getFallbackBlock() || (getBackMap(x
, y
) == mGameItemsHolder
.getFallbackBlock() && value
.hasCollision()) &&
155 (!value
.isTransparent() || value
== mGameItemsHolder
.getBlock("glass"))) {
156 setBackMap(x
, y
, value
);
160 public void destroyForeMap(int x
, int y
) {
161 Block block
= getForeMap(x
, y
);
162 if (block
.hasDrop()) {
163 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, mGameItemsHolder
.getItem(block
.getDrop()));
165 placeToForeground(x
, y
, mGameItemsHolder
.getFallbackBlock());
168 public void destroyBackMap(int x
, int y
) {
169 Block block
= getBackMap(x
, y
);
170 if (block
.hasDrop()) {
171 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, mGameItemsHolder
.getItem(block
.getDrop()));
173 placeToBackground(x
, y
, mGameItemsHolder
.getFallbackBlock());