DEADSOFTWARE

Refactor window controls
authorfredboy <fredboy@protonmail.com>
Fri, 10 May 2024 17:03:51 +0000 (00:03 +0700)
committerfredboy <fredboy@protonmail.com>
Fri, 10 May 2024 17:03:51 +0000 (00:03 +0700)
13 files changed:
core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/AbstractInventoryItemsMouseInputHandler.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectChestInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectFurnaceInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectSurvivalInventoryItemMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt
core/src/ru/deadsoftware/cavedroid/game/model/item/InventoryItem.kt
core/src/ru/deadsoftware/cavedroid/game/ui/windows/GameWindowsManager.kt
core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/AbstractInventoryWindow.kt
core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/AbstractInventoryWindowWithCraftGrid.kt [new file with mode: 0644]
core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/CraftingInventoryWindow.kt
core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/SurvivalInventoryWindow.kt

index aebf8eb654eb03b7865162b6ec721557c1eddb5c..d9bf21fe5dec302fad3e1cf984e7d44eae22852e 100644 (file)
@@ -118,7 +118,7 @@ public class GamePhysics {
     private Rectangle getShiftedMagnetingPlayerRect(Drop drop) {
         final Player player = mMobsController.getPlayer();
 
-        if (!player.inventory.canPickItem(drop.getItem())) {
+        if (!player.inventory.canPickItem(drop)) {
             return null;
         }
 
diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/AbstractInventoryItemsMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/AbstractInventoryItemsMouseInputHandler.kt
new file mode 100644 (file)
index 0000000..4aaff27
--- /dev/null
@@ -0,0 +1,104 @@
+package ru.deadsoftware.cavedroid.game.input.handler.mouse
+
+import com.badlogic.gdx.graphics.g2d.TextureRegion
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+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.model.item.InventoryItem
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
+import ru.deadsoftware.cavedroid.game.objects.container.Furnace
+import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
+import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindow
+import ru.deadsoftware.cavedroid.game.ui.windows.inventory.AbstractInventoryWindowWithCraftGrid
+
+abstract class AbstractInventoryItemsMouseInputHandler(
+    private val gameItemsHolder: GameItemsHolder,
+    private val gameWindowsManager: GameWindowsManager,
+    private val windowType: GameUiWindow,
+) : IGameInputHandler<MouseInputAction> {
+
+    protected abstract val windowTexture: TextureRegion
+
+    override fun checkConditions(action: MouseInputAction): Boolean {
+        return gameWindowsManager.getCurrentWindow() == windowType &&
+                isInsideWindow(action, windowTexture) &&
+                (action.actionKey is MouseInputActionKey.Left ||
+                        action.actionKey is MouseInputActionKey.Right ||
+                        action.actionKey is MouseInputActionKey.Screen)
+                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
+    }
+
+    protected fun updateCraftResult(window: AbstractInventoryWindowWithCraftGrid) {
+        window.craftResult = gameItemsHolder.craftItem(window.craftingItems.map(InventoryItem::item))
+            ?: gameItemsHolder.fallbackItem.toInventoryItem()
+    }
+
+    private fun reduceCraftItems(window: AbstractInventoryWindowWithCraftGrid) {
+        for (i in window.craftingItems.indices) {
+            if (window.craftingItems[i].amount > 1) {
+                window.craftingItems[i].amount--
+            } else {
+                window.craftingItems[i] = gameItemsHolder.fallbackItem.toInventoryItem()
+            }
+        }
+    }
+
+    protected fun handleInsidePlaceableCell(
+        action: MouseInputAction,
+        items: MutableList<InventoryItem>,
+        window: AbstractInventoryWindow,
+        index: Int
+    ) {
+        if (action.actionKey is MouseInputActionKey.Screen) {
+            if (!action.actionKey.touchUp) {
+                window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
+            } else {
+                if (action.actionKey.pointer == window.selectItemPointer) {
+                    window.onLeftCLick(items, gameItemsHolder, index, action.actionKey.pointer)
+                } else {
+                    window.onRightClick(items, index)
+                }
+            }
+        } else if (action.actionKey is MouseInputActionKey.Left) {
+            window.onLeftCLick(items, gameItemsHolder, index)
+        } else {
+            window.onRightClick(items, index)
+        }
+    }
+
+    protected fun handleInsideCraftResultCell(
+        action: MouseInputAction,
+        items: MutableList<InventoryItem>,
+        window: AbstractInventoryWindow,
+        index: Int
+    ) {
+        val selectedItem = window.selectedItem
+
+        if (!selectedItem.isNoneOrNull() && (selectedItem.item != items[index].item ||
+                    !selectedItem.canBeAdded(items[index].amount))) {
+            return
+        }
+
+        if (!selectedItem.isNoneOrNull()) {
+            selectedItem.amount += items[index].amount
+            items[index] = gameItemsHolder.fallbackItem.toInventoryItem()
+        } else {
+            if (action.actionKey is MouseInputActionKey.Screen) {
+                if (!action.actionKey.touchUp) {
+                    window.onLeftCLick(items, gameItemsHolder, Furnace.RESULT_INDEX, action.actionKey.pointer)
+                }
+            } else if (action.actionKey is MouseInputActionKey.Left) {
+                window.onLeftCLick(items, gameItemsHolder, index)
+            }
+        }
+
+        if (window is AbstractInventoryWindowWithCraftGrid) {
+            reduceCraftItems(window)
+        }
+
+    }
+
+}
\ No newline at end of file
index 3d1bfae969db22a0ea3594c6766e173d64951721..d47202e181bfe330a010c991668a54b15032f300 100644 (file)
@@ -1,20 +1,13 @@
 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.model.item.InventoryItem
-import ru.deadsoftware.cavedroid.game.objects.drop.DropController
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.ChestInventoryWindow
-import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
 import ru.deadsoftware.cavedroid.misc.Assets
 import javax.inject.Inject
 
@@ -23,82 +16,35 @@ class SelectChestInventoryItemMouseInputHandler @Inject constructor(
     private val gameWindowsManager: GameWindowsManager,
     private val mobsController: MobsController,
     private val gameItemsHolder: GameItemsHolder,
-    private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CHEST) {
 
-    private val chestWindowTexture get() = requireNotNull(Assets.textureRegions["chest"])
-
-    override fun checkConditions(action: MouseInputAction): Boolean {
-        return gameWindowsManager.getCurrentWindow() == GameUiWindow.CHEST &&
-                isInsideWindow(action, chestWindowTexture) &&
-                (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
-                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
-    }
+    override val windowTexture get() = requireNotNull(Assets.textureRegions["chest"])
 
     private fun handleInsideContentGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as ChestInventoryWindow
+        val itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.contentsInRow
 
-        val itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Chest.contentsInRow))
-
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.chest.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.chest.items  as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.chest.items  as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(window.chest.items  as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(window.chest.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, window.chest.items, window, itemIndex)
     }
 
     private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as ChestInventoryWindow
 
-        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Chest.itemsInRow))
+        var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.itemsInRow
         itemIndex += GameWindowsConfigs.Chest.hotbarCells
 
         if (itemIndex >= mobsController.player.inventory.size) {
             itemIndex -= mobsController.player.inventory.size
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
     }
 
     override fun handle(action: MouseInputAction) {
-        val chestTexture = chestWindowTexture
+        val texture = windowTexture
 
-        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - chestTexture.regionWidth / 2)
-        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - chestTexture.regionHeight / 2)
+        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
+        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
 
         val xOnGrid = (xOnWindow - GameWindowsConfigs.Chest.itemsGridMarginLeft) /
                 GameWindowsConfigs.Chest.itemsGridColWidth
@@ -123,9 +69,4 @@ class SelectChestInventoryItemMouseInputHandler @Inject constructor(
             handleInsideContentGrid(action, xOnContent.toInt(), yOnContent.toInt())
         }
     }
-
-    companion object {
-        private const val TAG = "SelectChestInventoryItemMouseInputHandler"
-
-    }
 }
\ No newline at end of file
index 32c7a43f5e221b3b91b4587d4ad3f138481a5415..88e362005316a756b7e0aaf829493fe20d5a3747 100644 (file)
@@ -1,16 +1,10 @@
 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.model.item.InventoryItem
-import ru.deadsoftware.cavedroid.game.objects.drop.DropController
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.CraftingInventoryWindow
@@ -22,80 +16,45 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
     private val gameWindowsManager: GameWindowsManager,
     private val mobsController: MobsController,
     private val gameItemsHolder: GameItemsHolder,
-    private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CRAFTING_TABLE) {
 
-    private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
-
-    override fun checkConditions(action: MouseInputAction): Boolean {
-        return gameWindowsManager.getCurrentWindow() == GameUiWindow.CRAFTING_TABLE &&
-                isInsideWindow(action, survivalWindowTexture) &&
-                (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
-                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
-    }
+    override val windowTexture get() = requireNotNull(Assets.textureRegions["crafting_table"])
 
     private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
 
-        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Crafting.itemsInRow))
+        var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Crafting.itemsInRow
         itemIndex += GameWindowsConfigs.Crafting.hotbarCells
 
         if (itemIndex >= mobsController.player.inventory.size) {
             itemIndex -= mobsController.player.inventory.size
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
     }
 
     private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
         val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
         val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.craftingItems, index)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.craftingItems, gameItemsHolder, index)
-        } else {
-            window.onRightClick(window.craftingItems, index)
-        }
+        handleInsidePlaceableCell(action, window.craftingItems, window, index)
 
-        window.craftResult =
-            gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
+        updateCraftResult(window)
     }
 
-    override fun handle(action: MouseInputAction) {
-        val survivalTexture = survivalWindowTexture
+    private fun handleInsideCraftResult(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
 
-        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
-        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
+        handleInsideCraftResultCell(action, window.craftResultList, window, 0)
+
+        updateCraftResult(window)
+    }
+
+    override fun handle(action: MouseInputAction) {
+        val texture = windowTexture
+
+        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
+        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
 
         val xOnGrid = (xOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginLeft) /
                 GameWindowsConfigs.Crafting.itemsGridColWidth
@@ -122,31 +81,9 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
             handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
         } else if (isInsideCraftGrid) {
             handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
-        } else if (isInsideCraftResult && action.actionKey.touchUp) {
-            val selectedItem = window.selectedItem
-            if (selectedItem == null || selectedItem.item.isNone() ||
-                (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
-                for (i in window.craftingItems.indices) {
-                    if ((window.craftingItems[i]?.amount ?: 0) > 1) {
-                        window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
-                    } else {
-                        window.craftingItems[i] = null
-                    }
-                }
-                if (selectedItem != null && !selectedItem.item.isNone()) {
-                    selectedItem.amount += (window.craftResult?.amount ?: 0)
-                } else {
-                    window.selectedItem = window.craftResult
-                }
-                window.craftResult = gameItemsHolder.craftItem(window.craftingItems
-                    .map { it?.item ?: gameItemsHolder.fallbackItem })
-            }
+        } else if (isInsideCraftResult) {
+            handleInsideCraftResult(action)
         }
 
     }
-
-    companion object {
-        private const val TAG = "SelectCraftingInventoryItemMouseInputHandler"
-
-    }
 }
\ No newline at end of file
index e11c433ad3749d5f136c66f9bff9f95174124166..f4a00eb9b18d22ed512557393bde5ba1e1342790 100644 (file)
@@ -1,17 +1,11 @@
 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.model.item.InventoryItem
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
-import ru.deadsoftware.cavedroid.game.objects.drop.DropController
 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
@@ -24,48 +18,21 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
     private val gameWindowsManager: GameWindowsManager,
     private val mobsController: MobsController,
     private val gameItemsHolder: GameItemsHolder,
-    private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.FURNACE) {
 
-    private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
-
-    override fun checkConditions(action: MouseInputAction): Boolean {
-        return gameWindowsManager.getCurrentWindow() == GameUiWindow.FURNACE &&
-                isInsideWindow(action, survivalWindowTexture) &&
-                (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
-                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
-    }
+    override val windowTexture get() = requireNotNull(Assets.textureRegions["furnace"])
 
     private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Furnace.itemsInRow))
+        var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Furnace.itemsInRow
         itemIndex += GameWindowsConfigs.Furnace.hotbarCells
 
         if (itemIndex >= mobsController.player.inventory.size) {
             itemIndex -= mobsController.player.inventory.size
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
     }
 
     private fun handleInsideFuel(action: MouseInputAction) {
@@ -75,49 +42,26 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
             return
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.FUEL_INDEX)
-        } else {
-            window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.FUEL_INDEX)
-        }
+        handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.FUEL_INDEX)
     }
 
     private fun handleInsideInput(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.furnace.items as MutableList<InventoryItem?>, gameItemsHolder, Furnace.INPUT_INDEX)
-        } else {
-            window.onRightClick(window.furnace.items as MutableList<InventoryItem?>, Furnace.INPUT_INDEX)
-        }
+        handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.INPUT_INDEX)
     }
 
-    override fun handle(action: MouseInputAction) {
-        val survivalTexture = survivalWindowTexture
+    private fun handleInsideResult(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
 
-        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
-        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
+        handleInsideCraftResultCell(action, window.furnace.items, window, Furnace.RESULT_INDEX)
+    }
+
+    override fun handle(action: MouseInputAction) {
+        val texture = windowTexture
+
+        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
+        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
 
         val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
                 GameWindowsConfigs.Furnace.itemsGridColWidth
@@ -148,24 +92,9 @@ class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
             handleInsideFuel(action)
         } else if (isInsideInput) {
             handleInsideInput(action)
-        } else if (isInsideResult && action.actionKey.touchUp) {
-            val selectedItem = window.selectedItem
-            if (selectedItem == null || selectedItem.item.isNone() ||
-                (selectedItem.item == window.furnace.result?.item && selectedItem.amount + (window.furnace.result?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
-
-                if (selectedItem != null && !selectedItem.item.isNone()) {
-                    selectedItem.amount += (window.furnace.result?.amount ?: 0)
-                } else {
-                    window.selectedItem = window.furnace.result
-                }
-                window.furnace.items[Furnace.RESULT_INDEX] = gameItemsHolder.fallbackItem.toInventoryItem()
-            }
+        } else if (isInsideResult) {
+            handleInsideResult(action)
         }
 
     }
-
-    companion object {
-        private const val TAG = "SelectFurnaceInventoryItemMouseInputHandler"
-
-    }
 }
\ No newline at end of file
index 263d59e90699e9b8874089199c6cc214f5de21f0..0a455ab2c70c47e53824d4225a0d395078c0809e 100644 (file)
@@ -1,16 +1,10 @@
 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.model.item.InventoryItem
-import ru.deadsoftware.cavedroid.game.objects.drop.DropController
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
@@ -22,80 +16,43 @@ class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
     private val gameWindowsManager: GameWindowsManager,
     private val mobsController: MobsController,
     private val gameItemsHolder: GameItemsHolder,
-    private val dropController: DropController,
-) : IGameInputHandler<MouseInputAction> {
+) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.SURVIVAL_INVENTORY) {
 
-    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.Right || action.actionKey is MouseInputActionKey.Screen)
-                && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
-    }
+    override val windowTexture get() = requireNotNull(Assets.textureRegions["survival"])
 
     private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
         val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
 
-        var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
+        var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
         itemIndex += GameWindowsConfigs.Survival.hotbarCells
 
         if (itemIndex >= mobsController.player.inventory.size) {
             itemIndex -= mobsController.player.inventory.size
         }
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left) {
-            window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
-        } else {
-            window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
-        }
-
-        Gdx.app.debug(
-            TAG,
-            "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
-        )
+        handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
     }
 
     private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
         val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
         val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
 
-        if (action.actionKey is MouseInputActionKey.Screen) {
-            if (!action.actionKey.touchUp) {
-                window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
-            } else {
-                if (action.actionKey.pointer == window.selectItemPointer) {
-                    window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
-                } else {
-                    window.onRightClick(window.craftingItems, index)
-                }
-            }
-        } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
-            window.onLeftCLick(window.craftingItems, gameItemsHolder, index)
-        } else {
-            window.onRightClick(window.craftingItems, index)
-        }
+        handleInsidePlaceableCell(action, window.craftingItems, window, index)
 
-        window.craftResult =
-            gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
+        updateCraftResult(window)
     }
 
-    override fun handle(action: MouseInputAction) {
-        val survivalTexture = survivalWindowTexture
+    private fun handleInsideCraftResult(action: MouseInputAction) {
         val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
 
-        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
-        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
+        handleInsideCraftResultCell(action, window.craftResultList, window, 0)
+
+        updateCraftResult(window)
+    }
+
+    override fun handle(action: MouseInputAction) {
+        val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - windowTexture.regionWidth / 2)
+        val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - windowTexture.regionHeight / 2)
 
         val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
                 GameWindowsConfigs.Survival.itemsGridColWidth
@@ -122,31 +79,9 @@ class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
             handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
         } else if (isInsideCraftGrid) {
             handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
-        } else if (isInsideCraftResult && action.actionKey.touchUp) {
-            val selectedItem = window.selectedItem
-            if (selectedItem == null || selectedItem.item.isNone() ||
-                (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
-                for (i in window.craftingItems.indices) {
-                    if ((window.craftingItems[i]?.amount ?: 0) > 1) {
-                        window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
-                    } else {
-                        window.craftingItems[i] = null
-                    }
-                }
-                if (selectedItem != null && !selectedItem.item.isNone()) {
-                    selectedItem.amount += (window.craftResult?.amount ?: 0)
-                } else {
-                    window.selectedItem = window.craftResult
-                }
-                window.craftResult = gameItemsHolder.craftItem(window.craftingItems
-                    .map { it?.item ?: gameItemsHolder.fallbackItem })
-            }
+        } else if (isInsideCraftResult) {
+            handleInsideCraftResult(action)
         }
 
     }
-
-    companion object {
-        private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
-
-    }
 }
\ No newline at end of file
index c5e10d06b30600a2c4aaac74ebedd9a97d67a781..c6dea8bc878f0d523be0fc447979961080fc94df 100644 (file)
@@ -59,11 +59,13 @@ class Inventory(
         }
     }
 
-    private fun getItemPickSlot(item: Item): Int {
+    private fun getItemPickSlot(drop: Drop): Int {
+        val item = drop.item
+
         for (i in _items.indices) {
             val inventoryItem = _items[i]
 
-            if (item == inventoryItem.item && inventoryItem.canBeAdded()) {
+            if (item == inventoryItem.item && inventoryItem.canBeAdded(drop.amount)) {
                 return i
             }
         }
@@ -79,12 +81,12 @@ class Inventory(
         return -1
     }
 
-    fun canPickItem(item: Item): Boolean {
-        return getItemPickSlot(item) >= 0
+    fun canPickItem(drop: Drop): Boolean {
+        return getItemPickSlot(drop) >= 0
     }
 
     fun pickDrop(drop: Drop) {
-        val slot = getItemPickSlot(drop.item).takeIf { it >= 0 } ?: return
+        val slot = getItemPickSlot(drop).takeIf { it >= 0 } ?: return
         val inventoryItem = _items[slot]
 
         if (inventoryItem.item == drop.item) {
index 44d3179839d2507e2f42dbcc33d0117c645d777d..f847f7d0e3dd4d1f19ed91ee37266d3355a218c0 100644 (file)
@@ -10,6 +10,8 @@ import ru.deadsoftware.cavedroid.misc.utils.drawSprite
 import ru.deadsoftware.cavedroid.misc.utils.drawString
 import ru.deadsoftware.cavedroid.misc.utils.px
 import java.io.Serializable
+import kotlin.contracts.ExperimentalContracts
+import kotlin.contracts.contract
 
 class InventoryItem @JvmOverloads constructor(
     val itemKey: String,
@@ -123,6 +125,11 @@ class InventoryItem @JvmOverloads constructor(
     }
 
     companion object {
-        fun InventoryItem?.isNoneOrNull() = this?.item == null || this.item.isNone()
+
+        @OptIn(ExperimentalContracts::class)
+        fun InventoryItem?.isNoneOrNull(): Boolean {
+            contract { returns(false) implies(this@isNoneOrNull != null) }
+            return this?.item == null || this.item.isNone()
+        }
     }
 }
index 32494ed4e7721784b9a057b129819a4451cf34a3..8d32f94e098322301c342a0308e9c7b43208f1a9 100644 (file)
@@ -1,5 +1,6 @@
 package ru.deadsoftware.cavedroid.game.ui.windows
 
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameScope
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.mobs.MobsController
@@ -15,6 +16,7 @@ class GameWindowsManager @Inject constructor(
     private val tooltipManager: TooltipManager,
     private val mobsController: MobsController,
     private val dropController: DropController,
+    private val gameItemsHolder: GameItemsHolder,
 ) {
 
     var creativeScrollAmount = 0
@@ -31,7 +33,7 @@ class GameWindowsManager @Inject constructor(
         if (mobsController.player.gameMode == 1) {
             currentWindow = CreativeInventoryWindow()
         } else {
-            currentWindow = SurvivalInventoryWindow()
+            currentWindow = SurvivalInventoryWindow(gameItemsHolder)
         }
     }
 
@@ -44,23 +46,13 @@ class GameWindowsManager @Inject constructor(
     }
 
     fun openCrafting() {
-        currentWindow = CraftingInventoryWindow()
+        currentWindow = CraftingInventoryWindow(gameItemsHolder)
     }
 
     fun closeWindow() {
-        (currentWindow as? SurvivalInventoryWindow)?.let { window ->
+        (currentWindow as? AbstractInventoryWindowWithCraftGrid)?.let { window ->
             window.craftingItems.forEach { item ->
-                item?.item?.let {
-                    dropController.addDrop(mobsController.player.x, mobsController.player.y, it, item.amount)
-                }
-            }
-        }
-
-        (currentWindow as? CraftingInventoryWindow)?.let { window ->
-            window.craftingItems.forEach { item ->
-                item?.item?.let {
-                    dropController.addDrop(mobsController.player.x, mobsController.player.y, it, item.amount)
-                }
+                dropController.addDrop(mobsController.player.x, mobsController.player.y, item)
             }
         }
 
index f49ba4908d1cd91b88ce7918ce823a8fef715d57..c26bda9c92c9652803e96e934695c5a94c3da763 100644 (file)
@@ -3,6 +3,7 @@ package ru.deadsoftware.cavedroid.game.ui.windows.inventory
 import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
 
 abstract class AbstractInventoryWindow {
 
@@ -12,17 +13,27 @@ abstract class AbstractInventoryWindow {
 
     var selectItemPointer: Int = -1
 
-    fun onLeftCLick(items: MutableList<InventoryItem?>, gameItemsHolder: GameItemsHolder, index: Int, pointer: Int = -1) {
-        if (selectedItem != null && selectedItem?.item?.isNone() != true && pointer >= 0 && selectItemPointer >= 0 && pointer != selectItemPointer) {
+    fun onLeftCLick(
+        items: MutableList<InventoryItem>,
+        gameItemsHolder: GameItemsHolder,
+        index: Int,
+        pointer: Int = -1
+    ) {
+        if (selectedItem != null &&
+            selectedItem?.item?.isNone() != true &&
+            pointer >= 0 && selectItemPointer >= 0 &&
+            pointer != selectItemPointer
+        ) {
             return
         }
 
         val clickedItem = items[index]
 
         selectedItem?.let { selectedItem ->
-            if (clickedItem != null && items[index]!!.item == selectedItem.item &&
-                items[index]!!.amount + selectedItem.amount <= selectedItem.item.params.maxStack) {
-                items[index]!!.amount += selectedItem.amount
+            if (!clickedItem.isNoneOrNull() && items[index].item == selectedItem.item &&
+                items[index].amount + selectedItem.amount <= selectedItem.item.params.maxStack
+            ) {
+                items[index].amount += selectedItem.amount
                 this@AbstractInventoryWindow.selectedItem = null
                 selectItemPointer = -1
                 return
@@ -35,15 +46,20 @@ abstract class AbstractInventoryWindow {
         selectItemPointer = pointer
     }
 
-    fun onRightClick(items: MutableList<InventoryItem?>, index: Int) {
+    fun onRightClick(items: MutableList<InventoryItem>, index: Int) {
         val clickedItem = items[index]
         val selectedItem = selectedItem
-            ?.takeIf { clickedItem == null || clickedItem.item.isNone() || it.item == items[index]!!.item && items[index]!!.amount + 1 < it.item.params.maxStack }
+            ?.takeIf {
+                !clickedItem.isNoneOrNull() || clickedItem.item.isNone() ||
+                        it.item == items[index].item && items[index].amount + 1 < it.item.params.maxStack
+            }
             ?: return
 
-        val newItem = selectedItem.item.toInventoryItem((clickedItem?.takeIf { !it.item.isNone() }?.amount ?: 0) + 1)
+        val newItem = selectedItem.item.toInventoryItem(
+            (clickedItem.takeIf { !it.item.isNone() }?.amount ?: 0) + 1
+        )
         items[index] = newItem
-        selectedItem.amount --
+        selectedItem.amount--
 
         if (selectedItem.amount <= 0) {
             this@AbstractInventoryWindow.selectedItem = null
diff --git a/core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/AbstractInventoryWindowWithCraftGrid.kt b/core/src/ru/deadsoftware/cavedroid/game/ui/windows/inventory/AbstractInventoryWindowWithCraftGrid.kt
new file mode 100644 (file)
index 0000000..066ca37
--- /dev/null
@@ -0,0 +1,24 @@
+package ru.deadsoftware.cavedroid.game.ui.windows.inventory
+
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
+import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
+
+abstract class AbstractInventoryWindowWithCraftGrid(
+    gameItemsHolder: GameItemsHolder,
+) : AbstractInventoryWindow() {
+
+    private val _items = Array(10) { gameItemsHolder.fallbackItem.toInventoryItem() }
+
+    val items get() = _items.asList()
+
+    val craftingItems get() = items.subList(0, 9) as MutableList<InventoryItem>
+
+    val craftResultList get() = items.subList(9, 10) as MutableList<InventoryItem>
+
+    var craftResult: InventoryItem
+        get() = craftResultList[0]
+        set(value) {
+            craftResultList[0] = value
+        }
+
+}
\ No newline at end of file
index d83f1d5d8104aab552d500088bdb3f732af0c44b..cd7c0ffcf62e3961b339e232e506ce5f61d63715 100644 (file)
@@ -1,15 +1,14 @@
 package ru.deadsoftware.cavedroid.game.ui.windows.inventory
 
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
 
-class CraftingInventoryWindow : AbstractInventoryWindow() {
+class CraftingInventoryWindow(
+    gameItemsHolder: GameItemsHolder
+) : AbstractInventoryWindowWithCraftGrid(gameItemsHolder) {
 
     override val type = GameUiWindow.CRAFTING_TABLE
 
     override var selectedItem: InventoryItem? = null
-
-    val craftingItems = MutableList<InventoryItem?>(9) { null }
-
-    var craftResult: InventoryItem? = null
 }
\ No newline at end of file
index 12026d894bc4cec749b6068346858ecc761b3944..ce250fc9e3a16600bb7c782aa6f4147d3f803840 100644 (file)
@@ -1,15 +1,14 @@
 package ru.deadsoftware.cavedroid.game.ui.windows.inventory
 
+import ru.deadsoftware.cavedroid.game.GameItemsHolder
 import ru.deadsoftware.cavedroid.game.GameUiWindow
 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
 
-class SurvivalInventoryWindow() : AbstractInventoryWindow() {
+class SurvivalInventoryWindow(
+    gameItemsHolder: GameItemsHolder
+) : AbstractInventoryWindowWithCraftGrid(gameItemsHolder) {
 
     override val type = GameUiWindow.SURVIVAL_INVENTORY
 
     override var selectedItem: InventoryItem? = null
-
-    val craftingItems = MutableList<InventoryItem?>(9) { null }
-
-    var craftResult: InventoryItem? = null
 }
\ No newline at end of file