DEADSOFTWARE

Add use item actions module
authorfredboy <fredboy@protonmail.com>
Fri, 19 Apr 2024 20:06:54 +0000 (03:06 +0700)
committerfredboy <fredboy@protonmail.com>
Fri, 19 Apr 2024 20:07:10 +0000 (03:07 +0700)
14 files changed:
android/assets/json/game_items.json
build.gradle
core/src/ru/deadsoftware/cavedroid/game/GameComponent.java
core/src/ru/deadsoftware/cavedroid/game/GameInput.java
core/src/ru/deadsoftware/cavedroid/game/GameItems.java
core/src/ru/deadsoftware/cavedroid/game/actions/GameActionsModule.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/PlaceBlockActionUtils.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/useitem/IUseItemAction.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToBackgroundAction.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/useitem/PlaceBlockItemToForegroundAction.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseLavaBucketAction.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/actions/useitem/UseWaterBucketAction.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
core/src/ru/deadsoftware/cavedroid/game/objects/Item.kt

index 515ffb6bbf7a9986b94149e570a9a0dba6e00dca..eb1cb2a8eb11a07db189e0d5b5867f0e9a106fd6 100644 (file)
       "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,
index 0dea3f7023f0b68b2bee89b60a258c09be524759..3d03f1f5d0bff2afa819547543714e2cab19fbb8 100644 (file)
@@ -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
index 25779f06851e234c83c818872bf290064eccd82d..8b1b7eefffae23d60c3ba0b5645f0d6622cff0a9 100644 (file)
@@ -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();
 
index 27c4df02e9b4a9ec9e5b05c8850867ef52e6e00f..e26b06bad903c54c3bbc2dcc4cefec8887a0749d 100644 (file)
@@ -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<String, IUseItemAction> 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<String, IUseItemAction> 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");
                 }
             }
         }
index a5d6c23f716681c78acf0d34f923d6fedbbaf3b0..079f4855d88465aebea7b60091b06e773d3ac328 100644 (file)
@@ -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 (file)
index 0000000..672c062
--- /dev/null
@@ -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 (file)
index 0000000..52b97d0
--- /dev/null
@@ -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<String, IUseItemAction>.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<String, IUseItemAction>.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 (file)
index 0000000..809055a
--- /dev/null
@@ -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 (file)
index 0000000..43eefc9
--- /dev/null
@@ -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 (file)
index 0000000..473250e
--- /dev/null
@@ -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 (file)
index 0000000..f9e09f5
--- /dev/null
@@ -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 (file)
index 0000000..fecb77f
--- /dev/null
@@ -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"
+    }
+
+}
index 49c59ee2e8eaae923627e7b81367d63e84778dcb..70013367928b170cd8296542983c7780f2879b84 100644 (file)
@@ -76,6 +76,10 @@ public class Player extends Mob {
         }
     }
 
+    public void setCurrentInventorySlotItem(int itemId) {
+        inventory[slot] = itemId;
+    }
+
     @Override
     public float getSpeed() {
         return SPEED;
index 1932ff8cbb8e31c0887a92dc1fbbac32e18d0056..a4c41b0424d62342a42ff00838f2bada8e594623 100644 (file)
@@ -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 {