From: fredboy Date: Sun, 19 May 2024 12:57:04 +0000 (+0700) Subject: Abstract falling block X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=6bb7e1d207c640ff3b8e41ce2fb1c6203985bdb1;p=cavedroid.git Abstract falling block --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 5c11576..245ebb0 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -108,14 +108,16 @@ "drop": "sand", "texture": "sand", "tool_level": 0, - "tool_type": "shovel" + "tool_type": "shovel", + "fallable": true }, "gravel": { "hp": 54, "drop": "gravel", "texture": "gravel", "tool_level": 0, - "tool_type": "shovel" + "tool_type": "shovel", + "fallable": true }, "gold_ore": { "hp": 900, diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt index 5507550..c23b80f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt @@ -2,8 +2,6 @@ package ru.deadsoftware.cavedroid.game.actions.updateblock 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.MobsController import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindUpdateBlockAction import javax.inject.Inject diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt index 42f551e..cd8b2d3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt @@ -2,8 +2,6 @@ package ru.deadsoftware.cavedroid.game.actions.updateblock 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.MobsController import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindUpdateBlockAction import javax.inject.Inject diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt deleted file mode 100644 index 3c52418..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateGravelAction.kt +++ /dev/null @@ -1,30 +0,0 @@ -package ru.deadsoftware.cavedroid.game.actions.updateblock - -import ru.deadsoftware.cavedroid.game.GameScope -import ru.deadsoftware.cavedroid.game.mobs.FallingGravel -import ru.deadsoftware.cavedroid.game.mobs.MobsController -import ru.deadsoftware.cavedroid.game.world.GameWorld -import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindUpdateBlockAction -import javax.inject.Inject - -@GameScope -@BindUpdateBlockAction(stringKey = UpdateGravelAction.BLOCK_KEY) -class UpdateGravelAction @Inject constructor( - private val gameWorld: GameWorld, - private val mobsController: MobsController, -) : IUpdateBlockAction { - - override fun update(x: Int, y: Int) { - val shouldFall = gameWorld.getForeMap(x, y + 1).params.hasCollision.not() - - if (shouldFall) { - gameWorld.resetForeMap(x, y) - FallingGravel(x * 16f, y * 16f) - .apply { attachToController(mobsController) } - } - } - - companion object { - const val BLOCK_KEY = "gravel" - } -} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt index 05d9863..53f56e3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateRequiresBlockAction.kt @@ -1,19 +1,31 @@ package ru.deadsoftware.cavedroid.game.actions.updateblock import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.FallingBlock +import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.world.GameWorld import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindUpdateBlockAction +import ru.deadsoftware.cavedroid.misc.utils.px import javax.inject.Inject @GameScope @BindUpdateBlockAction(stringKey = UpdateRequiresBlockAction.ACTION_KEY) class UpdateRequiresBlockAction @Inject constructor( private val gameWorld: GameWorld, + private val mobsController: MobsController, ) : IUpdateBlockAction { override fun update(x: Int, y: Int) { if (gameWorld.getForeMap(x, y + 1).params.hasCollision.not()) { - gameWorld.destroyForeMap(x, y) + val block = gameWorld.getForeMap(x, y) + + if (block.params.isFallable) { + gameWorld.resetForeMap(x, y) + FallingBlock(block.params.key, x.px, y.px) + .attachToController(mobsController) + } else { + gameWorld.destroyForeMap(x, y) + } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt deleted file mode 100644 index ac1ff8a..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateSandAction.kt +++ /dev/null @@ -1,30 +0,0 @@ -package ru.deadsoftware.cavedroid.game.actions.updateblock - -import ru.deadsoftware.cavedroid.game.GameScope -import ru.deadsoftware.cavedroid.game.mobs.FallingSand -import ru.deadsoftware.cavedroid.game.mobs.MobsController -import ru.deadsoftware.cavedroid.game.world.GameWorld -import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindUpdateBlockAction -import javax.inject.Inject - -@GameScope -@BindUpdateBlockAction(stringKey = UpdateSandAction.BLOCK_KEY) -class UpdateSandAction @Inject constructor( - private val gameWorld: GameWorld, - private val mobsController: MobsController, -) : IUpdateBlockAction { - - override fun update(x: Int, y: Int) { - val shouldFall = gameWorld.getForeMap(x, y + 1).params.hasCollision.not() - - if (shouldFall) { - gameWorld.resetForeMap(x, y) - FallingSand(x * 16f, y * 16f) - .apply { attachToController(mobsController) } - } - } - - companion object { - const val BLOCK_KEY = "sand" - } -} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingBlock.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingBlock.kt new file mode 100644 index 0000000..f273504 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingBlock.kt @@ -0,0 +1,53 @@ +package ru.deadsoftware.cavedroid.game.mobs + +import com.badlogic.gdx.graphics.g2d.SpriteBatch +import ru.deadsoftware.cavedroid.game.GameItemsHolder +import ru.deadsoftware.cavedroid.game.model.block.Block +import ru.deadsoftware.cavedroid.game.world.GameWorld +import ru.deadsoftware.cavedroid.misc.utils.bl +import ru.deadsoftware.cavedroid.misc.utils.px + +class FallingBlock( + private val blockKey: String, + x: Float, + y: Float, +) : Mob(x, y, 1.px, 1.px, Direction.RIGHT, Type.FALLING_BLOCK, Int.MAX_VALUE) { + + @Transient + private var _block: Block? = null + + init { + velocity.y = 1f + } + + override fun changeDir() = Unit + + override fun getSpeed() = 0f + + override fun jump() = Unit + + override fun ai( + gameWorld: GameWorld, + gameItemsHolder: GameItemsHolder, + mobsController: MobsController, + delta: Float + ) { + if (_block == null) { + _block = gameItemsHolder.getBlock(blockKey) + } + + if (velocity.isZero) { + gameWorld.setForeMap(x.bl, y.bl, _block) + kill() + } + } + + override fun draw( + spriteBatch: SpriteBatch, + x: Float, + y: Float, + delta: Float + ) { + _block?.draw(spriteBatch, x, y) + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java deleted file mode 100644 index 2ad4287..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingGravel.java +++ /dev/null @@ -1,72 +0,0 @@ -package ru.deadsoftware.cavedroid.game.mobs; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameItemsHolder; -import ru.deadsoftware.cavedroid.game.world.GameWorld; -import ru.deadsoftware.cavedroid.misc.Assets; - -import javax.annotation.CheckForNull; - -/** - * Falling gravel is actually a mob, that spawns in place of gravel when there is no block under it, - * falls down to the next block and becomes a block of gravel again. - */ -public class FallingGravel extends Mob { - - private static final String TAG = "FallingGravel"; - - /** - * Creates a FallingGravel mob at coordinates - * - * @param x X in pixels - * @param y Y in pixels - */ - public FallingGravel(float x, float y) { - super(x, y, 16, 16, Direction.LEFT, Type.GRAVEL, Integer.MAX_VALUE); - mVelocity = new Vector2(0, 1); - } - - @Override - public float getSpeed() { - return 0; - } - - @Override - public void jump() { - // no-op - } - - @Override - public void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, MobsController mobsController, float delta) { - if (mVelocity.isZero()) { - gameWorld.setForeMap(getMapX(), getUpperMapY(), gameItemsHolder.getBlock("gravel")); - kill(); - } - } - - @Override - public void changeDir() { - } - - @Override - public void damage(int damage) { - // no-op - } - - @Override - public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { - @CheckForNull final Texture texture = Assets.blockTextures.get("gravel"); - - if (texture == null) { - Gdx.app.error(TAG, "Couldn't draw: texture not found"); - kill(); - return; - } - - spriteBatch.draw(texture, x, y); - } - -} diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java deleted file mode 100644 index 970ff53..0000000 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/FallingSand.java +++ /dev/null @@ -1,73 +0,0 @@ -package ru.deadsoftware.cavedroid.game.mobs; - -import com.badlogic.gdx.Gdx; -import com.badlogic.gdx.graphics.Texture; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; -import com.badlogic.gdx.math.Vector2; -import ru.deadsoftware.cavedroid.game.GameItemsHolder; -import ru.deadsoftware.cavedroid.game.world.GameWorld; -import ru.deadsoftware.cavedroid.misc.Assets; - -import javax.annotation.CheckForNull; - - -/** - * Falling sand is actually a mob, that spawns in place of gravel when there is no block under it, - * falls down to the next block and becomes a block of sand again. - */ -public class FallingSand extends Mob { - - private static final String TAG = "FallingSand"; - - /** - * Creates a FallingSand mob at coordinates - * - * @param x X in pixels - * @param y Y in pixels - */ - public FallingSand(float x, float y) { - super(x, y, 16, 16, Direction.LEFT, Type.SAND, Integer.MAX_VALUE); - mVelocity = new Vector2(0, 1); - } - - @Override - public float getSpeed() { - return 0; - } - - @Override - public void jump() { - // no-op - } - - @Override - public void ai(GameWorld gameWorld, GameItemsHolder gameItemsHolder, MobsController mobsController, float delta) { - if (mVelocity.isZero()) { - gameWorld.setForeMap(getMapX(), getUpperMapY(), gameItemsHolder.getBlock("sand")); - kill(); - } - } - - @Override - public void changeDir() { - } - - @Override - public void damage(int damage) { - // no-op - } - - @Override - public void draw(SpriteBatch spriteBatch, float x, float y, float delta) { - @CheckForNull final Texture texture = Assets.blockTextures.get("sand"); - - if (texture == null) { - Gdx.app.error(TAG, "Couldn't draw: texture not found"); - kill(); - return; - } - - spriteBatch.draw(texture, x, y); - } - -} diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java index 62ddc50..04fa1bb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Mob.java @@ -27,8 +27,7 @@ public abstract class Mob extends Rectangle implements Serializable { public enum Type { MOB, - SAND, - GRAVEL + FALLING_BLOCK } public enum Direction { diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt b/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt index 369f752..1945462 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/block/CommonBlockParams.kt @@ -19,4 +19,5 @@ data class CommonBlockParams( val toolType: Class?, val damage: Int, val tint: String?, + val isFallable: Boolean, ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt b/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt index 69b1f18..ecbe5bf 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/dto/BlockDto.kt @@ -31,4 +31,5 @@ data class BlockDto( @SerialName("tool_type") val toolType: String? = null, @SerialName("damage") val damage: Int = 0, @SerialName("tint") val tint: String? = null, + @SerialName("fallable") val fallable: Boolean = false, ) diff --git a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt index 1b641ed..19d8c47 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/model/mapper/BlockMapper.kt @@ -57,6 +57,7 @@ class BlockMapper @Inject constructor( toolType = mapToolType(dto), damage = dto.damage, tint = dto.tint, + isFallable = dto.fallable, ) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt index 387407a..e92b9e2 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorldBlocksLogicControllerTask.kt @@ -33,7 +33,7 @@ class GameWorldBlocksLogicControllerTask @Inject constructor( val blockKey = block.params.key val action = updateBlockActions[blockKey] - ?: updateBlockActions.getRequiresBlockAction().takeIf { block.params.requiresBlock } + ?: updateBlockActions.getRequiresBlockAction().takeIf { block.params.run { requiresBlock || isFallable } } action?.update(x, y) }