DEADSOFTWARE

d227ae350a72477eb3ce94e2a6ccb4699280dfdc
[cavedroid.git] /
1 package ru.fredboy.cavedroid.ux.controls.input.handler.mouse
2
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
14
15 @GameScope
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) {
23
24 override val windowTexture get() = requireNotNull(textureRegions["survival"])
25
26 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
27 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
28
29 var itemIndex = xOnGrid + yOnGrid * GameWindowsConfigs.Survival.itemsInRow
30 itemIndex += GameWindowsConfigs.Survival.hotbarCells
31
32 if (itemIndex >= mobController.player.inventory.size) {
33 itemIndex -= mobController.player.inventory.size
34 }
35
36 handleInsidePlaceableCell(action, mobController.player.inventory.items, window, itemIndex)
37 }
38
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!!
42
43 handleInsidePlaceableCell(action, window.craftingItems, window, index)
44
45 updateCraftResult(window)
46 }
47
48 private fun handleInsideCraftResult(action: MouseInputAction) {
49 val window = gameWindowsManager.currentWindow as SurvivalInventoryWindow
50
51 handleInsideCraftResultCell(action, window.craftResultList, window, 0)
52
53 updateCraftResult(window)
54 }
55
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)
59
60 val xOnGrid = (xOnWindow - GameWindowsConfigs.Survival.itemsGridMarginLeft) /
61 GameWindowsConfigs.Survival.itemsGridColWidth
62 val yOnGrid = (yOnWindow - GameWindowsConfigs.Survival.itemsGridMarginTop) /
63 GameWindowsConfigs.Survival.itemsGridRowHeight
64
65 val xOnCraft = (xOnWindow - GameWindowsConfigs.Survival.craftOffsetX) /
66 GameWindowsConfigs.Survival.itemsGridColWidth
67 val yOnCraft = (yOnWindow - GameWindowsConfigs.Survival.craftOffsetY) /
68 GameWindowsConfigs.Survival.itemsGridRowHeight
69
70 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Survival.itemsInRow &&
71 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Survival.itemsInCol
72
73 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Survival.craftGridSize &&
74 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Survival.craftGridSize
75
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
80
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)
87 }
88
89 }
90 }