DEADSOFTWARE

Delete old GameItems
[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;
11 import javax.annotation.CheckForNull;
12 import javax.inject.Inject;
14 @GameScope
15 public class GameWorld {
17 private final DropController mDropController;
18 private final MobsController mMobsController;
19 private final GameItemsHolder mGameItemsHolder;
21 private final int mWidth;
22 private final int mHeight;
23 private final Block[][] mForeMap;
24 private final Block[][] mBackMap;
26 @Inject
27 public GameWorld(DropController dropController,
28 MobsController mobsController,
29 GameItemsHolder gameItemsHolder,
30 @CheckForNull Block[][] foreMap,
31 @CheckForNull Block[][] backMap) {
32 mDropController = dropController;
33 mMobsController = mobsController;
34 mGameItemsHolder = gameItemsHolder;
36 boolean isNewGame = foreMap == null || backMap == null;
38 if (isNewGame) {
39 final WorldGeneratorConfig config = WorldGeneratorConfig.Companion.getDefault();
40 mWidth = config.getWidth();
41 mHeight = config.getHeight();
42 Pair<Block[][], Block[][]> maps = new GameWorldGenerator(config, mGameItemsHolder).generate();
43 mForeMap = maps.getFirst();
44 mBackMap = maps.getSecond();
45 mMobsController.getPlayer().respawn(this, mGameItemsHolder);
46 } else {
47 mForeMap = foreMap;
48 mBackMap = backMap;
49 mWidth = mForeMap.length;
50 mHeight = mForeMap[0].length;
51 }
52 }
54 public int getWidth() {
55 return mWidth;
56 }
58 public int getHeight() {
59 return mHeight;
60 }
62 public float getWidthPx() {
63 return mWidth * 16f;
64 }
66 public float getHeightPx() {
67 return mHeight * 16f;
68 }
70 public Block[][] getFullForeMap() {
71 return mForeMap;
72 }
74 public Block[][] getFullBackMap() {
75 return mBackMap;
76 }
78 private int transformX(int x) {
79 x = x % getWidth();
80 if (x < 0) {
81 x = getWidth() - Math.abs(x);
82 }
83 return x;
84 }
86 private Block getMap(int x, int y, int layer) {
87 Block map = mGameItemsHolder.getFallbackBlock();
88 try {
89 x = transformX(x);
90 map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
91 } catch (ArrayIndexOutOfBoundsException ignored) {
92 }
93 return map;
94 }
96 private void setMap(int x, int y, int layer, Block value) {
97 try {
98 x = transformX(x);
99 if (layer == 0) {
100 mForeMap[x][y] = value;
101 } else {
102 mBackMap[x][y] = value;
104 } catch (ArrayIndexOutOfBoundsException ignored) {
108 public boolean hasForeAt(int x, int y) {
109 return getMap(x, y, 0) != mGameItemsHolder.getFallbackBlock();
112 public boolean hasBackAt(int x, int y) {
113 return getMap(x, y, 1) != mGameItemsHolder.getFallbackBlock();
116 public Block getForeMap(int x, int y) {
117 return getMap(x, y, 0);
120 public void setForeMap(int x, int y, Block block) {
121 setMap(x, y, 0, block);
124 public void resetForeMap(int x, int y) {
125 setForeMap(x, y, mGameItemsHolder.getFallbackBlock());
128 public Block getBackMap(int x, int y) {
129 return getMap(x, y, 1);
132 public void setBackMap(int x, int y, Block block) {
133 setMap(x, y, 1, block);
136 public void placeToForeground(int x, int y, Block value) {
137 if (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) {
138 setForeMap(x, y, value);
139 } else if (value instanceof Block.Slab && getForeMap(x, y) == value) {
140 setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey()));
144 public void placeToBackground(int x, int y, Block value) {
145 if (value == mGameItemsHolder.getFallbackBlock() || (getBackMap(x, y) == mGameItemsHolder.getFallbackBlock() && value.hasCollision()) &&
146 (!value.isTransparent() || value == mGameItemsHolder.getBlock("glass"))) {
147 setBackMap(x, y, value);
151 public void destroyForeMap(int x, int y) {
152 Block block = getForeMap(x, y);
153 if (block.hasDrop()) {
154 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
156 placeToForeground(x, y, mGameItemsHolder.getFallbackBlock());
159 public void destroyBackMap(int x, int y) {
160 Block block = getBackMap(x, y);
161 if (block.hasDrop()) {
162 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
164 placeToBackground(x, y, mGameItemsHolder.getFallbackBlock());