X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavedroid%2Fgame%2Finput%2Fhandler%2Fmouse%2FSelectCraftingInventoryItemMouseInputHandler.kt;h=88e362005316a756b7e0aaf829493fe20d5a3747;hb=b2f824726f647543e69f31ac5723df37750b5778;hp=489815029f5d72da169c5a62c52a256dca6e7990;hpb=6b49d51c59f6d639d949d1e9c264f7f144a305ab;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt index 4898150..88e3620 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/SelectCraftingInventoryItemMouseInputHandler.kt @@ -1,15 +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.ui.windows.GameWindowsConfigs import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager import ru.deadsoftware.cavedroid.game.ui.windows.inventory.CraftingInventoryWindow @@ -21,90 +16,45 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor( private val gameWindowsManager: GameWindowsManager, private val mobsController: MobsController, private val gameItemsHolder: GameItemsHolder, -) : IGameInputHandler { +) : 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 - } - - private fun onLeftCLick(items: MutableList, window: CraftingInventoryWindow, index: Int) { - val selectedItem = window.selectedItem - val clickedItem = items[index] - - if (clickedItem != null && selectedItem != null && items[index]!!.item == selectedItem.item && - items[index]!!.amount + selectedItem.amount <= selectedItem.item.params.maxStack) { - items[index]!!.amount += selectedItem.amount - window.selectedItem = null - return - } - - val item = items[index] - items[index] = selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem() - window.selectedItem = item - } - - private fun onRightClick(items: MutableList, window: CraftingInventoryWindow, index: Int) { - val clickedItem = items[index] - val selectedItem = window.selectedItem - ?.takeIf { clickedItem == null || 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) - items[index] = newItem - selectedItem.amount -- - - if (selectedItem.amount <= 0) { - window.selectedItem = null - } - } + 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 >= 36) { - itemIndex -= 36 + if (itemIndex >= mobsController.player.inventory.size) { + itemIndex -= mobsController.player.inventory.size } - if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) { - onLeftCLick(mobsController.player.inventory.items as MutableList, window, itemIndex) - } else { - onRightClick(mobsController.player.inventory.items as MutableList, window, 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.Left || action.actionKey is MouseInputActionKey.Screen) { - onLeftCLick(window.craftingItems, window, index) - } else { - onRightClick(window.craftingItems, window, 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 @@ -132,30 +82,8 @@ class SelectCraftingInventoryItemMouseInputHandler @Inject constructor( } else if (isInsideCraftGrid) { handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt()) } else if (isInsideCraftResult) { - 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 }) - } + handleInsideCraftResult(action) } } - - companion object { - private const val TAG = "SelectCraftingInventoryItemMouseInputHandler" - - } } \ No newline at end of file