DEADSOFTWARE

Fix crash in craft
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectChestInventoryItemMouseInputHandler.kt
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.ChestInventoryWindow
11 import ru.deadsoftware.cavedroid.misc.Assets
12 import javax.inject.Inject
14 @GameScope
15 class SelectChestInventoryItemMouseInputHandler @Inject constructor(
16 private val gameWindowsManager: GameWindowsManager,
17 private val mobsController: MobsController,
18 private val gameItemsHolder: GameItemsHolder,
19 ) : AbstractInventoryItemsMouseInputHandler(gameItemsHolder, gameWindowsManager, GameUiWindow.CHEST) {
21 override val windowTexture get() = requireNotNull(Assets.textureRegions["chest"])
23 private fun handleInsideContentGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
24 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
25 val itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.contentsInRow
27 handleInsidePlaceableCell(action, window.chest.items, window, itemIndex)
28 }
30 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
31 val window = gameWindowsManager.currentWindow as ChestInventoryWindow
33 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Chest.itemsInRow
34 itemIndex += GameWindowsConfigs.Chest.hotbarCells
36 if (itemIndex >= mobsController.player.inventory.size) {
37 itemIndex -= mobsController.player.inventory.size
38 }
40 handleInsidePlaceableCell(action, mobsController.player.inventory.items, window, itemIndex)
41 }
43 override fun handle(action: MouseInputAction) {
44 val texture = windowTexture
46 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - texture.regionWidth / 2)
47 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - texture.regionHeight / 2)
49 val xOnGrid = (xOnWindow - GameWindowsConfigs.Chest.itemsGridMarginLeft) /
50 GameWindowsConfigs.Chest.itemsGridColWidth
51 val yOnGrid = (yOnWindow - GameWindowsConfigs.Chest.itemsGridMarginTop) /
52 GameWindowsConfigs.Chest.itemsGridRowHeight
54 val xOnContent = (xOnWindow - GameWindowsConfigs.Chest.contentsMarginLeft) /
55 GameWindowsConfigs.Chest.itemsGridColWidth
56 val yOnContent = (yOnWindow - GameWindowsConfigs.Chest.contentsMarginTop) /
57 GameWindowsConfigs.Chest.itemsGridRowHeight
59 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Chest.itemsInRow &&
60 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Chest.itemsInCol
62 val isInsideContentGrid = xOnContent >= 0 && xOnContent < GameWindowsConfigs.Chest.contentsInRow &&
63 yOnContent >= 0 && yOnContent < GameWindowsConfigs.Chest.contentsInCol
66 if (isInsideInventoryGrid) {
67 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
68 } else if (isInsideContentGrid) {
69 handleInsideContentGrid(action, xOnContent.toInt(), yOnContent.toInt())
70 }
71 }
72 }