DEADSOFTWARE

Update README
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / input / handler / mouse / SelectCraftingInventoryItemMouseInputHandler.kt
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.model.item.InventoryItem
13 import ru.deadsoftware.cavedroid.game.windows.GameWindowsConfigs
14 import ru.deadsoftware.cavedroid.game.windows.GameWindowsManager
15 import ru.deadsoftware.cavedroid.game.windows.inventory.CraftingInventoryWindow
16 import ru.deadsoftware.cavedroid.misc.Assets
17 import javax.inject.Inject
19 @GameScope
20 class SelectCraftingInventoryItemMouseInputHandler @Inject constructor(
21 private val gameWindowsManager: GameWindowsManager,
22 private val mobsController: MobsController,
23 private val gameItemsHolder: GameItemsHolder,
24 ) : IGameInputHandler<MouseInputAction> {
26 private val survivalWindowTexture get() = requireNotNull(Assets.textureRegions["survival"])
28 override fun checkConditions(action: MouseInputAction): Boolean {
29 return gameWindowsManager.getCurrentWindow() == GameUiWindow.CRAFTING_TABLE &&
30 isInsideWindow(action, survivalWindowTexture) &&
31 (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Right || action.actionKey is MouseInputActionKey.Touch)
32 && action.actionKey.touchUp
33 }
35 private fun onLeftCLick(items: MutableList<InventoryItem?>, window: CraftingInventoryWindow, index: Int) {
36 val selectedItem = window.selectedItem
37 val clickedItem = items[index]
39 if (clickedItem != null && selectedItem != null && items[index]!!.item == selectedItem.item &&
40 items[index]!!.amount + selectedItem.amount <= selectedItem.item.params.maxStack) {
41 items[index]!!.amount += selectedItem.amount
42 window.selectedItem = null
43 return
44 }
46 val item = items[index]
47 items[index] = selectedItem ?: gameItemsHolder.fallbackItem.toInventoryItem()
48 window.selectedItem = item
49 }
51 private fun onRightClick(items: MutableList<InventoryItem?>, window: CraftingInventoryWindow, index: Int) {
52 val clickedItem = items[index]
53 val selectedItem = window.selectedItem
54 ?.takeIf { clickedItem == null || clickedItem.item.isNone() || it.item == items[index]!!.item && items[index]!!.amount + 1 < it.item.params.maxStack }
55 ?: return
57 val newItem = selectedItem.item.toInventoryItem((clickedItem?.takeIf { !it.item.isNone() }?.amount ?: 0) + 1)
58 items[index] = newItem
59 selectedItem.amount --
61 if (selectedItem.amount <= 0) {
62 window.selectedItem = null
63 }
64 }
66 private fun handleInsideInventoryGrid(action: MouseInputAction, xOnGrid: Int, yOnGrid: Int) {
67 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
69 var itemIndex = ((xOnGrid.toInt() + yOnGrid.toInt() * GameWindowsConfigs.Crafting.itemsInRow))
70 itemIndex += GameWindowsConfigs.Crafting.hotbarCells
72 if (itemIndex >= 36) {
73 itemIndex -= 36
74 }
76 if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
77 onLeftCLick(mobsController.player.inventory, window, itemIndex)
78 } else {
79 onRightClick(mobsController.player.inventory, window, itemIndex)
80 }
82 Gdx.app.debug(
83 TAG,
84 "selected item: ${window.selectedItem?.item?.params?.key ?: "null"}; index $itemIndex, grid ($xOnGrid;$yOnGrid)"
85 )
86 }
88 private fun handleInsideCraft(action: MouseInputAction, xOnCraft: Int, yOnCraft: Int) {
89 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
90 val index = xOnCraft + yOnCraft * GameWindowsConfigs.Crafting.craftGridSize
92 if (action.actionKey is MouseInputActionKey.Left || action.actionKey is MouseInputActionKey.Touch) {
93 onLeftCLick(window.craftingItems, window, index)
94 } else {
95 onRightClick(window.craftingItems, window, index)
96 }
98 window.craftResult =
99 gameItemsHolder.craftItem(window.craftingItems.map { it?.item ?: gameItemsHolder.fallbackItem })
102 override fun handle(action: MouseInputAction) {
103 val survivalTexture = survivalWindowTexture
104 val window = gameWindowsManager.currentWindow as CraftingInventoryWindow
106 val xOnWindow = action.screenX - (action.cameraViewport.width / 2 - survivalTexture.regionWidth / 2)
107 val yOnWindow = action.screenY - (action.cameraViewport.height / 2 - survivalTexture.regionHeight / 2)
109 val xOnGrid = (xOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginLeft) /
110 GameWindowsConfigs.Crafting.itemsGridColWidth
111 val yOnGrid = (yOnWindow - GameWindowsConfigs.Crafting.itemsGridMarginTop) /
112 GameWindowsConfigs.Crafting.itemsGridRowHeight
114 val xOnCraft = (xOnWindow - GameWindowsConfigs.Crafting.craftOffsetX) /
115 GameWindowsConfigs.Crafting.itemsGridColWidth
116 val yOnCraft = (yOnWindow - GameWindowsConfigs.Crafting.craftOffsetY) /
117 GameWindowsConfigs.Crafting.itemsGridRowHeight
119 val isInsideInventoryGrid = xOnGrid >= 0 && xOnGrid < GameWindowsConfigs.Crafting.itemsInRow &&
120 yOnGrid >= 0 && yOnGrid < GameWindowsConfigs.Crafting.itemsInCol
122 val isInsideCraftGrid = xOnCraft >= 0 && xOnCraft < GameWindowsConfigs.Crafting.craftGridSize &&
123 yOnCraft >= 0 && yOnCraft < GameWindowsConfigs.Crafting.craftGridSize
125 val isInsideCraftResult = xOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetX &&
126 xOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetX + GameWindowsConfigs.Crafting.itemsGridColWidth &&
127 yOnWindow > GameWindowsConfigs.Crafting.craftResultOffsetY &&
128 yOnWindow < GameWindowsConfigs.Crafting.craftResultOffsetY + GameWindowsConfigs.Crafting.itemsGridRowHeight
130 if (isInsideInventoryGrid) {
131 handleInsideInventoryGrid(action, xOnGrid.toInt(), yOnGrid.toInt())
132 } else if (isInsideCraftGrid) {
133 handleInsideCraft(action, xOnCraft.toInt(), yOnCraft.toInt())
134 } else if (isInsideCraftResult) {
135 val selectedItem = window.selectedItem
136 if (selectedItem == null || selectedItem.item.isNone() ||
137 (selectedItem.item == window.craftResult?.item && selectedItem.amount + (window.craftResult?.amount ?: 0) <= selectedItem.item.params.maxStack)) {
138 for (i in window.craftingItems.indices) {
139 if ((window.craftingItems[i]?.amount ?: 0) > 1) {
140 window.craftingItems[i]?.amount = window.craftingItems[i]?.amount!! - 1
141 } else {
142 window.craftingItems[i] = null
145 if (selectedItem != null && !selectedItem.item.isNone()) {
146 selectedItem.amount += (window.craftResult?.amount ?: 0)
147 } else {
148 window.selectedItem = window.craftResult
150 window.craftResult = gameItemsHolder.craftItem(window.craftingItems
151 .map { it?.item ?: gameItemsHolder.fallbackItem })
157 companion object {
158 private const val TAG = "SelectCraftingInventoryItemMouseInputHandler"