DEADSOFTWARE

Add some survival inventory controls
authorfredboy <fredboy@protonmail.com>
Tue, 23 Apr 2024 04:05:17 +0000 (11:05 +0700)
committerfredboy <fredboy@protonmail.com>
Tue, 23 Apr 2024 04:05:17 +0000 (11:05 +0700)
core/src/ru/deadsoftware/cavedroid/game/GameRenderer.java
core/src/ru/deadsoftware/cavedroid/game/input/MouseInputHandlersModule.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt
core/src/ru/deadsoftware/cavedroid/game/render/TouchControlsRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/render/windows/SurvivalWindowRenderer.kt
core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsConfigs.kt
core/src/ru/deadsoftware/cavedroid/game/windows/GameWindowsManager.kt

index 7647e69c2a6c0ad9d782de5b5533ccc75123fec1..8118fec05abe59756d90d568f06851b4e7243c45 100644 (file)
@@ -16,6 +16,7 @@ import ru.deadsoftware.cavedroid.game.mobs.MobsController;
 import ru.deadsoftware.cavedroid.game.mobs.Player;
 import ru.deadsoftware.cavedroid.game.objects.TouchButton;
 import ru.deadsoftware.cavedroid.game.render.IGameRenderer;
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager;
 import ru.deadsoftware.cavedroid.misc.Assets;
 import ru.deadsoftware.cavedroid.misc.Renderer;
 
@@ -39,6 +40,7 @@ public class GameRenderer extends Renderer {
     private final KeyboardInputActionMapper mKeyboardInputActionMapper;
     private final Set<IGameInputHandler<MouseInputAction>> mMouseInputHandlers;
     private final Set<IGameInputHandler<KeyboardInputAction>> mKeyboardInputHandlers;
+    private final GameWindowsManager mGameWindowsManager;
 
     @Inject
     GameRenderer(MainConfig mainConfig,
@@ -48,7 +50,8 @@ public class GameRenderer extends Renderer {
                  MouseInputActionMapper mouseInputActionMapper,
                  KeyboardInputActionMapper keyboardInputActionMapper,
                  Set<IGameInputHandler<MouseInputAction>> mouseInputHandlers,
-                 Set<IGameInputHandler<KeyboardInputAction>> keyboardInputHandlers) {
+                 Set<IGameInputHandler<KeyboardInputAction>> keyboardInputHandlers,
+                 GameWindowsManager gameWindowsManager) {
         super(mainConfig.getWidth(), mainConfig.getHeight());
 
         mMainConfig = mainConfig;
@@ -60,6 +63,7 @@ public class GameRenderer extends Renderer {
         mKeyboardInputActionMapper = keyboardInputActionMapper;
         mMouseInputHandlers = mouseInputHandlers;
         mKeyboardInputHandlers = keyboardInputHandlers;
+        mGameWindowsManager = gameWindowsManager;
 
         Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
     }
@@ -134,6 +138,9 @@ public class GameRenderer extends Renderer {
     }
 
     private TouchButton getTouchedKey(float touchX, float touchY) {
+        if (mGameWindowsManager.getCurrentWindow() != GameUiWindow.NONE) {
+            return nullButton;
+        }
         for (ObjectMap.Entry<String, TouchButton> entry : Assets.guiMap) {
             TouchButton button = entry.value;
             if (button.getRect().contains(touchX, touchY)) {
index e17662eb22bc05631d1f5fb9c7d2402d8b8b2bc4..aef4c0bca680d01f3624114a19bf6e92ee5ce3df 100644 (file)
@@ -58,4 +58,11 @@ object MouseInputHandlersModule {
     fun bindUseItemMouseInputActionHandler(handler: UseItemMouseInputHandler): IGameInputHandler<MouseInputAction> {
         return handler
     }
+
+    @Binds
+    @IntoSet
+    @GameScope
+    fun bindSelectSurvivalInventoryItemMouseInputHandler(handler: SelectSurvivalInventoryItemMouseInputHandler): IGameInputHandler<MouseInputAction> {
+        return handler
+    }
 }
\ No newline at end of file
diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt
new file mode 100644 (file)
index 0000000..559ccfc
--- /dev/null
@@ -0,0 +1,65 @@
+package ru.deadsoftware.cavedroid.game.input.handler.mouse
+
+import com.badlogic.gdx.Gdx
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import ru.deadsoftware.cavedroid.game.GameScope
+import ru.deadsoftware.cavedroid.game.GameUiWindow
+import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
+import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
+import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
+import ru.deadsoftware.cavedroid.game.input.isInsideWindow
+import ru.deadsoftware.cavedroid.game.mobs.MobsController
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
+import ru.deadsoftware.cavedroid.misc.Assets
+import javax.inject.Inject
+
+@GameScope
+class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
+    private val gameWindowsManager: GameWindowsManager,
+    private val mobsController: MobsController,
+    private val gameItemsHolder: GameItemsHolder,
+) : IGameInputHandler<MouseInputAction> {
+
+    private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
+
+    override fun checkConditions(action: MouseInputAction): Boolean {
+        return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
+            isInsideWindow(action, survivalWindowTexture) &&
+                (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch)
+                && action.actionKey.touchUp
+    }
+
+    override fun handle(action: MouseInputAction) {
+        val survivalTexture = survivalWindowTexture
+        val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2 +
+                GameWindowsConfigs.Survival.itemsGridMarginLeft)) /
+                GameWindowsConfigs.Survival.itemsGridColWidth
+        val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2 +
+                GameWindowsConfigs.Survival.itemsGridMarginTop)) /
+                GameWindowsConfigs.Survival.itemsGridRowHeight
+
+        if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Survival.itemsInRow ||
+            yOnGrid < 0 || yOnGrid > GameWindowsConfigs.Survival.itemsInCol) {
+            return
+        }
+
+        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
+        itemIndex += GameWindowsConfigs.Survival.hotbarCells
+
+        if (itemIndex >= mobsController.player.inventory.size) {
+            itemIndex -= mobsController.player.inventory.size
+        }
+
+        val item = mobsController.player.inventory[itemIndex]
+        mobsController.player.inventory[itemIndex] = gameWindowsManager.selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem()
+        gameWindowsManager.selectedItem = item
+
+        Gdx.app.debug(TAG, "selected item: ${gameWindowsManager.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)")
+    }
+
+    companion object {
+        private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
+
+    }
+}
\ No newline at end of file
index 49cc59d6a5366f484df8a51a6ecd400d538a2a14..89186595d360ee3f2642306baefeb893ab94e223 100644 (file)
@@ -35,6 +35,19 @@ class InventoryItem @JvmOverloads constructor(
         spriteBatch.drawString(text, x, y, Color.WHITE)
     }
 
+    fun drawSelected(spriteBatch: SpriteBatch, x: Float, y: Float) {
+        if (item.isNone()) {
+            return
+        }
+
+        val sprite = item.sprite
+        sprite.setOriginCenter()
+        sprite.setPosition(x, y)
+        sprite.setScale(1.25f)
+        sprite.draw(spriteBatch)
+        sprite.setScale(1f)
+    }
+
     fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, x: Float, y: Float) {
         if (item.isNone()) {
             return
index d9dc5c48ab9b01818874852802643cc256aafe2f..5c95d696c675cadba52dcaf7fd1d8186e9a79491 100644 (file)
@@ -5,8 +5,10 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer
 import com.badlogic.gdx.math.Rectangle
 import ru.deadsoftware.cavedroid.MainConfig
 import ru.deadsoftware.cavedroid.game.GameScope
+import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
 import ru.deadsoftware.cavedroid.game.mobs.Player.ControlMode
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.misc.Assets
 import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component1
 import ru.deadsoftware.cavedroid.misc.utils.ArrayMapExtensions.component2
@@ -16,6 +18,7 @@ import javax.inject.Inject
 class TouchControlsRenderer @Inject constructor(
     private val mainConfig: MainConfig,
     private val mobsController: MobsController,
+    private val gameWindowsManager: GameWindowsManager,
 ) : IGameRenderer {
 
     override val renderLayer get() = RENDER_LAYER
@@ -23,7 +26,7 @@ class TouchControlsRenderer @Inject constructor(
     private val shadeTexture get() = Assets.textureRegions[SHADE_KEY]
 
     override fun draw(spriteBatch: SpriteBatch, shapeRenderer: ShapeRenderer, viewport: Rectangle, delta: Float) {
-        if (!mainConfig.isTouch) {
+        if (!mainConfig.isTouch || gameWindowsManager.getCurrentWindow() != GameUiWindow.NONE) {
             return
         }
 
index f8a6ac00b929ce8b9305716b76d1eeda35e946bd..39580de82cecd8a42d63a0dcf9eb25fd991ddb97 100644 (file)
@@ -11,6 +11,8 @@ import ru.deadsoftware.cavedroid.game.mobs.Mob
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
 import ru.deadsoftware.cavedroid.game.render.IGameRenderer
 import ru.deadsoftware.cavedroid.game.render.WindowsRenderer
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs
+import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.misc.Assets
 import javax.inject.Inject
 import kotlin.math.atan
@@ -19,6 +21,7 @@ import kotlin.math.atan
 class SurvivalWindowRenderer @Inject constructor(
     private val mainConfig: MainConfig,
     private val mobsController: MobsController,
+    private val gameWindowsManager: GameWindowsManager,
 ) : AbstractWindowRenderer(), IGameRenderer {
 
     override val renderLayer get() = WindowsRenderer.RENDER_LAYER
@@ -47,10 +50,10 @@ class SurvivalWindowRenderer @Inject constructor(
     }
 
     private fun drawPlayerPortrait(spriteBatch: SpriteBatch, windowX: Float, windowY: Float, delta: Float) {
-        val portraitX = windowX + Config.portraitMarginLeft +
-                (Config.portraitWidth / 2 - mobsController.player.width / 2)
-        val portraitY = windowY + Config.portraitMarginTop +
-                (Config.portraitHeight / 2 - mobsController.player.height / 2)
+        val portraitX = windowX + GameWindowsConfigs.Survival.portraitMarginLeft +
+                (GameWindowsConfigs.Survival.portraitWidth / 2 - mobsController.player.width / 2)
+        val portraitY = windowY + GameWindowsConfigs.Survival.portraitMarginTop +
+                (GameWindowsConfigs.Survival.portraitHeight / 2 - mobsController.player.height / 2)
 
         setPortraitHeadRotation(portraitX, portraitY)
         mobsController.player.draw(spriteBatch, portraitX, portraitY, delta)
@@ -69,51 +72,38 @@ class SurvivalWindowRenderer @Inject constructor(
         drawItemsGrid(
             spriteBatch = spriteBatch,
             shapeRenderer = shapeRenderer,
-            gridX = windowX + Config.itemsGridMarginLeft,
-            gridY = windowY + Config.itemsGridMarginTop,
+            gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
+            gridY = windowY + GameWindowsConfigs.Survival.itemsGridMarginTop,
             items = mobsController.player.inventory.asSequence()
-                .drop(Config.hotbarCells)
-                .take(Config.itemsInCol * Config.itemsInRow)
+                .drop(GameWindowsConfigs.Survival.hotbarCells)
+                .take(GameWindowsConfigs.Survival.itemsInCol * GameWindowsConfigs.Survival.itemsInRow)
                 .asIterable(),
-            itemsInRow = Config.itemsInRow,
-            cellWidth = Config.itemsGridColWidth,
-            cellHeight = Config.itemsGridRowHeight,
+            itemsInRow = GameWindowsConfigs.Survival.itemsInRow,
+            cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth,
+            cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight,
         )
 
         drawItemsGrid(
             spriteBatch = spriteBatch,
             shapeRenderer = shapeRenderer,
-            gridX = windowX + Config.itemsGridMarginLeft,
-            gridY = windowY + survivalWindow.regionHeight - Config.hotbarOffsetFromBottom,
+            gridX = windowX + GameWindowsConfigs.Survival.itemsGridMarginLeft,
+            gridY = windowY + survivalWindow.regionHeight - GameWindowsConfigs.Survival.hotbarOffsetFromBottom,
             items = mobsController.player.inventory.asSequence()
-                .take(Config.hotbarCells)
+                .take(GameWindowsConfigs.Survival.hotbarCells)
                 .asIterable(),
-            itemsInRow = Config.hotbarCells,
-            cellWidth = Config.itemsGridColWidth,
-            cellHeight = Config.itemsGridRowHeight,
+            itemsInRow = GameWindowsConfigs.Survival.hotbarCells,
+            cellWidth = GameWindowsConfigs.Survival.itemsGridColWidth,
+            cellHeight = GameWindowsConfigs.Survival.itemsGridRowHeight,
+        )
+
+        gameWindowsManager.selectedItem?.drawSelected(
+            spriteBatch = spriteBatch,
+            x = Gdx.input.x * (viewport.width / Gdx.graphics.width),
+            y = Gdx.input.y * (viewport.height / Gdx.graphics.height)
         )
     }
 
     companion object {
         private const val SURVIVAL_WINDOW_KEY = "survival"
-
-        private data object Config {
-            const val itemsGridMarginLeft = 8f
-            const val itemsGridMarginTop = 84f
-
-            const val itemsGridRowHeight = 18f
-            const val itemsGridColWidth = 18f
-
-            const val itemsInRow = 8
-            const val itemsInCol = 5
-
-            const val hotbarOffsetFromBottom = 24f
-            const val hotbarCells = 9
-
-            const val portraitMarginLeft = 24f
-            const val portraitMarginTop = 8f
-            const val portraitWidth = 48f
-            const val portraitHeight = 68f
-        }
     }
 }
\ No newline at end of file
index cf8112720b64d60158e755f58d32c088e9c7c03f..6dde753a860c02360c47631413e310f897a6813e 100644 (file)
@@ -21,4 +21,23 @@ object GameWindowsConfigs {
 
         val itemsOnPage get() = itemsInCol * itemsInRow
     }
+
+    data object Survival {
+        const val itemsGridMarginLeft = 8f
+        const val itemsGridMarginTop = 84f
+
+        const val itemsGridRowHeight = 18f
+        const val itemsGridColWidth = 18f
+
+        const val itemsInRow = 9
+        const val itemsInCol = 5
+
+        const val hotbarOffsetFromBottom = 24f
+        const val hotbarCells = 9
+
+        const val portraitMarginLeft = 24f
+        const val portraitMarginTop = 8f
+        const val portraitWidth = 48f
+        const val portraitHeight = 68f
+    }
 }
\ No newline at end of file
index 22476350367c54e5f4b5d3a0fa949b648748589f..302562fb9b60d086b30bc79ca6b1baf22099ef12 100644 (file)
@@ -4,6 +4,7 @@ import ru.deadsoftware.cavedroid.MainConfig
 import ru.deadsoftware.cavedroid.game.GameScope
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
 import javax.inject.Inject
 
 @GameScope
@@ -14,6 +15,7 @@ class GameWindowsManager @Inject constructor(
 
     var creativeScrollAmount = 0
     var isDragging = false
+    var selectedItem: InventoryItem? = null
 
     fun getCurrentWindow(): GameUiWindow {
         return mainConfig.gameUiWindow