559ccfc79388a09157838d3238f0326709764f24
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import com.badlogic.gdx.Gdx
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.IGameInputHandler
8 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
9 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
10 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
11 import ru.deadsoftware.cavedroid.game.mobs.MobsController
12 import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs
13 import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
14 import ru.deadsoftware.cavedroid.misc.Assets
15 import javax.inject.Inject
17 @GameScope
18 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
19 private val gameWindowsManager: GameWindowsManager,
20 private val mobsController: MobsController,
21 private val gameItemsHolder: GameItemsHolder,
22 ) : IGameInputHandler<MouseInputAction> {
24 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
26 override fun checkConditions(action: MouseInputAction): Boolean {
27 return gameWindowsManager.getCurrentWindow() == GameUiWindow.SURVIVAL_INVENTORY &&
28 isInsideWindow(action, survivalWindowTexture) &&
29 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch)
30 && action.actionKey.touchUp
31 }
33 override fun handle(action: MouseInputAction) {
34 val survivalTexture = survivalWindowTexture
35 val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2 +
36 GameWindowsConfigs.Survival.itemsGridMarginLeft)) /
37 GameWindowsConfigs.Survival.itemsGridColWidth
38 val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2 +
39 GameWindowsConfigs.Survival.itemsGridMarginTop)) /
40 GameWindowsConfigs.Survival.itemsGridRowHeight
42 if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Survival.itemsInRow ||
43 yOnGrid < 0 || yOnGrid > GameWindowsConfigs.Survival.itemsInCol) {
44 return
45 }
47 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Survival.itemsInRow))
48 itemIndex += GameWindowsConfigs.Survival.hotbarCells
50 if (itemIndex >= mobsController.player.inventory.size) {
51 itemIndex -= mobsController.player.inventory.size
52 }
54 val item = mobsController.player.inventory[itemIndex]
55 mobsController.player.inventory[itemIndex] = gameWindowsManager.selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem()
56 gameWindowsManager.selectedItem = item
58 Gdx.app.debug(TAG, "selected item: ${gameWindowsManager.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)")
59 }
61 companion object {
62 private const val TAG = "SelectSurvivalInventoryItemMouseInputHandler"
64 }
65 }