DEADSOFTWARE

976f443c93ceff7a8fb20c7beffd33296607cd04
[cavedroid.git] /
1 package ru.deadsoftware.cavedroid.game.input.handler.mouse
3 import ru.deadsoftware.cavedroid.misc.annotations.multibinding.BindMouseInputHandler
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.ui.windows.GameWindowsManager
8 import ru.deadsoftware.cavedroid.game.input.IMouseInputHandler
9 import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction
10 import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey
11 import ru.deadsoftware.cavedroid.game.input.isInsideWindow
12 import ru.deadsoftware.cavedroid.game.mobs.MobsController
13 import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsConfigs
14 import ru.deadsoftware.cavedroid.misc.Assets
15 import javax.inject.Inject
17 @GameScope
18 @BindMouseInputHandler
19 class SelectCreativeInventoryItemMouseInputHandler @Inject constructor(
20 private val gameItemsHolder: GameItemsHolder,
21 private val gameWindowsManager: GameWindowsManager,
22 private val mobsController: MobsController,
23 ) : IMouseInputHandler {
25 private val creativeInventoryTexture get() = requireNotNull(Assets.textureRegions["creative"])
27 override fun checkConditions(action: MouseInputAction): Boolean {
28 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
29 !gameWindowsManager.isDragging &&
30 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Screen) &&
31 action.actionKey.touchUp && isInsideWindow(action, creativeInventoryTexture)
32 }
34 override fun handle(action: MouseInputAction) {
35 val creativeTexture = creativeInventoryTexture
36 val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - creativeTexture.regionWidth / 2 +
37 GameWindowsConfigs.Creative.itemsGridMarginLeft)) /
38 GameWindowsConfigs.Creative.itemsGridColWidth
39 val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - creativeTexture.regionHeight / 2 +
40 GameWindowsConfigs.Creative.itemsGridMarginTop)) /
41 GameWindowsConfigs.Creative.itemsGridRowHeight
43 if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Creative.itemsInRow ||
44 yOnGrid < 0 || yOnGrid >= GameWindowsConfigs.Creative.itemsInCol) {
45 return
46 }
48 val itemIndex = (gameWindowsManager.creativeScrollAmount * GameWindowsConfigs.Creative.itemsInRow +
49 (xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Creative.itemsInRow))
50 val item = gameItemsHolder.getItemFromCreativeInventory(itemIndex)
51 mobsController.player.inventory.addItem(item)
52 }
54 }