summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 546c7c8)
raw | patch | inline | side by side (parent: 546c7c8)
author | fredboy <fredboy@protonmail.com> | |
Fri, 19 Apr 2024 20:06:54 +0000 (03:06 +0700) | ||
committer | fredboy <fredboy@protonmail.com> | |
Fri, 19 Apr 2024 20:07:10 +0000 (03:07 +0700) |
14 files changed:
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,
diff --git a/build.gradle b/build.gradle
index 0dea3f7023f0b68b2bee89b60a258c09be524759..3d03f1f5d0bff2afa819547543714e2cab19fbb8 100644 (file)
--- a/build.gradle
+++ b/build.gradle
appName = "CaveDroid"
gdxVersion = '1.12.0'
guavaVersion = '28.1'
- daggerVersion = '2.40.1'
+ daggerVersion = '2.51.1'
}
repositories {
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 25779f06851e234c83c818872bf290064eccd82d..8b1b7eefffae23d60c3ba0b5645f0d6622cff0a9 100644 (file)
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 27c4df02e9b4a9ec9e5b05c8850867ef52e6e00f..e26b06bad903c54c3bbc2dcc4cefec8887a0749d 100644 (file)
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;
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();
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 a5d6c23f716681c78acf0d34f923d6fedbbaf3b0..079f4855d88465aebea7b60091b06e773d3ac328 100644 (file)
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
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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
--- /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"
+ }
+
+}
diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
index 49c59ee2e8eaae923627e7b81367d63e84778dcb..70013367928b170cd8296542983c7780f2879b84 100644 (file)
}
}
+ 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 1932ff8cbb8e31c0887a92dc1fbbac32e18d0056..a4c41b0424d62342a42ff00838f2bada8e594623 100644 (file)
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 {