DEADSOFTWARE

cc993c99a5e81f3afcd1d761a0cbd57ad72873e2
[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.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.CraftingInventoryWindow
12 import ru.deadsoftware.cavedroid.misc.Assets
13 import javax.inject.Inject
15 @GameScope
16 @MouseInputHandler
17 class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val mobsController: MobsController,
20 private val gameItemsHolder: GameItemsHolder,
21 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CRAFTING_TABLE) {
23 override val windowTexture get() = requireNotNull(Assets.textureRegions["crafting_table"])
25 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
26 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
28 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Crafting.itemsInRow
29 itemIndex += GameWindowsConfigs.Crafting.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 CraftingInventoryWindow
40 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize
42 handleInsidePlaceableCell(action, window.craftingItems, window, index)
44 updateCraftResult(window)
45 }
47 private fun handleInsideCraftResult(action: MouseInputAction) {
48 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
50 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
52 updateCraftResult(window)
53 }
55 override fun handle(action: MouseInputAction) {
56 val texture = windowTexture
58 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
59 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
61 val xOnGrid = (xOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginLeft) /
62 GameWindowsConfigs.Crafting.itemsGridColWidth
63 val yOnGrid = (yOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginTop) /
64 GameWindowsConfigs.Crafting.itemsGridRowHeight
66 val xOnCraft = (xOnWindow - GameWindowsConfigs.Crafting.craftOffsetX) /
67 GameWindowsConfigs.Crafting.itemsGridColWidth
68 val yOnCraft = (yOnWindow - GameWindowsConfigs.Crafting.craftOffsetY) /
69 GameWindowsConfigs.Crafting.itemsGridRowHeight
71 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Crafting.itemsInRow &&
72 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Crafting.itemsInCol
74 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Crafting.craftGridSize &&
75 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Crafting.craftGridSize
77 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetX &&
78 xOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetX + GameWindowsConfigs.Crafting.itemsGridColWidth &&
79 yOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetY &&
80 yOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetY + GameWindowsConfigs.Crafting.itemsGridRowHeight
82 if (isInsideInventoryGrid) {
83 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
84 } else if (isInsideCraftGrid) {
85 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
86 } else if (isInsideCraftResult) {
87 handleInsideCraftResult(action)
88 }
90 }
91 }