9f25b9adc9b59942f3622b3e74e6f503921f534d
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.MouseInputHandler
7 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
8 import ru.deadsoftware.cavedroid.game.mobs.MobsController
9 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
10 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager
11 import ru.deadsoftware.cavedroid.game.ui.windows.inventory.SurvivalInventoryWindow
12 import ru.deadsoftware.cavedroid.misc.Assets
13 import javax.inject.Inject
15 @GameScope
16 @MouseInputHandler
17 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val mobsController: MobsController,
20 private val gameItemsHolder: GameItemsHolder,
21 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.SURVIVAL_INVENTORY) {
23 override val windowTexture get() = requireNotNull(Assets.textureRegions["survival"])
25 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
26 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
28 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
29 itemIndex += GameWindowsConfigs.Survival.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 handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
39 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
40 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
42 handleInsidePlaceableCell(action, window.craftingItems, window, index)
44 updateCraftResult(window)
45 }
47 private fun handleInsideCraftResult(action: MouseInputAction) {
48 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
50 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
52 updateCraftResult(window)
53 }
55 override fun handle(action: MouseInputAction) {
56 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - windowTexture.regionWidth / 2)
57 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - windowTexture.regionHeight / 2)
59 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
60 GameWindowsConfigs.Survival.itemsGridColWidth
61 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
62 GameWindowsConfigs.Survival.itemsGridRowHeight
64 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
65 GameWindowsConfigs.Survival.itemsGridColWidth
66 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
67 GameWindowsConfigs.Survival.itemsGridRowHeight
69 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
70 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
72 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
73 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
75 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
76 xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
77 yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
78 yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
80 if (isInsideInventoryGrid) {
81 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
82 } else if (isInsideCraftGrid) {
83 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
84 } else if (isInsideCraftResult) {
85 handleInsideCraftResult(action)
86 }
88 }
89 }