diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java
index 31d842b331e85768e3ff9369c26e93c0160c361b..d52b50f672039328773896ee12f11072949db368 100644 (file)
package ru.deadsoftware.cavedroid.game.world;
-import com.badlogic.gdx.utils.Disposable;
import kotlin.Pair;
-import ru.deadsoftware.cavedroid.game.GameItems;
+import ru.deadsoftware.cavedroid.game.GameItemsHolder;
import ru.deadsoftware.cavedroid.game.GameScope;
-import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
-import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
import ru.deadsoftware.cavedroid.game.mobs.MobsController;
+import ru.deadsoftware.cavedroid.game.model.block.Block;
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem;
+import ru.deadsoftware.cavedroid.game.model.item.Item;
import ru.deadsoftware.cavedroid.game.model.world.generator.WorldGeneratorConfig;
-import ru.deadsoftware.cavedroid.game.objects.Block;
-import ru.deadsoftware.cavedroid.game.objects.DropController;
+import ru.deadsoftware.cavedroid.game.objects.container.Container;
+import ru.deadsoftware.cavedroid.game.objects.drop.DropController;
+import ru.deadsoftware.cavedroid.game.objects.container.Furnace;
+import ru.deadsoftware.cavedroid.game.objects.container.ContainerController;
+import ru.deadsoftware.cavedroid.misc.utils.MeasureUnitsUtilsKt;
import javax.annotation.CheckForNull;
import javax.inject.Inject;
-import java.util.Timer;
@GameScope
-public class GameWorld implements Disposable {
+public class GameWorld {
- private static final int UPDATE_RANGE = 16;
+ private static final int FOREGROUND_Z = 0;
+ private static final int BACKGROUND_Z = 1;
private final DropController mDropController;
private final MobsController mMobsController;
-
- private final Timer mGameFluidsTimer;
- private final GameFluidsThread mGameFluidsThread;
+ private final GameItemsHolder mGameItemsHolder;
+ private final ContainerController mContainerController;
private final int mWidth;
private final int mHeight;
- private final int[][] mForeMap;
- private final int[][] mBackMap;
+ private final Block[][] mForeMap;
+ private final Block[][] mBackMap;
- private boolean mShouldUpdate;
- private int mUpdateX;
- private int mUpdateY;
+ private final WorldGeneratorConfig mWorldConfig = WorldGeneratorConfig.Companion.getDefault();
@Inject
public GameWorld(DropController dropController,
MobsController mobsController,
- @CheckForNull int[][] foreMap,
- @CheckForNull int[][] backMap) {
+ GameItemsHolder gameItemsHolder,
+ ContainerController containerController,
+ @CheckForNull Block[][] foreMap,
+ @CheckForNull Block[][] backMap) {
mDropController = dropController;
mMobsController = mobsController;
+ mGameItemsHolder = gameItemsHolder;
+ mContainerController = containerController;
boolean isNewGame = foreMap == null || backMap == null;
if (isNewGame) {
- final WorldGeneratorConfig config = WorldGeneratorConfig.Companion.getDefault();
- mWidth = config.getWidth();
- mHeight = config.getHeight();
- Pair<int[][], int[][]> maps = new GameWorldGenerator(config).generate();
+ mWidth = mWorldConfig.getWidth();
+ mHeight = mWorldConfig.getHeight();
+ Pair<Block[][], Block[][]> maps = new GameWorldGenerator(mWorldConfig, mGameItemsHolder).generate();
mForeMap = maps.getFirst();
mBackMap = maps.getSecond();
- mMobsController.getPlayer().respawn(this);
+ mMobsController.getPlayer().respawn(this, mGameItemsHolder);
} else {
mForeMap = foreMap;
mBackMap = backMap;
mWidth = mForeMap.length;
mHeight = mForeMap[0].length;
}
-
- mGameFluidsThread = new GameFluidsThread(this, mMobsController);
-
- mGameFluidsTimer = new Timer();
- mGameFluidsTimer.scheduleAtFixedRate(mGameFluidsThread, 0, GameFluidsThread.FLUID_UPDATE_INTERVAL_MS);
}
public int getWidth() {
return mHeight;
}
+ /**
+ * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
+ */
+ @Deprecated
public float getWidthPx() {
- return mWidth * 16f;
+ return MeasureUnitsUtilsKt.getPx(mWidth);
}
+ /**
+ * @deprecated for kotlin use {@link MeasureUnitsUtilsKt#getPx } extension val
+ */
+ @Deprecated
public float getHeightPx() {
- return mHeight * 16f;
+ return MeasureUnitsUtilsKt.getPx(mHeight);
}
- public int[][] getFullForeMap() {
+ public Block[][] getFullForeMap() {
return mForeMap;
}
- public int[][] getFullBackMap() {
+ public Block[][] getFullBackMap() {
return mBackMap;
}
return x;
}
- private int getMap(int x, int y, int layer) {
- int map = 0;
- try {
- x = transformX(x);
- map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
- } catch (ArrayIndexOutOfBoundsException ignored) {
+ private Block getMap(int x, int y, int layer) {
+ Block map = mGameItemsHolder.getFallbackBlock();
+
+ if (y < 0 || y >= getHeight()) {
+ return map;
+ }
+
+ x = transformX(x);
+
+ if (x < 0 || x >= getWidth()) {
+ return map;
}
+
+ map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
+
return map;
}
- private void setMap(int x, int y, int layer, int value) {
- try {
- x = transformX(x);
- if (layer == 0) {
- mForeMap[x][y] = value;
- } else {
- mBackMap[x][y] = value;
- }
- } catch (ArrayIndexOutOfBoundsException ignored) {
+ private void setMap(int x, int y, int layer, Block value) {
+ if (y < 0 || y >= getHeight()) {
+ return;
+ }
+
+ x = transformX(x);
+
+ if (x < 0 || x >= getWidth()) {
+ return;
+ }
+
+ mContainerController.destroyContainer(x, y, layer, false);
+
+ if (value.isContainer()) {
+ mContainerController.addContainer(x, y, layer, (Class<? extends Block.Container>) value.getClass());
+ }
+
+ if (layer == 0) {
+ mForeMap[x][y] = value;
+ } else {
+ mBackMap[x][y] = value;
}
}
- public boolean hasForeAt(int x, int y) {
- return getMap(x, y, 0) != 0;
+ private boolean isSameSlab(Block slab1, Block slab2) {
+ if (!(slab1 instanceof Block.Slab) || !(slab2 instanceof Block.Slab)) {
+ return false;
+ }
+
+ return slab1.getParams().getKey().equals(((Block.Slab) slab2).getOtherPartBlockKey())
+ || slab1.getParams().getKey().equals(slab2.getParams().getKey());
}
- public boolean hasBackAt(int x, int y) {
- return getMap(x, y, 1) != 0;
+ public boolean hasForeAt(int x, int y) {
+ return getMap(x, y, FOREGROUND_Z) != mGameItemsHolder.getFallbackBlock();
}
- public int getForeMap(int x, int y) {
- return getMap(x, y, 0);
+ public boolean hasBackAt(int x, int y) {
+ return getMap(x, y, BACKGROUND_Z) != mGameItemsHolder.getFallbackBlock();
}
- public Block getForeMapBlock(int x, int y) {
- return GameItems.getBlock(getMap(x, y, 0));
+ public Block getForeMap(int x, int y) {
+ return getMap(x, y, FOREGROUND_Z);
}
- public void setForeMap(int x, int y, int id) {
- setMap(x, y, 0, id);
+ public void setForeMap(int x, int y, Block block) {
+ setMap(x, y, FOREGROUND_Z, block);
}
- public int getBackMap(int x, int y) {
- return getMap(x, y, 1);
+ public void resetForeMap(int x, int y) {
+ setForeMap(x, y, mGameItemsHolder.getFallbackBlock());
}
- public Block getBackMapBlock(int x, int y) {
- return GameItems.getBlock(getMap(x, y, 1));
+ public Block getBackMap(int x, int y) {
+ return getMap(x, y, BACKGROUND_Z);
}
- public void setBackMap(int x, int y, int id) {
- setMap(x, y, 1, id);
+ public void setBackMap(int x, int y, Block block) {
+ setMap(x, y, BACKGROUND_Z, block);
}
- private void placeSlab(int x, int y, int value) {
- switch (value) {
- case 51:
- setForeMap(x, y, 52);
- break;
- case 53:
- setForeMap(x, y, 21);
- break;
- case 54:
- setForeMap(x, y, 5);
- break;
- case 55:
- setForeMap(x, y, 4);
- break;
- case 56:
- setForeMap(x, y, 28);
- break;
- case 58:
- setForeMap(x, y, 57);
- break;
- }
+ public boolean canPlaceToForeground(int x, int y, Block value) {
+ return !hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision();
}
- public void placeToForeground(int x, int y, int value) {
- if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
+ public boolean placeToForeground(int x, int y, Block value) {
+ if (canPlaceToForeground(x, y, value)) {
setForeMap(x, y, value);
- } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
- placeSlab(x, y, value);
+ return true;
+ } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) {
+ setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey()));
+ return true;
}
- mUpdateX = x - 8;
- mUpdateY = y - 8;
- mShouldUpdate = true;
+ return false;
}
- public void placeToBackground(int x, int y, int value) {
- if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
- (!GameItems.getBlock(value).isTransparent() || value == 18)) {
+ public boolean placeToBackground(int x, int y, Block value) {
+ if (value == mGameItemsHolder.getFallbackBlock() || (getBackMap(x, y) == mGameItemsHolder.getFallbackBlock() && value.hasCollision()) &&
+ (!value.isTransparent() || value == mGameItemsHolder.getBlock("glass") || value.isChest() || value.isSlab())) {
setBackMap(x, y, value);
+ return true;
}
+ return false;
}
- public void destroyForeMap(int x, int y) {
- Block block = GameItems.getBlock(getForeMap(x, y));
- if (block.hasDrop()) {
- mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
+ private void playerDurateTool() {
+ final InventoryItem playerCurrentItem = mMobsController.getPlayer().inventory.getActiveItem();
+ if (playerCurrentItem.getItem().isTool()) {
+ mMobsController.getPlayer().decreaseCurrentItemCount(mGameItemsHolder);
}
- placeToForeground(x, y, 0);
}
- public void destroyBackMap(int x, int y) {
- Block block = GameItems.getBlock(getBackMap(x, y));
- if (block.hasDrop()) {
- mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
+ private boolean shouldDrop(Block block) {
+ final Item item = mMobsController.getPlayer().inventory.getActiveItem().getItem();
+ int toolLevel = item.isTool() ? ((Item.Tool) item).getLevel() : 0;
+ if (item.isTool() && block.getParams().getToolType() != item.getClass()) {
+ toolLevel = 0;
}
- placeToBackground(x, y, 0);
+ return toolLevel >= block.getParams().getToolLevel();
}
- private void updateBlock(int x, int y) {
- if (getForeMap(x, y) == 10) {
- if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
- setForeMap(x, y, 0);
- mMobsController.addMob(FallingSand.class, x * 16, y * 16);
- updateBlock(x, y - 1);
- }
+ public void destroyForeMap(int x, int y) {
+ Block block = getForeMap(x, y);
+ if (block.isContainer()) {
+ mContainerController.destroyContainer(x, y, FOREGROUND_Z);
}
-
- if (getForeMap(x, y) == 11) {
- if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
- setForeMap(x, y, 0);
- mMobsController.addMob(FallingGravel.class, x * 16, y * 16);
- updateBlock(x, y - 1);
+ if (block.hasDrop() && shouldDrop(block)) {
+ for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) {
+ mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
}
}
+ playerDurateTool();
+ placeToForeground(x, y, mGameItemsHolder.getFallbackBlock());
+ }
- if (hasForeAt(x, y) && getForeMapBlock(x, y).requiresBlock()) {
- if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
- destroyForeMap(x, y);
- updateBlock(x, y - 1);
- }
- }
+ public WorldGeneratorConfig getWorldConfig() {
+ return mWorldConfig;
+ }
- if (getForeMap(x, y) == 2) {
- if (hasForeAt(x, y - 1) && (getForeMapBlock(x, y - 1).hasCollision() ||
- GameItems.isFluid(getForeMap(x, y - 1)))) {
- setForeMap(x, y, 3);
+ public void destroyBackMap(int x, int y) {
+ Block block = getBackMap(x, y);
+ if (block.hasDrop() && shouldDrop(block)) {
+ for (int i = 0; i < block.getParams().getDropInfo().getCount(); i++) {
+ mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop()));
}
}
+ playerDurateTool();
+ placeToBackground(x, y, mGameItemsHolder.getFallbackBlock());
}
- public void update() {
- if (mShouldUpdate) {
- for (int y = mUpdateY; y < mUpdateY + UPDATE_RANGE; y++) {
- for (int x = mUpdateX; x < mUpdateX + UPDATE_RANGE; x++) {
- updateBlock(x, y);
- }
- }
- mShouldUpdate = false;
+ @CheckForNull
+ private Container getContainerAt(int x, int y, int z) {
+ return mContainerController.getContainer(transformX(x), y, z);
+ }
+
+ @CheckForNull
+ public Container getForegroundContainer(int x, int y) {
+ return getContainerAt(x, y, FOREGROUND_Z);
+ }
+
+ @CheckForNull
+ public Container getBackgroundContainer(int x, int y) {
+ return getContainerAt(x, y, BACKGROUND_Z);
+ }
+
+ @CheckForNull
+ public Furnace getForegroundFurnace(int x, int y) {
+ @CheckForNull
+ final Container container = getForegroundContainer(x, y);
+
+ if (container instanceof Furnace) {
+ return (Furnace) container;
}
+
+ return null;
}
- @Override
- public void dispose() {
- mGameFluidsTimer.cancel();
+ @CheckForNull
+ public Furnace getBackgroundFurnace(int x, int y) {
+ @CheckForNull
+ final Container container = getBackgroundContainer(x, y);
+
+ if (container instanceof Furnace) {
+ return (Furnace) container;
+ }
+
+ return null;
}
}
\ No newline at end of file