[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectSurvivalInventoryItemMouseInputHandler.kt
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
index 0d0a64771f6cf6f7097d2cd54316ff850075b448..d5858ac0d70822b6f1aa2a317669273c75f4e203 100644 (file)
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.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
private val gameWindowsManager: GameWindowsManager,
private val mobsController: MobsController,
private val gameItemsHolder: GameItemsHolder,
+ private val dropController: DropController,
) : IGameInputHandler<MouseInputAction> {
private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
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
- }
-
- private fun onLeftCLick(items: MutableList<InventoryItem?>, window: SurvivalInventoryWindow, 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<InventoryItem?>, window: SurvivalInventoryWindow, 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
- }
+ && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
}
private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
itemIndex += GameWindowsConfigs.Survival.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<InventoryItem?>, window, itemIndex)
+ 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 {
- onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
+ window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
}
Gdx.app.debug(
val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
- if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
- onLeftCLick(window.craftingItems, window, index)
+ 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 {
- onRightClick(window.craftingItems, window, index)
+ window.onRightClick(window.craftingItems, index)
}
window.craftResult =
handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
} else if (isInsideCraftGrid) {
handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
- } else if (isInsideCraftResult) {
+ } 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)) {
window.craftResult = gameItemsHolder.craftItem(window.craftingItems
.map { it?.item ?: gameItemsHolder.fallbackItem })
}
+ } else if (action.actionKey.touchUp) {
+ window.selectedItem?.let { selectedItem ->
+ dropController.addDrop(
+ /* x = */ mobsController.player.x + (32f * mobsController.player.direction.basis),
+ /* y = */ mobsController.player.y,
+ /* item = */ selectedItem.item,
+ /* count = */ selectedItem.amount,
+ )
+ window.selectedItem = null
+ }
}
}