DEADSOFTWARE

ab65fb8c05d888d644bb63fcc618b46b453588d6
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.Gdx
4 import ru.deadsoftware.cavedroid.game.GameItemsHolder
5 import ru.deadsoftware.cavedroid.game.GameScope
6 import ru.deadsoftware.cavedroid.game.GameUiWindow
7 import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
8 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
9 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
10 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
11 import ru.deadsoftware.cavedroid.game.mobs.MobsController
12 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
13 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
14 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
15 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
16 import ru.deadsoftware.cavedroid.misc.Assets
17 import javax.inject.Inject
19 @GameScope
20 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
21 private val gameWindowsManager: GameWindowsManager,
22 private val mobsController: MobsController,
23 private val gameItemsHolder: GameItemsHolder,
24 ) : IGameInputHandler<MouseInputAction> {
26 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
28 override fun checkConditions(action: MouseInputAction): Boolean {
29 return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
30 isInsideWindow(action, survivalWindowTexture) &&
31 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Touch)
32 && action.actionKey.touchUp
33 }
35 private fun onLeftCLick(items: MutableList<InventoryItem?>, window: SurvivalInventoryWindow, index: Int) {
36 val selectedItem = window.selectedItem
37 val clickedItem = items[index]
39 if (clickedItem != null && selectedItem != null && items[index]!!.item == selectedItem.item &&
40 items[index]!!.amount + selectedItem.amount <= selectedItem.item.params.maxStack) {
41 items[index]!!.amount += selectedItem.amount
42 window.selectedItem = null
43 return
44 }
46 val item = items[index]
47 items[index] = selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem()
48 window.selectedItem = item
49 }
51 private fun onRightClick(items: MutableList<InventoryItem?>, window: SurvivalInventoryWindow, index: Int) {
52 val clickedItem = items[index]
53 val selectedItem = window.selectedItem
54 ?.takeIf { clickedItem == null || clickedItem.item.isNone() || it.item == items[index]!!.item && items[index]!!.amount + 1 < it.item.params.maxStack }
55 ?: return
57 val newItem = selectedItem.item.toInventoryItem((clickedItem?.takeIf { !it.item.isNone() }?.amount ?: 0) + 1)
58 items[index] = newItem
59 selectedItem.amount --
61 if (selectedItem.amount <= 0) {
62 window.selectedItem = null
63 }
64 }
66 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
67 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
69 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
70 itemIndex += GameWindowsConfigs.Survival.hotbarCells
72 if (itemIndex >= 36) {
73 itemIndex -= 36
74 }
76 if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
77 onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
78 } else {
79 onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, window, itemIndex)
80 }
82 Gdx.app.debug(
83 TAG,
84 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
85 )
86 }
88 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
89 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
90 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
92 if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
93 onLeftCLick(window.craftingItems, window, index)
94 } else {
95 onRightClick(window.craftingItems, window, index)
96 }
98 window.craftResult =
99 gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
102 override fun handle(action: MouseInputAction) {
103 val survivalTexture = survivalWindowTexture
104 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
106 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
107 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
109 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
110 GameWindowsConfigs.Survival.itemsGridColWidth
111 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
112 GameWindowsConfigs.Survival.itemsGridRowHeight
114 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
115 GameWindowsConfigs.Survival.itemsGridColWidth
116 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
117 GameWindowsConfigs.Survival.itemsGridRowHeight
119 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
120 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
122 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
123 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
125 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
126 xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
127 yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
128 yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
130 if (isInsideInventoryGrid) {
131 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
132 } else if (isInsideCraftGrid) {
133 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
134 } else if (isInsideCraftResult) {
135 val selectedItem = window.selectedItem
136 if (selectedItem == null || selectedItem.item.isNone() ||
137 (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
138 for (i in window.craftingItems.indices) {
139 if ((window.craftingItems[i]?.amount ?: 0) > 1) {
140 window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
141 } else {
142 window.craftingItems[i] = null
145 if (selectedItem != null && !selectedItem.item.isNone()) {
146 selectedItem.amount += (window.craftResult?.amount ?: 0)
147 } else {
148 window.selectedItem = window.craftResult
150 window.craftResult = gameItemsHolder.craftItem(window.craftingItems
151 .map { it?.item ?: gameItemsHolder.fallbackItem })
157 companion object {
158 private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"