0a455ab2c70c47e53824d4225a0d395078c0809e
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.ui.windows.GameWindowsConfigs
9 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
10 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
11 import ru.deadsoftware.cavedroid.misc.Assets
12 import javax.inject.Inject
14 @GameScope
15 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
16 private val gameWindowsManager: GameWindowsManager,
17 private val mobsController: MobsController,
18 private val gameItemsHolder: GameItemsHolder,
19 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.SURVIVAL_INVENTORY) {
21 override val windowTexture get() = requireNotNull(Assets.textureRegions["survival"])
23 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
24 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
26 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
27 itemIndex += GameWindowsConfigs.Survival.hotbarCells
29 if (itemIndex >= mobsController.player.inventory.size) {
30 itemIndex -= mobsController.player.inventory.size
31 }
33 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
34 }
36 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
37 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
38 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
40 handleInsidePlaceableCell(action, window.craftingItems, window, index)
42 updateCraftResult(window)
43 }
45 private fun handleInsideCraftResult(action: MouseInputAction) {
46 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
48 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
50 updateCraftResult(window)
51 }
53 override fun handle(action: MouseInputAction) {
54 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - windowTexture.regionWidth / 2)
55 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - windowTexture.regionHeight / 2)
57 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
58 GameWindowsConfigs.Survival.itemsGridColWidth
59 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
60 GameWindowsConfigs.Survival.itemsGridRowHeight
62 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
63 GameWindowsConfigs.Survival.itemsGridColWidth
64 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
65 GameWindowsConfigs.Survival.itemsGridRowHeight
67 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
68 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
70 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
71 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
73 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
74 xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
75 yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
76 yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
78 if (isInsideInventoryGrid) {
79 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
80 } else if (isInsideCraftGrid) {
81 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
82 } else if (isInsideCraftResult) {
83 handleInsideCraftResult(action)
84 }
86 }
87 }