DEADSOFTWARE

f4a00eb9b18d22ed512557393bde5ba1e1342790
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import ru.deadsoftware.cavedroid.game.GameItemsHolder
4 import ru.deadsoftware.cavedroid.game.GameScope
5 import ru.deadsoftware.cavedroid.game.GameUiWindow
6 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
7 import ru.deadsoftware.cavedroid.game.mobs.MobsController
8 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
9 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
10 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
11 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
12 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.FurnaceInventoryWindow
13 import ru.deadsoftware.cavedroid.misc.Assets
14 import javax.inject.Inject
16 @GameScope
17 class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val mobsController: MobsController,
20 private val gameItemsHolder: GameItemsHolder,
21 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.FURNACE) {
23 override val windowTexture get() = requireNotNull(Assets.textureRegions["furnace"])
25 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
26 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
28 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Furnace.itemsInRow
29 itemIndex += GameWindowsConfigs.Furnace.hotbarCells
31 if (itemIndex >= mobsController.player.inventory.size) {
32 itemIndex -= mobsController.player.inventory.size
33 }
35 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
36 }
38 private fun handleInsideFuel(action: MouseInputAction) {
39 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
41 if (!window.selectedItem.isNoneOrNull() && window.selectedItem?.item?.params?.burningTimeMs == null) {
42 return
43 }
45 handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.FUEL_INDEX)
46 }
48 private fun handleInsideInput(action: MouseInputAction) {
49 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
51 handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.INPUT_INDEX)
52 }
54 private fun handleInsideResult(action: MouseInputAction) {
55 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
57 handleInsideCraftResultCell(action, window.furnace.items, window, Furnace.RESULT_INDEX)
58 }
60 override fun handle(action: MouseInputAction) {
61 val texture = windowTexture
63 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
64 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
66 val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
67 GameWindowsConfigs.Furnace.itemsGridColWidth
68 val yOnGrid = (yOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginTop) /
69 GameWindowsConfigs.Furnace.itemsGridRowHeight
71 val isInsideInput = xOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginLeft &&
72 xOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
73 yOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginTop &&
74 yOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
76 val isInsideFuel = xOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginLeft &&
77 xOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
78 yOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginTop &&
79 yOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
81 val isInsideResult = xOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetX &&
82 xOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetX + GameWindowsConfigs.Furnace.itemsGridColWidth &&
83 yOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetY &&
84 yOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetY + GameWindowsConfigs.Furnace.itemsGridRowHeight
86 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Furnace.itemsInRow &&
87 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Furnace.itemsInCol
89 if (isInsideInventoryGrid) {
90 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
91 } else if (isInsideFuel) {
92 handleInsideFuel(action)
93 } else if (isInsideInput) {
94 handleInsideInput(action)
95 } else if (isInsideResult) {
96 handleInsideResult(action)
97 }
99 }