From 9f3d2a56e9e81f3d0553220c684d6c6000969501 Mon Sep 17 00:00:00 2001 From: fredboy Date: Sat, 11 May 2024 00:41:38 +0700 Subject: [PATCH] Add bed --- android/assets/json/crafting.json | 3 ++ android/assets/json/game_items.json | 4 +-- .../game/actions/UpdateBlockActionsModule.kt | 16 ++++++++++ .../game/actions/UseItemActionsModule.kt | 8 +++++ .../updateblock/UpdateBedLeftAction.kt | 26 ++++++++++++++++ .../updateblock/UpdateBedRightAction.kt | 26 ++++++++++++++++ .../game/actions/useitem/UseBedAction.kt | 31 +++++++++++++++++++ .../cavedroid/game/world/GameWorld.java | 6 +++- 8 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt create mode 100644 core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseBedAction.kt diff --git a/android/assets/json/crafting.json b/android/assets/json/crafting.json index 6404dd6..9a67e01 100644 --- a/android/assets/json/crafting.json +++ b/android/assets/json/crafting.json @@ -103,5 +103,8 @@ }, "chest": { "input": ["planks_.*", "planks_.*", "planks_.*", "planks_.*", "none", "planks_.*", "planks_.*", "planks_.*", "planks_.*"] + }, + "bed": { + "input": ["wool_.*", "wool_.*", "wool_.*", "planks_.*", "planks_.*", "planks_.*", "none", "none", "none"] } } \ No newline at end of file diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 95d4c7a..5c11576 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -228,7 +228,7 @@ "collision": false, "background": true, "transparent": true, - "drop": "none", + "drop": "bed", "texture": "bed_l", "tool_level": 0, "tool_type": "axe" @@ -238,7 +238,7 @@ "collision": false, "background": true, "transparent": true, - "drop": "none", + "drop": "bed", "texture": "bed_r", "tool_level": 0, "tool_type": "axe" diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt index 5dcfe0e..6f0fc0f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/UpdateBlockActionsModule.kt @@ -49,4 +49,20 @@ class UpdateBlockActionsModule { fun bindUpdateSnowedGrassAction(action: UpdateSnowedGrassAction): IUpdateBlockAction { return action; } + + @Binds + @IntoMap + @StringKey(UpdateBedLeftAction.BLOCK_KEY) + @GameScope + fun bindUpdateBedLeftAction(action: UpdateBedLeftAction): IUpdateBlockAction { + return action; + } + + @Binds + @IntoMap + @StringKey(UpdateBedRightAction.BLOCK_KEY) + @GameScope + fun bindUpdateBedRightAction(action: UpdateBedRightAction): IUpdateBlockAction { + return action; + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt index a6aed22..d5e4f4f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/UseItemActionsModule.kt @@ -42,4 +42,12 @@ class UseItemActionsModule { return action } + @Binds + @IntoMap + @StringKey(UseBedAction.ACTION_KEY) + @GameScope + fun bindUseBedAction(action: UseBedAction): IUseItemAction { + return action + } + } diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt new file mode 100644 index 0000000..15621a2 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedLeftAction.kt @@ -0,0 +1,26 @@ +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 javax.inject.Inject + +@GameScope +class UpdateBedLeftAction @Inject constructor( + private val gameWorld: GameWorld, + private val gameItemsHolder: GameItemsHolder, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + val bedRight = gameItemsHolder.getBlock("bed_r") + if (gameWorld.getForeMap(x + 1, y) != bedRight) { + gameWorld.resetForeMap(x, y) + } + } + + companion object { + const val BLOCK_KEY = "bed_l" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt new file mode 100644 index 0000000..4d47b35 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/updateblock/UpdateBedRightAction.kt @@ -0,0 +1,26 @@ +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 javax.inject.Inject + +@GameScope +class UpdateBedRightAction @Inject constructor( + private val gameWorld: GameWorld, + private val gameItemsHolder: GameItemsHolder, +) : IUpdateBlockAction { + + override fun update(x: Int, y: Int) { + val bedLeft = gameItemsHolder.getBlock("bed_l") + if (gameWorld.getForeMap(x - 1, y) != bedLeft) { + gameWorld.resetForeMap(x, y) + } + } + + companion object { + const val BLOCK_KEY = "bed_r" + } +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseBedAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseBedAction.kt new file mode 100644 index 0000000..e0f9964 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseBedAction.kt @@ -0,0 +1,31 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +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.item.Item +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UseBedAction @Inject constructor( + private val gameWorld: GameWorld, + private val mobsController: MobsController, + private val gameItemsHolder: GameItemsHolder, +) : IUseItemAction { + + override fun perform(item: Item.Usable, x: Int, y: Int) { + val bedLeft = gameItemsHolder.getBlock("bed_l") + val bedRight = gameItemsHolder.getBlock("bed_r") + + if (gameWorld.canPlaceToForeground(x, y, bedLeft) && gameWorld.canPlaceToForeground(x + 1, y, bedRight)) { + gameWorld.placeToForeground(x, y, bedLeft) + gameWorld.placeToForeground(x + 1, y, bedRight) + mobsController.player.inventory.decreaseCurrentItemAmount() + } + } + + companion object { + const val ACTION_KEY = "use_bed_action" + } +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java index c038f15..d52b50f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java +++ b/core/src/ru/deadsoftware/cavedroid/game/world/GameWorld.java @@ -183,8 +183,12 @@ public class GameWorld { setMap(x, y, BACKGROUND_Z, block); } + public boolean canPlaceToForeground(int x, int y, Block value) { + return !hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision(); + } + public boolean placeToForeground(int x, int y, Block value) { - if (!hasForeAt(x, y) || value == mGameItemsHolder.getFallbackBlock() || !getForeMap(x, y).hasCollision()) { + if (canPlaceToForeground(x, y, value)) { setForeMap(x, y, value); return true; } else if (value instanceof Block.Slab && isSameSlab(value, getForeMap(x, y))) { -- 2.29.2