DEADSOFTWARE

Store block references intead of ids
[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.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;
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 public float getWidthPx() {
64 return mWidth * 16f;
65 }
67 public float getHeightPx() {
68 return mHeight * 16f;
69 }
71 public Block[][] getFullForeMap() {
72 return mForeMap;
73 }
75 public Block[][] getFullBackMap() {
76 return mBackMap;
77 }
79 private int transformX(int x) {
80 x = x % getWidth();
81 if (x < 0) {
82 x = getWidth() - Math.abs(x);
83 }
84 return x;
85 }
87 private Block getMap(int x, int y, int layer) {
88 Block map = mGameItemsHolder.getFallbackBlock();
89 try {
90 x = transformX(x);
91 map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
92 } catch (ArrayIndexOutOfBoundsException ignored) {
93 }
94 return map;
95 }
97 private void setMap(int x, int y, int layer, Block value) {
98 try {
99 x = transformX(x);
100 if (layer == 0) {
101 mForeMap[x][y] = value;
102 } else {
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());