DEADSOFTWARE

Add top slabs
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / world / GameWorld.java
1 package ru.deadsoftware.cavedroid.game.world;
3 import kotlin.Pair;
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;
15 @GameScope
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;
27 @Inject
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;
39 if (isNewGame) {
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);
47 } else {
48 mForeMap = foreMap;
49 mBackMap = backMap;
50 mWidth = mForeMap.length;
51 mHeight = mForeMap[0].length;
52 }
53 }
55 public int getWidth() {
56 return mWidth;
57 }
59 public int getHeight() {
60 return mHeight;
61 }
63 /**
64 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
65 */
66 @Deprecated
67 public float getWidthPx() {
68 return MeasureUnitsUtilsKt.getPx(mWidth);
69 }
71 /**
72 * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
73 */
74 @Deprecated
75 public float getHeightPx() {
76 return MeasureUnitsUtilsKt.getPx(mHeight);
77 }
79 public Block[][] getFullForeMap() {
80 return mForeMap;
81 }
83 public Block[][] getFullBackMap() {
84 return mBackMap;
85 }
87 private int transformX(int x) {
88 x = x % getWidth();
89 if (x < 0) {
90 x = getWidth() - Math.abs(x);
91 }
92 return x;
93 }
95 private Block getMap(int x, int y, int layer) {
96 Block map = mGameItemsHolder.getFallbackBlock();
97 try {
98 x = transformX(x);
99 map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
100 } catch (ArrayIndexOutOfBoundsException ignored) {
102 return map;
105 private void setMap(int x, int y, int layer, Block value) {
106 try {
107 x = transformX(x);
108 if (layer == 0) {
109 mForeMap[x][y] = value;
110 } else {
111 mBackMap[x][y] = value;
113 } catch (ArrayIndexOutOfBoundsException ignored) {
117 private boolean isSameSlab(Block slab1, Block slab2) {
118 if (!(slab1 instanceof Block.Slab) || !(slab2 instanceof Block.Slab)) {
119 return false;
122 return slab1.getParams().getKey().equals(((Block.Slab) slab2).getOtherPartBlockKey())
123 || slab1.getParams().getKey().equals(slab2.getParams().getKey());
126 public boolean hasForeAt(int x, int y) {
127 return getMap(x, y, 0) != mGameItemsHolder.getFallbackBlock();
130 public boolean hasBackAt(int x, int y) {
131 return getMap(x, y, 1) != mGameItemsHolder.getFallbackBlock();
134 public Block getForeMap(int x, int y) {
135 return getMap(x, y, 0);
138 public void setForeMap(int x, int y, Block block) {
139 setMap(x, y, 0, block);
142 public void resetForeMap(int x, int y) {
143 setForeMap(x, y, mGameItemsHolder.getFallbackBlock());
146 public Block getBackMap(int x, int y) {
147 return getMap(x, y, 1);
150 public void setBackMap(int x, int y, Block block) {
151 setMap(x, y, 1, block);
154 public void placeToForeground(int x, int y, Block value) {
155 if (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) {
156 setForeMap(x, y, value);
157 } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) {
158 setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey()));
162 public void placeToBackground(int x, int y, Block value) {
163 if (value == mGameItemsHolder.getFallbackBlock() || (getBackMap(x, y) == mGameItemsHolder.getFallbackBlock() && value.hasCollision()) &&
164 (!value.isTransparent() || value == mGameItemsHolder.getBlock("glass"))) {
165 setBackMap(x, y, value);
169 public void destroyForeMap(int x, int y) {
170 Block block = getForeMap(x, y);
171 if (block.hasDrop()) {
172 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
174 placeToForeground(x, y, mGameItemsHolder.getFallbackBlock());
177 public void destroyBackMap(int x, int y) {
178 Block block = getBackMap(x, y);
179 if (block.hasDrop()) {
180 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
182 placeToBackground(x, y, mGameItemsHolder.getFallbackBlock());