DEADSOFTWARE

ddd71c4418cbae9603214f0c8985b84083564f13
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectCreativeInventoryItemMouseInputHandler.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.windows.GameWindowsManager
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.misc.Assets
14 import javax.inject.Inject
16 @GameScope
17 class SelectCreativeInventoryItemMouseInputHandler @Inject constructor(
18 private val gameItemsHolder: GameItemsHolder,
19 private val gameWindowsManager: GameWindowsManager,
20 private val mobsController: MobsController,
21 ) : IGameInputHandler<MouseInputAction> {
23 private val creativeInventoryTexture get() = requireNotNull(Assets.textureRegions["creative"])
25 override fun checkConditions(action: MouseInputAction): Boolean {
26 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CREATIVE_INVENTORY &&
27 !gameWindowsManager.isDragging &&
28 action.actionKey is MouseInputActionKey.Left &&
29 action.actionKey.touchUp && isInsideWindow(action, creativeInventoryTexture)
30 }
32 override fun handle(action: MouseInputAction) {
33 val creativeTexture = creativeInventoryTexture
34 val xOnGrid = (action.screenX - (action.cameraViewport.width / 2 - creativeTexture.regionWidth / 2 +
35 GameWindowsConfigs.Creative.itemsGridMarginLeft)) /
36 GameWindowsConfigs.Creative.itemsGridColWidth
37 val yOnGrid = (action.screenY - (action.cameraViewport.height / 2 - creativeTexture.regionHeight / 2 +
38 GameWindowsConfigs.Creative.itemsGridMarginTop)) /
39 GameWindowsConfigs.Creative.itemsGridRowHeight
41 if (xOnGrid < 0 || xOnGrid >= GameWindowsConfigs.Creative.itemsInRow ||
42 yOnGrid < 0 || yOnGrid >= GameWindowsConfigs.Creative.itemsInCol) {
43 return
44 }
46 val itemIndex = (gameWindowsManager.creativeScrollAmount * GameWindowsConfigs.Creative.itemsInRow +
47 (xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Creative.itemsInRow))
49 mobsController.player.inventory.copyInto(
50 destination = mobsController.player.inventory,
51 destinationOffset = 1,
52 startIndex = 0,
53 endIndex = mobsController.player.inventory.size - 1
54 )
56 val item = gameItemsHolder.getItemFromCreativeInventory(itemIndex)
57 mobsController.player.inventory[0] = item.toInventoryItem(amount = item.params.maxStack)
58 }
60 }