91ec3111998f2f7400ffb3576ec4b48843a3d9b0
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.objects.DropController
14 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
15 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
16 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
17 import ru.deadsoftware.cavedroid.misc.Assets
18 import javax.inject.Inject
20 @GameScope
21 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
22 private val gameWindowsManager: GameWindowsManager,
23 private val mobsController: MobsController,
24 private val gameItemsHolder: GameItemsHolder,
25 private val dropController: DropController,
26 ) : IGameInputHandler<MouseInputAction> {
28 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
30 override fun checkConditions(action: MouseInputAction): Boolean {
31 return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
32 isInsideWindow(action, survivalWindowTexture) &&
33 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
34 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
35 }
37 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
38 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
40 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
41 itemIndex += GameWindowsConfigs.Survival.hotbarCells
43 if (itemIndex >= mobsController.player.inventory.size) {
44 itemIndex -= mobsController.player.inventory.size
45 }
47 if (action.actionKey is MouseInputActionKey.Screen) {
48 if (!action.actionKey.touchUp) {
49 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
50 } else {
51 if (action.actionKey.pointer == window.selectItemPointer) {
52 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
53 } else {
54 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
55 }
56 }
57 } else if (action.actionKey is MouseInputActionKey.Left) {
58 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
59 } else {
60 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
61 }
63 Gdx.app.debug(
64 TAG,
65 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
66 )
67 }
69 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
70 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
71 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
73 if (action.actionKey is MouseInputActionKey.Screen) {
74 if (!action.actionKey.touchUp) {
75 window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
76 } else {
77 if (action.actionKey.pointer == window.selectItemPointer) {
78 window.onLeftCLick(window.craftingItems, gameItemsHolder, index, action.actionKey.pointer)
79 } else {
80 window.onRightClick(window.craftingItems, index)
81 }
82 }
83 } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
84 window.onLeftCLick(window.craftingItems, gameItemsHolder, index)
85 } else {
86 window.onRightClick(window.craftingItems, index)
87 }
89 window.craftResult =
90 gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
91 }
93 override fun handle(action: MouseInputAction) {
94 val survivalTexture = survivalWindowTexture
95 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
97 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
98 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
100 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
101 GameWindowsConfigs.Survival.itemsGridColWidth
102 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
103 GameWindowsConfigs.Survival.itemsGridRowHeight
105 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
106 GameWindowsConfigs.Survival.itemsGridColWidth
107 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
108 GameWindowsConfigs.Survival.itemsGridRowHeight
110 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
111 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
113 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
114 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
116 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
117 xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
118 yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
119 yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
121 if (isInsideInventoryGrid) {
122 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
123 } else if (isInsideCraftGrid) {
124 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
125 } else if (isInsideCraftResult && action.actionKey.touchUp) {
126 val selectedItem = window.selectedItem
127 if (selectedItem == null || selectedItem.item.isNone() ||
128 (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
129 for (i in window.craftingItems.indices) {
130 if ((window.craftingItems[i]?.amount ?: 0) > 1) {
131 window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
132 } else {
133 window.craftingItems[i] = null
134 }
135 }
136 if (selectedItem != null && !selectedItem.item.isNone()) {
137 selectedItem.amount += (window.craftResult?.amount ?: 0)
138 } else {
139 window.selectedItem = window.craftResult
140 }
141 window.craftResult = gameItemsHolder.craftItem(window.craftingItems
142 .map { it?.item ?: gameItemsHolder.fallbackItem })
143 }
144 }
146 }
148 companion object {
149 private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
151 }
152 }