DEADSOFTWARE

839135e425d7ab829ad55087ceec31982df81323
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.Gdx
4 import com.badlogic.gdx.utils.TimeUtils
5 import ru.deadsoftware.cavedroid.game.GameItemsHolder
6 import ru.deadsoftware.cavedroid.game.GameScope
7 import ru.deadsoftware.cavedroid.game.GameUiWindow
8 import ru.deadsoftware.cavedroid.game.input.IGameInputHandler
9 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
10 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
11 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
12 import ru.deadsoftware.cavedroid.game.mobs.MobsController
13 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem
14 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
15 import ru.deadsoftware.cavedroid.game.objects.drop.DropController
16 import ru.deadsoftware.cavedroid.game.objects.furnace.Furnace
17 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
18 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
19 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.FurnaceInventoryWindow
20 import ru.deadsoftware.cavedroid.misc.Assets
21 import javax.inject.Inject
23 @GameScope
24 class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
25 private val gameWindowsManager: GameWindowsManager,
26 private val mobsController: MobsController,
27 private val gameItemsHolder: GameItemsHolder,
28 private val dropController: DropController,
29 ) : IGameInputHandler<MouseInputAction> {
31 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
33 override fun checkConditions(action: MouseInputAction): Boolean {
34 return gameWindowsManager.getCurrentWindow() == GameUiWindow.FURNACE &&
35 isInsideWindow(action, survivalWindowTexture) &&
36 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Screen)
37 && (action.actionKey.touchUp || action.actionKey is MouseInputActionKey.Screen)
38 }
40 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
41 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
43 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Furnace.itemsInRow))
44 itemIndex += GameWindowsConfigs.Furnace.hotbarCells
46 if (itemIndex >= mobsController.player.inventory.size) {
47 itemIndex -= mobsController.player.inventory.size
48 }
50 if (action.actionKey is MouseInputActionKey.Screen) {
51 if (!action.actionKey.touchUp) {
52 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
53 } else {
54 if (action.actionKey.pointer == window.selectItemPointer) {
55 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex, action.actionKey.pointer)
56 } else {
57 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
58 }
59 }
60 } else if (action.actionKey is MouseInputActionKey.Left) {
61 window.onLeftCLick(mobsController.player.inventory.items as MutableList<InventoryItem?>, gameItemsHolder, itemIndex)
62 } else {
63 window.onRightClick(mobsController.player.inventory.items as MutableList<InventoryItem?>, itemIndex)
64 }
66 Gdx.app.debug(
67 TAG,
68 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
69 )
70 }
72 private fun handleInsideFuel(action: MouseInputAction) {
73 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
75 if (!window.selectedItem.isNoneOrNull() && window.selectedItem?.item?.params?.burningTimeMs == null) {
76 return
77 }
79 if (action.actionKey is MouseInputActionKey.Screen) {
80 if (!action.actionKey.touchUp) {
81 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
82 } else {
83 if (action.actionKey.pointer == window.selectItemPointer) {
84 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.FUEL_INDEX, action.actionKey.pointer)
85 } else {
86 window.onRightClick(window.furnace.items, Furnace.FUEL_INDEX)
87 }
88 }
89 } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
90 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.FUEL_INDEX)
91 } else {
92 window.onRightClick(window.furnace.items, Furnace.FUEL_INDEX)
93 }
94 }
96 private fun handleInsideInput(action: MouseInputAction) {
97 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
99 if (action.actionKey is MouseInputActionKey.Screen) {
100 if (!action.actionKey.touchUp) {
101 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
102 } else {
103 if (action.actionKey.pointer == window.selectItemPointer) {
104 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.INPUT_INDEX, action.actionKey.pointer)
105 } else {
106 window.onRightClick(window.furnace.items, Furnace.INPUT_INDEX)
109 } else if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) {
110 window.onLeftCLick(window.furnace.items, gameItemsHolder, Furnace.INPUT_INDEX)
111 } else {
112 window.onRightClick(window.furnace.items, Furnace.INPUT_INDEX)
116 override fun handle(action: MouseInputAction) {
117 val survivalTexture = survivalWindowTexture
118 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
120 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
121 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
123 val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
124 GameWindowsConfigs.Furnace.itemsGridColWidth
125 val yOnGrid = (yOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginTop) /
126 GameWindowsConfigs.Furnace.itemsGridRowHeight
128 val isInsideInput = xOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginLeft &&
129 xOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
130 yOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginTop &&
131 yOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
133 val isInsideFuel = xOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginLeft &&
134 xOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
135 yOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginTop &&
136 yOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
138 val isInsideResult = xOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetX &&
139 xOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetX + GameWindowsConfigs.Furnace.itemsGridColWidth &&
140 yOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetY &&
141 yOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetY + GameWindowsConfigs.Furnace.itemsGridRowHeight
143 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Furnace.itemsInRow &&
144 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Furnace.itemsInCol
146 if (isInsideInventoryGrid) {
147 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
148 } else if (isInsideFuel) {
149 handleInsideFuel(action)
150 } else if (isInsideInput) {
151 handleInsideInput(action)
152 } else if (isInsideResult && action.actionKey.touchUp) {
153 val selectedItem = window.selectedItem
154 if (selectedItem == null || selectedItem.item.isNone() ||
155 (selectedItem.item == window.furnace.result?.item && selectedItem.amount + (window.furnace.result?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
157 if (selectedItem != null && !selectedItem.item.isNone()) {
158 selectedItem.amount += (window.furnace.result?.amount ?: 0)
159 } else {
160 window.selectedItem = window.furnace.result
162 window.furnace.items[Furnace.RESULT_INDEX] = null
168 companion object {
169 private const val TAG = "SelectFurnaceInventoryItemMouseInputHandler"