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
.item
.InventoryItem
;
9 import ru
.deadsoftware
.cavedroid
.game
.model
.item
.Item
;
10 import ru
.deadsoftware
.cavedroid
.game
.model
.world
.generator
.WorldGeneratorConfig
;
11 import ru
.deadsoftware
.cavedroid
.game
.objects
.drop
.DropController
;
12 import ru
.deadsoftware
.cavedroid
.game
.objects
.furnace
.Furnace
;
13 import ru
.deadsoftware
.cavedroid
.game
.objects
.furnace
.FurnaceController
;
14 import ru
.deadsoftware
.cavedroid
.misc
.utils
.MeasureUnitsUtilsKt
;
16 import javax
.annotation
.CheckForNull
;
17 import javax
.inject
.Inject
;
20 public class GameWorld
{
22 private final DropController mDropController
;
23 private final MobsController mMobsController
;
24 private final GameItemsHolder mGameItemsHolder
;
25 private final FurnaceController mFurnaceController
;
27 private final int mWidth
;
28 private final int mHeight
;
29 private final Block
[][] mForeMap
;
30 private final Block
[][] mBackMap
;
32 private final WorldGeneratorConfig mWorldConfig
= WorldGeneratorConfig
.Companion
.getDefault();
35 public GameWorld(DropController dropController
,
36 MobsController mobsController
,
37 GameItemsHolder gameItemsHolder
,
38 FurnaceController furnaceController
,
39 @CheckForNull Block
[][] foreMap
,
40 @CheckForNull Block
[][] backMap
) {
41 mDropController
= dropController
;
42 mMobsController
= mobsController
;
43 mGameItemsHolder
= gameItemsHolder
;
44 mFurnaceController
= furnaceController
;
46 boolean isNewGame
= foreMap
== null || backMap
== null;
49 mWidth
= mWorldConfig
.getWidth();
50 mHeight
= mWorldConfig
.getHeight();
51 Pair
<Block
[][], Block
[][]> maps
= new GameWorldGenerator(mWorldConfig
, mGameItemsHolder
).generate();
52 mForeMap
= maps
.getFirst();
53 mBackMap
= maps
.getSecond();
54 mMobsController
.getPlayer().respawn(this, mGameItemsHolder
);
58 mWidth
= mForeMap
.length
;
59 mHeight
= mForeMap
[0].length
;
63 public int getWidth() {
67 public int getHeight() {
72 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
75 public float getWidthPx() {
76 return MeasureUnitsUtilsKt
.getPx(mWidth
);
80 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
83 public float getHeightPx() {
84 return MeasureUnitsUtilsKt
.getPx(mHeight
);
87 public Block
[][] getFullForeMap() {
91 public Block
[][] getFullBackMap() {
95 private int transformX(int x
) {
98 x
= getWidth() - Math
.abs(x
);
103 private Block
getMap(int x
, int y
, int layer
) {
104 Block map
= mGameItemsHolder
.getFallbackBlock();
106 if (y
< 0 || y
>= getHeight()) {
112 if (x
< 0 || x
>= getWidth()) {
116 map
= (layer
== 0) ? mForeMap
[x
][y
] : mBackMap
[x
][y
];
121 private void setMap(int x
, int y
, int layer
, Block value
) {
122 if (y
< 0 || y
>= getHeight()) {
128 if (x
< 0 || x
>= getWidth()) {
133 mForeMap
[x
][y
] = value
;
135 mBackMap
[x
][y
] = value
;
139 private boolean isSameSlab(Block slab1
, Block slab2
) {
140 if (!(slab1
instanceof Block
.Slab
) || !(slab2
instanceof Block
.Slab
)) {
144 return slab1
.getParams().getKey().equals(((Block
.Slab
) slab2
).getOtherPartBlockKey())
145 || slab1
.getParams().getKey().equals(slab2
.getParams().getKey());
148 public boolean hasForeAt(int x
, int y
) {
149 return getMap(x
, y
, 0) != mGameItemsHolder
.getFallbackBlock();
152 public boolean hasBackAt(int x
, int y
) {
153 return getMap(x
, y
, 1) != mGameItemsHolder
.getFallbackBlock();
156 public Block
getForeMap(int x
, int y
) {
157 return getMap(x
, y
, 0);
160 public void setForeMap(int x
, int y
, Block block
) {
161 setMap(x
, y
, 0, block
);
164 public void resetForeMap(int x
, int y
) {
165 setForeMap(x
, y
, mGameItemsHolder
.getFallbackBlock());
168 public Block
getBackMap(int x
, int y
) {
169 return getMap(x
, y
, 1);
172 public void setBackMap(int x
, int y
, Block block
) {
173 setMap(x
, y
, 1, block
);
176 public boolean placeToForeground(int x
, int y
, Block value
) {
177 if (!hasForeAt(x
, y
) || value
== mGameItemsHolder
.getFallbackBlock() || !getForeMap(x
, y
).hasCollision()) {
179 if (value
.isFurnace()) {
180 mFurnaceController
.addFurnace(x
, y
, 0);
183 setForeMap(x
, y
, value
);
185 } else if (value
instanceof Block
.Slab
&& isSameSlab(value
, getForeMap(x
, y
))) {
186 setForeMap(x
, y
, mGameItemsHolder
.getBlock(((Block
.Slab
) value
).getFullBlockKey()));
192 public boolean placeToBackground(int x
, int y
, Block value
) {
193 if (value
== mGameItemsHolder
.getFallbackBlock() || (getBackMap(x
, y
) == mGameItemsHolder
.getFallbackBlock() && value
.hasCollision()) &&
194 (!value
.isTransparent() || value
== mGameItemsHolder
.getBlock("glass"))) {
195 if (value
.isFurnace()) {
196 mFurnaceController
.addFurnace(x
, y
, 1);
198 setBackMap(x
, y
, value
);
204 private void playerDurateTool() {
205 final InventoryItem playerCurrentItem
= mMobsController
.getPlayer().inventory
.getActiveItem();
206 if (playerCurrentItem
.getItem().isTool()) {
207 mMobsController
.getPlayer().decreaseCurrentItemCount(mGameItemsHolder
);
211 private boolean shouldDrop(Block block
) {
212 final Item item
= mMobsController
.getPlayer().inventory
.getActiveItem().getItem();
213 int toolLevel
= item
.isTool() ?
((Item
.Tool
) item
).getLevel() : 0;
214 if (item
.isTool() && block
.getParams().getToolType() != item
.getClass()) {
217 return toolLevel
>= block
.getParams().getToolLevel();
220 public void destroyForeMap(int x
, int y
) {
221 Block block
= getForeMap(x
, y
);
222 if (block
.isFurnace()) {
223 mFurnaceController
.destroyFurnace(x
, y
, 0, mDropController
);
225 if (block
.hasDrop() && shouldDrop(block
)) {
226 for (int i
= 0; i
< block
.getParams().getDropInfo().getCount(); i
++) {
227 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, mGameItemsHolder
.getItem(block
.getDrop()));
231 placeToForeground(x
, y
, mGameItemsHolder
.getFallbackBlock());
234 public WorldGeneratorConfig
getWorldConfig() {
238 public void destroyBackMap(int x
, int y
) {
239 Block block
= getBackMap(x
, y
);
240 if (block
.isFurnace()) {
241 mFurnaceController
.destroyFurnace(x
, y
, 0, mDropController
);
243 if (block
.hasDrop() && shouldDrop(block
)) {
244 for (int i
= 0; i
< block
.getParams().getDropInfo().getCount(); i
++) {
245 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, mGameItemsHolder
.getItem(block
.getDrop()));
249 placeToBackground(x
, y
, mGameItemsHolder
.getFallbackBlock());
253 public Furnace
getForegroundFurnace(int x
, int y
) {
254 return mFurnaceController
.getFurnace(x
, y
, 0);
258 public Furnace
getBackgroundFurnace(int x
, int y
) {
259 return mFurnaceController
.getFurnace(x
, y
, 1);