From: fredboy Date: Fri, 19 Apr 2024 20:06:54 +0000 (+0700) Subject: Add use item actions module X-Git-Tag: alpha0.5.2~2 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=47099181db7f7d785c9c471cbaca474ebcb67d95;p=cavedroid.git Add use item actions module --- diff --git a/android/assets/json/game_items.json b/android/assets/json/game_items.json index 515ffb6..eb1cb2a 100644 --- a/android/assets/json/game_items.json +++ b/android/assets/json/game_items.json @@ -445,13 +445,15 @@ "id": 68, "name": "Lava Bucket", "origin_x": 0.25, - "type": "bucket" + "type": "bucket", + "action_key": "use_lava_bucket" }, "bucket_water": { "id": 67, "name": "Water Bucket", "origin_x": 0.25, - "type": "bucket" + "type": "bucket", + "action_key": "use_water_bucket" }, "cactus": { "id": 54, diff --git a/build.gradle b/build.gradle index 0dea3f7..3d03f1f 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ allprojects { appName = "CaveDroid" gdxVersion = '1.12.0' guavaVersion = '28.1' - daggerVersion = '2.40.1' + daggerVersion = '2.51.1' } repositories { @@ -70,9 +70,9 @@ project(":core") { dependencies { api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.google.guava:guava:$guavaVersion-android" - api 'com.google.dagger:dagger:2.40.1' + api "com.google.dagger:dagger:$daggerVersion" implementation 'org.jetbrains:annotations:23.1.0' implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.23" - annotationProcessor 'com.google.dagger:dagger-compiler:2.40.1' + annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion" } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java index 25779f0..8b1b7ee 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameComponent.java @@ -2,9 +2,10 @@ package ru.deadsoftware.cavedroid.game; import dagger.Component; import ru.deadsoftware.cavedroid.MainComponent; +import ru.deadsoftware.cavedroid.game.actions.GameActionsModule; @GameScope -@Component(dependencies = MainComponent.class, modules = GameModule.class) +@Component(dependencies = MainComponent.class, modules = { GameModule.class, GameActionsModule.class }) public interface GameComponent { GameProc getGameProc(); diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java index 27c4df0..e26b06b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameInput.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameInput.java @@ -8,26 +8,36 @@ import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.utils.TimeUtils; import com.google.common.collect.Range; import ru.deadsoftware.cavedroid.MainConfig; +import ru.deadsoftware.cavedroid.game.actions.PlaceBlockActionUtilsKt; +import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction; import ru.deadsoftware.cavedroid.game.mobs.Mob; import ru.deadsoftware.cavedroid.game.mobs.MobsController; import ru.deadsoftware.cavedroid.game.mobs.Pig; import ru.deadsoftware.cavedroid.game.mobs.Player; import ru.deadsoftware.cavedroid.game.objects.DropController; +import ru.deadsoftware.cavedroid.game.objects.Item; import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; import ru.deadsoftware.cavedroid.misc.ControlMode; +import javax.annotation.CheckForNull; import javax.inject.Inject; +import java.util.Map; + import static ru.deadsoftware.cavedroid.game.GameItems.*; @GameScope public class GameInput { + private static final String TAG = "GameInput"; + private final MainConfig mMainConfig; private final GameWorld mGameWorld; private final DropController mDropController; private final MobsController mMobsController; + private final Map mUseItemActionMap; + private final Player mPlayer; private ControlMode mControlMode; @@ -51,11 +61,13 @@ public class GameInput { public GameInput(MainConfig mainConfig, GameWorld gameWorld, DropController dropController, - MobsController mobsController) { + MobsController mobsController, + Map useItemActionMap) { mMainConfig = mainConfig; mGameWorld = gameWorld; mDropController = dropController; mMobsController = mobsController; + mUseItemActionMap = useItemActionMap; mPlayer = mMobsController.getPlayer(); @@ -194,24 +206,23 @@ public class GameInput { private void useItem(int x, int y, int id, boolean bg) { mPlayer.startHitting(); - String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id); + if (id > 0) { - if (getItem(id).isBlock()) { + final Item item = getItem(id); + @CheckForNull final String actionKey = item.getActionKey(); + if (item.isBlock()) { if (!bg) { - mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id)); + PlaceBlockActionUtilsKt.placeToForegroundAction(mUseItemActionMap, item, x, y); } else { - mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id)); + PlaceBlockActionUtilsKt.placeToBackgroundAction(mUseItemActionMap, item, x, y); } - } else { - switch (key) { - case "bucket_water": - mGameWorld.placeToForeground(x, y, getBlockId("water")); - mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty"); - break; - case "bucket_lava": - mGameWorld.placeToForeground(x, y, getBlockId("lava")); - mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty"); - break; + } else if (actionKey != null) { + final IUseItemAction useItemAction = mUseItemActionMap.get(actionKey); + + if (useItemAction != null) { + useItemAction.perform(item, x, y); + } else { + Gdx.app.error(TAG, "use item action " + actionKey + "not found"); } } } diff --git a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java index a5d6c23..079f485 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GameItems.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GameItems.java @@ -197,12 +197,14 @@ public class GameItems { int id = Assets.getIntFromJson(item, "id", count); + String actionKey = Assets.getStringFromJson(item, "action_key", null); + if (count >= id) { count++; } itemsIds.put(key, id); - itemsSet.add(new Item(id, key, name, type, sprite, origin)); + itemsSet.add(new Item(id, key, name, type, sprite, origin, actionKey)); } catch (GdxRuntimeException e) { Gdx.app.error(TAG, e.getMessage()); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt new file mode 100644 index 0000000..672c062 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt @@ -0,0 +1,46 @@ +package ru.deadsoftware.cavedroid.game.actions + +import dagger.Binds +import dagger.Module +import dagger.multibindings.IntoMap +import dagger.multibindings.StringKey +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.actions.useitem.* + +@Module +class GameActionsModule { + + @Binds + @IntoMap + @StringKey(UseWaterBucketAction.ACTION_KEY) + @GameScope + fun bindUseWaterBucketAction(action: UseWaterBucketAction): IUseItemAction { + return action + } + + @Binds + @IntoMap + @StringKey(UseLavaBucketAction.ACTION_KEY) + @GameScope + fun bindUseLavaBucketAction(action: UseLavaBucketAction): IUseItemAction { + return action + } + + @Binds + @IntoMap + @StringKey(PlaceBlockItemToForegroundAction.ACTION_KEY) + @GameScope + fun bindPlaceBlockItemToForegroundAction(action: PlaceBlockItemToForegroundAction): IUseItemAction { + return action + } + + @Binds + @IntoMap + @StringKey(PlaceBlockItemToBackgroundAction.ACTION_KEY) + @GameScope + fun bindPlaceBlockItemToBackgroundAction(action: PlaceBlockItemToBackgroundAction): IUseItemAction { + return action + } + + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt new file mode 100644 index 0000000..52b97d0 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt @@ -0,0 +1,19 @@ +package ru.deadsoftware.cavedroid.game.actions + +import com.badlogic.gdx.Gdx +import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction +import ru.deadsoftware.cavedroid.game.actions.useitem.PlaceBlockItemToBackgroundAction +import ru.deadsoftware.cavedroid.game.actions.useitem.PlaceBlockItemToForegroundAction +import ru.deadsoftware.cavedroid.game.objects.Item + +private const val TAG = "PlaceBlockActionUtils" + +fun Map.placeToForegroundAction(item: Item, x: Int, y: Int) { + get(PlaceBlockItemToForegroundAction.ACTION_KEY)?.perform(item, x, y) + ?: Gdx.app.error(TAG, "action place_foreground_block not found") +} + +fun Map.placeToBackgroundAction(item: Item, x: Int, y: Int) { + get(PlaceBlockItemToBackgroundAction.ACTION_KEY)?.perform(item, x, y) + ?: Gdx.app.error(TAG, "action place_background_block not found") +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/IUseItemAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/IUseItemAction.kt new file mode 100644 index 0000000..809055a --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/IUseItemAction.kt @@ -0,0 +1,9 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +import ru.deadsoftware.cavedroid.game.objects.Item + +interface IUseItemAction { + + fun perform(item: Item, x: Int, y: Int) + +} \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToBackgroundAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToBackgroundAction.kt new file mode 100644 index 0000000..43eefc9 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToBackgroundAction.kt @@ -0,0 +1,23 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.objects.Item +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class PlaceBlockItemToBackgroundAction @Inject constructor( + private val gameWorld: GameWorld, +) : IUseItemAction { + + override fun perform(item: Item, x: Int, y: Int) { + val block = item.toBlock() + requireNotNull(block) { "error: trying to place non block item" } + gameWorld.placeToBackground(x, y, block.id) + } + + companion object { + const val ACTION_KEY = "place_background_block" + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToForegroundAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToForegroundAction.kt new file mode 100644 index 0000000..473250e --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToForegroundAction.kt @@ -0,0 +1,23 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.objects.Item +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class PlaceBlockItemToForegroundAction @Inject constructor( + private val gameWorld: GameWorld, +) : IUseItemAction { + + override fun perform(item: Item, x: Int, y: Int) { + val block = item.toBlock() + requireNotNull(block) { "error: trying to place non block item" } + gameWorld.placeToForeground(x, y, block.id) + } + + companion object { + const val ACTION_KEY = "place_foreground_block" + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseLavaBucketAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseLavaBucketAction.kt new file mode 100644 index 0000000..f9e09f5 --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseLavaBucketAction.kt @@ -0,0 +1,24 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +import ru.deadsoftware.cavedroid.game.GameItems +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import ru.deadsoftware.cavedroid.game.objects.Item +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UseLavaBucketAction @Inject constructor( + private val gameWorld: GameWorld, + private val mobsController: MobsController, +) : IUseItemAction { + + override fun perform(item: Item, x: Int, y: Int) { + gameWorld.placeToForeground(x, y, GameItems.getBlockId("lava")) + mobsController.player.setCurrentInventorySlotItem(GameItems.getItemId("bucket_empty")) + } + + companion object { + const val ACTION_KEY = "use_lava_bucket" + } +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseWaterBucketAction.kt b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseWaterBucketAction.kt new file mode 100644 index 0000000..fecb77f --- /dev/null +++ b/core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseWaterBucketAction.kt @@ -0,0 +1,25 @@ +package ru.deadsoftware.cavedroid.game.actions.useitem + +import ru.deadsoftware.cavedroid.game.GameItems +import ru.deadsoftware.cavedroid.game.GameScope +import ru.deadsoftware.cavedroid.game.mobs.MobsController +import ru.deadsoftware.cavedroid.game.objects.Item +import ru.deadsoftware.cavedroid.game.world.GameWorld +import javax.inject.Inject + +@GameScope +class UseWaterBucketAction @Inject constructor( + private val gameWorld: GameWorld, + private val mobsController: MobsController, +) : IUseItemAction { + + override fun perform(item: Item, x: Int, y: Int) { + gameWorld.placeToForeground(x, y, GameItems.getBlockId("water")) + mobsController.player.setCurrentInventorySlotItem(GameItems.getItemId("bucket_empty")) + } + + companion object { + const val ACTION_KEY = "use_water_bucket" + } + +} diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 49c59ee..7001336 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -76,6 +76,10 @@ public class Player extends Mob { } } + public void setCurrentInventorySlotItem(int itemId) { + inventory[slot] = itemId; + } + @Override public float getSpeed() { return SPEED; diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt index 1932ff8..a4c41b0 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt @@ -5,12 +5,13 @@ import ru.deadsoftware.cavedroid.game.GameItems import ru.deadsoftware.cavedroid.misc.utils.SpriteOrigin data class Item( - val id: Int, - val key: String, - val name: String, - val type: String, - val sprite: Sprite?, - val defaultOrigin: SpriteOrigin, + val id: Int, + val key: String, + val name: String, + val type: String, + val sprite: Sprite?, + val defaultOrigin: SpriteOrigin, + val actionKey: String?, ) { init {