1 package ru.fredboy.cavedroid.ux.controls.input.handler.mouse
3 import ru.fredboy.cavedroid.common.di.GameScope
4 import ru.fredboy.cavedroid.domain.assets.usecase.GetTextureRegionByNameUseCase
5 import ru.fredboy.cavedroid.domain.items.repository.ItemsRepository
6 import ru.fredboy.cavedroid.game.controller.mob.MobController
7 import ru.fredboy.cavedroid.game.window.GameWindowType
8 import ru.fredboy.cavedroid.game.window.GameWindowsConfigs
9 import ru.fredboy.cavedroid.game.window.GameWindowsManager
10 import ru.fredboy.cavedroid.game.window.inventory.SurvivalInventoryWindow
11 import ru.fredboy.cavedroid.ux.controls.input.action.MouseInputAction
12 import ru.fredboy.cavedroid.ux.controls.input.annotation.BindMouseInputHandler
13 import javax.inject.Inject
16 @BindMouseInputHandler
17 class SelectSurvivalInventoryItemMouseInputHandler @Inject constructor(
18 private val gameWindowsManager: GameWindowsManager,
19 private val mobController: MobController,
20 private val textureRegions: GetTextureRegionByNameUseCase,
21 private val itemsRepository: ItemsRepository,
22 ) : AbstractInventoryItemsMouseInputHandler(itemsRepository, gameWindowsManager, GameWindowType.SURVIVAL_INVENTORY) {
24 override val windowTexture get() = requireNotNull(textureRegions["survival"])
26 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
27 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
29 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
30 itemIndex += GameWindowsConfigs.Survival.hotbarCells
32 if (itemIndex >= mobController.player.inventory.size) {
33 itemIndex -= mobController.player.inventory.size
36 handleInsidePlaceableCell(action, mobController.player.inventory.items, window, itemIndex)
39 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
40 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
41 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize // this is crafting on purpose!!
43 handleInsidePlaceableCell(action, window.craftingItems, window, index)
45 updateCraftResult(window)
48 private fun handleInsideCraftResult(action: MouseInputAction) {
49 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
51 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
53 updateCraftResult(window)
56 override fun handle(action: MouseInputAction) {
57 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - windowTexture.regionWidth / 2)
58 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - windowTexture.regionHeight / 2)
60 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
61 GameWindowsConfigs.Survival.itemsGridColWidth
62 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
63 GameWindowsConfigs.Survival.itemsGridRowHeight
65 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
66 GameWindowsConfigs.Survival.itemsGridColWidth
67 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
68 GameWindowsConfigs.Survival.itemsGridRowHeight
70 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
71 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
73 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
74 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
76 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Survival.craftResultOffsetX &&
77 xOnWindow < GameWindowsConfigs.Survival.craftResultOffsetX + GameWindowsConfigs.Survival.itemsGridColWidth &&
78 yOnWindow > GameWindowsConfigs.Survival.craftResultOffsetY &&
79 yOnWindow < GameWindowsConfigs.Survival.craftResultOffsetY + GameWindowsConfigs.Survival.itemsGridRowHeight
81 if (isInsideInventoryGrid) {
82 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
83 } else if (isInsideCraftGrid) {
84 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
85 } else if (isInsideCraftResult) {
86 handleInsideCraftResult(action)