1 package ru
.deadsoftware
.cavedroid
.game
.world
;
4 import ru
.deadsoftware
.cavedroid
.game
.GameItems
;
5 import ru
.deadsoftware
.cavedroid
.game
.GameItemsHolder
;
6 import ru
.deadsoftware
.cavedroid
.game
.GameScope
;
7 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
8 import ru
.deadsoftware
.cavedroid
.game
.model
.block
.Block
;
9 import ru
.deadsoftware
.cavedroid
.game
.model
.world
.generator
.WorldGeneratorConfig
;
10 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
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() {
63 public float getWidthPx() {
67 public float getHeightPx() {
71 public Block
[][] getFullForeMap() {
75 public Block
[][] getFullBackMap() {
79 private int transformX(int x
) {
82 x
= getWidth() - Math
.abs(x
);
87 private Block
getMap(int x
, int y
, int layer
) {
88 Block map
= mGameItemsHolder
.getFallbackBlock();
91 map
= (layer
== 0) ? mForeMap
[x
][y
] : mBackMap
[x
][y
];
92 } catch (ArrayIndexOutOfBoundsException ignored
) {
97 private void setMap(int x
, int y
, int layer
, Block value
) {
101 mForeMap
[x
][y
] = value
;
103 mBackMap
[x
][y
] = value
;
105 } catch (ArrayIndexOutOfBoundsException ignored
) {
109 public boolean hasForeAt(int x
, int y
) {
110 return getMap(x
, y
, 0) != mGameItemsHolder
.getFallbackBlock();
113 public boolean hasBackAt(int x
, int y
) {
114 return getMap(x
, y
, 1) != mGameItemsHolder
.getFallbackBlock();
117 public Block
getForeMap(int x
, int y
) {
118 return getMap(x
, y
, 0);
121 public void setForeMap(int x
, int y
, Block block
) {
122 setMap(x
, y
, 0, block
);
125 public void resetForeMap(int x
, int y
) {
126 setForeMap(x
, y
, mGameItemsHolder
.getFallbackBlock());
129 public Block
getBackMap(int x
, int y
) {
130 return getMap(x
, y
, 1);
133 public void setBackMap(int x
, int y
, Block block
) {
134 setMap(x
, y
, 1, block
);
137 public void placeToForeground(int x
, int y
, Block value
) {
138 if (!hasForeAt(x
, y
) || value
== mGameItemsHolder
.getFallbackBlock() || !getForeMap(x
, y
).hasCollision()) {
139 setForeMap(x
, y
, value
);
140 } else if (value
instanceof Block
.Slab
&& getForeMap(x
, y
) == value
) {
141 setForeMap(x
, y
, mGameItemsHolder
.getBlock(((Block
.Slab
) value
).getFullBlockKey()));
145 public void placeToBackground(int x
, int y
, Block value
) {
146 if (value
== mGameItemsHolder
.getFallbackBlock() || (getBackMap(x
, y
) == mGameItemsHolder
.getFallbackBlock() && value
.hasCollision()) &&
147 (!value
.isTransparent() || value
== mGameItemsHolder
.getBlock("glass"))) {
148 setBackMap(x
, y
, value
);
152 public void destroyForeMap(int x
, int y
) {
153 Block block
= getForeMap(x
, y
);
154 if (block
.hasDrop()) {
155 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
157 placeToForeground(x
, y
, mGameItemsHolder
.getFallbackBlock());
160 public void destroyBackMap(int x
, int y
) {
161 Block block
= getBackMap(x
, y
);
162 if (block
.hasDrop()) {
163 mDropController
.addDrop(transformX(x
) * 16 + 4, y
* 16 + 4, GameItems
.getItemId(block
.getDrop()));
165 placeToBackground(x
, y
, mGameItemsHolder
.getFallbackBlock());