X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fworld%2FGameWorld.java;h=686f1c946c81f55fb90ef5d4578538a6d85e3c2f;hb=b9841a5aa1ccc1c4d30b23854b14d8de346951fd;hp=d4a66bcb8ad622ca43193d9df7f9cdb95d24d020;hpb=6a71a9abc5bc34547c0e4bea7ab19a6bee628a3c;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index d4a66bc..686f1c9 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -1,12 +1,15 @@ package ru.deadsoftware.cavedroid.game.world; 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.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.DropController; +import ru.deadsoftware.cavedroid.misc.utils.MeasureUnitsUtilsKt; import javax.annotation.CheckForNull; import javax.inject.Inject; @@ -16,30 +19,34 @@ public class GameWorld { private final DropController mDropController; private final MobsController mMobsController; + private final GameItemsHolder mGameItemsHolder; 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 final WorldGeneratorConfig mWorldConfig = WorldGeneratorConfig.Companion.getDefault(); @Inject public GameWorld(DropController dropController, MobsController mobsController, - @CheckForNull int[][] foreMap, - @CheckForNull int[][] backMap) { + GameItemsHolder gameItemsHolder, + @CheckForNull Block[][] foreMap, + @CheckForNull Block[][] backMap) { mDropController = dropController; mMobsController = mobsController; + mGameItemsHolder = gameItemsHolder; boolean isNewGame = foreMap == null || backMap == null; if (isNewGame) { - final WorldGeneratorConfig config = WorldGeneratorConfig.Companion.getDefault(); - mWidth = config.getWidth(); - mHeight = config.getHeight(); - Pair maps = new GameWorldGenerator(config).generate(); + mWidth = mWorldConfig.getWidth(); + mHeight = mWorldConfig.getHeight(); + Pair 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; @@ -56,19 +63,27 @@ public class GameWorld { 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; } @@ -80,91 +95,138 @@ public class GameWorld { 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; + } + + if (layer == 0) { + mForeMap[x][y] = value; + } else { + mBackMap[x][y] = value; } } + 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 hasForeAt(int x, int y) { - return getMap(x, y, 0) != 0; + return getMap(x, y, 0) != mGameItemsHolder.getFallbackBlock(); } public boolean hasBackAt(int x, int y) { - return getMap(x, y, 1) != 0; + return getMap(x, y, 1) != mGameItemsHolder.getFallbackBlock(); } - public int getForeMap(int x, int y) { + public Block getForeMap(int x, int y) { return getMap(x, y, 0); } - public Block getForeMapBlock(int x, int y) { - return GameItems.getBlock(getMap(x, y, 0)); + public void setForeMap(int x, int y, Block block) { + setMap(x, y, 0, block); } - public void setForeMap(int x, int y, int id) { - setMap(x, y, 0, id); + public void resetForeMap(int x, int y) { + setForeMap(x, y, mGameItemsHolder.getFallbackBlock()); } - public int getBackMap(int x, int y) { + public Block getBackMap(int x, int y) { return getMap(x, y, 1); } - public Block getBackMapBlock(int x, int y) { - return GameItems.getBlock(getMap(x, y, 1)); - } - - 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, 1, block); } - 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 (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) { setForeMap(x, y, value); - } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) { - final Block block = GameItems.getBlock(value); - if (block instanceof Block.Slab) { - setForeMap(x, y, GameItems.getBlockId(((Block.Slab) block).getFullBlockKey())); - } + return true; + } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) { + setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey())); + return 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"))) { setBackMap(x, y, value); + return true; + } + return false; + } + + private void playerDurateTool() { + final InventoryItem playerCurrentItem = mMobsController.getPlayer().inventory.getActiveItem(); + if (playerCurrentItem.getItem().isTool()) { + mMobsController.getPlayer().decreaseCurrentItemCount(mGameItemsHolder); } } + 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; + } + return toolLevel >= block.getParams().getToolLevel(); + } + 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())); + Block block = getForeMap(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())); + } } - placeToForeground(x, y, 0); + playerDurateTool(); + placeToForeground(x, y, mGameItemsHolder.getFallbackBlock()); + } + + public WorldGeneratorConfig getWorldConfig() { + return mWorldConfig; } 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())); + 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())); + } } - placeToBackground(x, y, 0); + playerDurateTool(); + placeToBackground(x, y, mGameItemsHolder.getFallbackBlock()); } } \ No newline at end of file