DEADSOFTWARE

eac8cfa2d54b88c8a58d713841dff5ee8934c91c
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindMouseInputHandler
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.action.MouseInputAction
8 import ru.deadsoftware.cavedroid.game.mobs.MobsController
9 import ru.deadsoftware.cavedroid.game.model.item.InventoryItem.Companion.isNoneOrNull
10 import ru.deadsoftware.cavedroid.game.objects.container.Furnace
11 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
12 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
13 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.FurnaceInventoryWindow
14 import ru.deadsoftware.cavedroid.misc.Assets
15 import javax.inject.Inject
17 @GameScope
18 @BindMouseInputHandler
19 class SelectFurnaceInventoryItemMouseInputHandler @Inject constructor(
20 private val gameWindowsManager: GameWindowsManager,
21 private val mobsController: MobsController,
22 private val gameItemsHolder: GameItemsHolder,
23 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.FURNACE) {
25 override val windowTexture get() = requireNotNull(Assets.textureRegions["furnace"])
27 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
28 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
30 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Furnace.itemsInRow
31 itemIndex += GameWindowsConfigs.Furnace.hotbarCells
33 if (itemIndex >= mobsController.player.inventory.size) {
34 itemIndex -= mobsController.player.inventory.size
35 }
37 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
38 }
40 private fun handleInsideFuel(action: MouseInputAction) {
41 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
43 if (!window.selectedItem.isNoneOrNull() && window.selectedItem?.item?.params?.burningTimeMs == null) {
44 return
45 }
47 handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.FUEL_INDEX)
48 }
50 private fun handleInsideInput(action: MouseInputAction) {
51 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
53 handleInsidePlaceableCell(action, window.furnace.items, window, Furnace.INPUT_INDEX)
54 }
56 private fun handleInsideResult(action: MouseInputAction) {
57 val window = gameWindowsManager.currentWindow as FurnaceInventoryWindow
59 handleInsideCraftResultCell(action, window.furnace.items, window, Furnace.RESULT_INDEX)
60 }
62 override fun handle(action: MouseInputAction) {
63 val texture = windowTexture
65 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
66 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
68 val xOnGrid = (xOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginLeft) /
69 GameWindowsConfigs.Furnace.itemsGridColWidth
70 val yOnGrid = (yOnWindow - GameWindowsConfigs.Furnace.itemsGridMarginTop) /
71 GameWindowsConfigs.Furnace.itemsGridRowHeight
73 val isInsideInput = xOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginLeft &&
74 xOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
75 yOnWindow > GameWindowsConfigs.Furnace.smeltInputMarginTop &&
76 yOnWindow < GameWindowsConfigs.Furnace.smeltInputMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
78 val isInsideFuel = xOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginLeft &&
79 xOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginLeft + GameWindowsConfigs.Furnace.itemsGridColWidth &&
80 yOnWindow > GameWindowsConfigs.Furnace.smeltFuelMarginTop &&
81 yOnWindow < GameWindowsConfigs.Furnace.smeltFuelMarginTop + GameWindowsConfigs.Furnace.itemsGridRowHeight
83 val isInsideResult = xOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetX &&
84 xOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetX + GameWindowsConfigs.Furnace.itemsGridColWidth &&
85 yOnWindow > GameWindowsConfigs.Furnace.smeltResultOffsetY &&
86 yOnWindow < GameWindowsConfigs.Furnace.smeltResultOffsetY + GameWindowsConfigs.Furnace.itemsGridRowHeight
88 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Furnace.itemsInRow &&
89 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Furnace.itemsInCol
91 if (isInsideInventoryGrid) {
92 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
93 } else if (isInsideFuel) {
94 handleInsideFuel(action)
95 } else if (isInsideInput) {
96 handleInsideInput(action)
97 } else if (isInsideResult) {
98 handleInsideResult(action)
99 }