X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Fworld%2FGameWorld.java;h=91793c845a23406745876c1fc0cad8ffb013a615;hb=fd73b6dd24b9fe55c0bc8f52f8aa1104a5fe3cb0;hp=18afcd4b3eec1c725a4675c5b8b429c317256da4;hpb=2aa65a4cdb47df8014f28342d460fc6639bed885;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 18afcd4..91793c8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -1,64 +1,55 @@ package ru.deadsoftware.cavedroid.game.world; -import com.badlogic.gdx.utils.Disposable; -import com.badlogic.gdx.utils.TimeUtils; 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.objects.Block; +import ru.deadsoftware.cavedroid.game.model.block.Block; +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; @GameScope -public class GameWorld implements Disposable { - - private static final int DEFAULT_WIDTH = 1024; - private static final int DEFAULT_HEIGHT = 256; - private static final int UPDATE_RANGE = 16; +public class GameWorld { private final DropController mDropController; private final MobsController mMobsController; - private final GameFluidsThread mGameFluidsThread; + private final GameItemsHolder mGameItemsHolder; private final int mWidth; private final int mHeight; - private final int[][] mForeMap; - private final int[][] mBackMap; - - private boolean mShouldUpdate; - private int mUpdateX; - private int mUpdateY; + private final Block[][] mForeMap; + private final Block[][] mBackMap; @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) { - mWidth = DEFAULT_WIDTH; - mHeight = DEFAULT_HEIGHT; - Pair maps = GameWorldGenerator.INSTANCE.generate(mWidth, mHeight, TimeUtils.millis()); + final WorldGeneratorConfig config = WorldGeneratorConfig.Companion.getDefault(); + mWidth = config.getWidth(); + mHeight = config.getHeight(); + Pair maps = new GameWorldGenerator(config, 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, Thread.currentThread()); } public int getWidth() { @@ -69,19 +60,27 @@ public class GameWorld implements Disposable { 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; } @@ -93,8 +92,8 @@ public class GameWorld implements Disposable { return x; } - private int getMap(int x, int y, int layer) { - int map = 0; + private Block getMap(int x, int y, int layer) { + Block map = mGameItemsHolder.getFallbackBlock(); try { x = transformX(x); map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y]; @@ -103,7 +102,7 @@ public class GameWorld implements Disposable { return map; } - private void setMap(int x, int y, int layer, int value) { + private void setMap(int x, int y, int layer, Block value) { try { x = transformX(x); if (layer == 0) { @@ -116,147 +115,61 @@ public class GameWorld implements Disposable { } 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); - } - - 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 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 void 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) { - placeSlab(x, y, value); + } else if (value instanceof Block.Slab && getForeMap(x, y) == value) { + setForeMap(x, y, mGameItemsHolder.getBlock(((Block.Slab) value).getFullBlockKey())); } - mUpdateX = x - 8; - mUpdateY = y - 8; - mShouldUpdate = true; } - 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 void 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); } } public void destroyForeMap(int x, int y) { - Block block = GameItems.getBlock(getForeMap(x, y)); + Block block = getForeMap(x, y); if (block.hasDrop()) { - mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop())); + mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); } - placeToForeground(x, y, 0); + placeToForeground(x, y, mGameItemsHolder.getFallbackBlock()); } public void destroyBackMap(int x, int y) { - Block block = GameItems.getBlock(getBackMap(x, y)); + Block block = getBackMap(x, y); if (block.hasDrop()) { - mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop())); - } - placeToBackground(x, y, 0); - } - - 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); - } - } - - 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 (hasForeAt(x, y) && getForeMapBlock(x, y).requiresBlock()) { - if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) { - destroyForeMap(x, y); - updateBlock(x, y - 1); - } - } - - 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); - } + mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, mGameItemsHolder.getItem(block.getDrop())); } - } - - 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; - } - - if (!mGameFluidsThread.isAlive()) { - mGameFluidsThread.start(); - } - } - - public void startFluidsThread() { - mGameFluidsThread.start(); - } - - @Override - public void dispose() { - mGameFluidsThread.interrupt(); + placeToBackground(x, y, mGameItemsHolder.getFallbackBlock()); } } \ No newline at end of file