From 398c9c46a58db1b26f502f9a55956ba03b90deaf Mon Sep 17 00:00:00 2001 From: fredboy Date: Thu, 9 May 2024 13:56:03 +0700 Subject: [PATCH] Drop items from hotbar --- android/assets/json/texture_regions.json | 6 +++ android/assets/json/touch_buttons.json | 7 +++ android/assets/touch_gui.png | Bin 2137 -> 6389 bytes .../keyboard/DropItemKeyboardInputHandler.kt | 29 ++++------- .../handler/mouse/HotbarMouseInputHandler.kt | 49 ++++++++++++++---- .../cavedroid/game/mobs/player/Inventory.kt | 18 ++++--- .../cavedroid/game/mobs/player/Player.java | 6 +++ .../cavedroid/game/objects/Drop.kt | 3 +- .../game/objects/DropController.java | 10 ++-- 9 files changed, 87 insertions(+), 41 deletions(-) diff --git a/android/assets/json/texture_regions.json b/android/assets/json/texture_regions.json index 6ccbffa..d071423 100644 --- a/android/assets/json/texture_regions.json +++ b/android/assets/json/texture_regions.json @@ -35,6 +35,12 @@ "y": 26, "w": 26, "h": 26 + }, + "inv": { + "x": 78, + "y": 26, + "w": 26, + "h": 26 } }, "allitems": { diff --git a/android/assets/json/touch_buttons.json b/android/assets/json/touch_buttons.json index 4d83301..8111096 100644 --- a/android/assets/json/touch_buttons.json +++ b/android/assets/json/touch_buttons.json @@ -5,5 +5,12 @@ "w": 32, "h": 32, "key": "L-Alt" + }, + "inv": { + "x": -64, + "y": -32, + "w": 32, + "h": 32, + "key": "E" } } \ No newline at end of file diff --git a/android/assets/touch_gui.png b/android/assets/touch_gui.png index b1944137a192cc296ac26d7233486d182bc61ec0..83521da12644b16933cf94a6d54ab39827b4770b 100644 GIT binary patch literal 6389 zcmeHLc{J3~+y9O&Nt$S}rD>#4voFjvLo#Kl5t34gG4mZJ%uF+bL5dbyM2b{)DwMR) zMnx-8C`qNFMYIU5qV(Pw+Rpns=RKYC{{HLExpSZIz0dQx&;8uzJ~Q{b#nr`Ow3eY3 z1VN*n99iz5jRw_4Qv-Z=J~SBvng)2O6W__n64C?@Kog+`A%GMODq>i60kR506;cIl zEU3Cb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-sCKxYSL5|oblhkf1jEi zL{XGFub?ZZ4}D(MEI|G_nNq2*Wodi<=g*%z3DI@*8C4P($1!~0S0+ck@3V(PC~Q{s zd;-l3Xqw2a&6hbz(}g~RCxEzSE_Pa(EM5mBhm7z0eriETG=Drz%c^)}SNe>G09~DM zAvvEuedT^BPe1iKA-fQuL~w)@A{!SH>802i2=~+Jg2gV+_U# zdT~FgBRZX zIzV6SZ^L7nuCpqkZiR?u2dL!?I+|#M+;0X31_lNO9pFEhlJi$oUG3rk0000 { private val hotbarTexture get() = requireNotNull(Assets.textureRegions["hotbar"]) @@ -36,15 +41,37 @@ class HotbarMouseInputHandler @Inject constructor( buttonHoldTask = null } - private fun handleHold() { - buttonHoldTask = null - gameWindowsManager.openInventory() + private fun createDrop(item: Item, playerX: Float, playerY: Float, amount: Int) { + dropController.addDrop( + /* x = */ playerX + ((DROP_DISTANCE - Drop.DROP_SIZE / 2) * mobsController.player.direction.basis), + /* y = */ playerY, + /* item = */ item, + /* count = */ amount + ) + } + + private fun getActionSlot(action: MouseInputAction): Int { + return ((action.screenX - + (action.cameraViewport.width / 2 - hotbarTexture.regionWidth / 2)) + / HOTBAR_CELL_WIDTH).toInt() + } + + private fun handleHold(action: MouseInputAction) { +// buttonHoldTask = null +// gameWindowsManager.openInventory() + val player = mobsController.player + val actionSlot = getActionSlot(action) + val currentItem = player.inventory.items[actionSlot] + val dropAmount = if (currentItem.item.isTool()) currentItem.amount else 1 + + createDrop(currentItem.item, player.x, player.y, dropAmount) + player.inventory.decreaseItemAmount(actionSlot, dropAmount) } private fun handleDown(action: MouseInputAction) { buttonHoldTask = object : Timer.Task() { override fun run() { - handleHold() + handleHold(action) } } @@ -52,10 +79,7 @@ class HotbarMouseInputHandler @Inject constructor( } private fun handleUp(action: MouseInputAction) { - mobsController.player.inventory.activeSlot = - ((action.screenX - - (action.cameraViewport.width / 2 - hotbarTexture.regionWidth / 2)) - / HOTBAR_CELL_WIDTH).toInt() + mobsController.player.inventory.activeSlot = getActionSlot(action) } private fun handleScroll(action: MouseInputAction) { @@ -65,7 +89,7 @@ class HotbarMouseInputHandler @Inject constructor( mobsController.player.inventory.activeSlot += action.actionKey.amountY.toInt() if (mobsController.player.inventory.activeSlot < 0) { mobsController.player.inventory.activeSlot = Player.HOTBAR_SIZE - 1 - } else if (mobsController.player.inventory.activeSlot >= Player.HOTBAR_SIZE){ + } else if (mobsController.player.inventory.activeSlot >= Player.HOTBAR_SIZE) { mobsController.player.inventory.activeSlot = 0 } } @@ -75,7 +99,12 @@ class HotbarMouseInputHandler @Inject constructor( cancelHold() } - if (action.actionKey !is MouseInputActionKey.Left && action.actionKey !is MouseInputActionKey.Screen ) { + if (buttonHoldTask != null && buttonHoldTask?.isScheduled != true) { + buttonHoldTask = null + return + } + + if (action.actionKey !is MouseInputActionKey.Left && action.actionKey !is MouseInputActionKey.Screen) { if (action.actionKey is MouseInputActionKey.Scroll) { handleScroll(action) } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt index 44ee6dd..36827c3 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt @@ -82,9 +82,9 @@ class Inventory( val inventoryItem = items[slot] if (inventoryItem.item == drop.item) { - inventoryItem.add() + inventoryItem.add(drop.amount) } else { - _items[slot] = drop.item.toInventoryItem() + _items[slot] = drop.item.toInventoryItem(drop.amount) if (slot == activeSlot) { showCurrentItemTooltip() } @@ -106,10 +106,16 @@ class Inventory( } @JvmOverloads - fun decreaseCurrentItemAmount(count: Int = 1) { - activeItem.subtract(count) - if (activeItem.amount <= 0) { - _items[activeSlot] = fallbackItem + fun decreaseItemAmount(slot: Int, count: Int = 1) { + val item = _items[slot] + item.subtract(count) + if (item.amount <= 0) { + _items[slot] = fallbackItem } } + + @JvmOverloads + fun decreaseCurrentItemAmount(count: Int = 1) { + decreaseItemAmount(activeSlot, count) + } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java index 58ddc02..5fcfe31 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java @@ -10,6 +10,7 @@ import ru.deadsoftware.cavedroid.game.model.block.Block; import ru.deadsoftware.cavedroid.game.model.item.InventoryItem; import ru.deadsoftware.cavedroid.game.model.item.Item; import ru.deadsoftware.cavedroid.game.objects.Drop; +import ru.deadsoftware.cavedroid.game.objects.DropController; import ru.deadsoftware.cavedroid.game.ui.TooltipManager; import ru.deadsoftware.cavedroid.game.world.GameWorld; import ru.deadsoftware.cavedroid.misc.Assets; @@ -83,6 +84,11 @@ public class Player extends Mob { } } + public void dropCurrentItem(DropController dropController) { + final InventoryItem activeItem = inventory.getActiveItem(); + + } + private Vector2 getSpawnPoint(GameWorld gameWorld, GameItemsHolder itemsHolder) { if (spawnPoint != null) { return spawnPoint; diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt index 5b10fed..b6f21bb 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt @@ -6,10 +6,11 @@ import com.badlogic.gdx.math.Vector2 import ru.deadsoftware.cavedroid.game.GameItemsHolder import ru.deadsoftware.cavedroid.game.model.item.Item -class Drop( +class Drop @JvmOverloads constructor( x: Float, y: Float, _item: Item, + val amount: Int = 1, ) : Rectangle(x, y, DROP_SIZE, DROP_SIZE) { val itemKey = _item.params.key diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java b/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java index 639a2aa..e111f9c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java @@ -27,16 +27,14 @@ public class DropController implements Serializable { } public void addDrop(float x, float y, Item item) { - if (item.isNone()) { - return; - } - mDrops.add(new Drop(x, y, item)); + addDrop(x, y, item, 1); } public void addDrop(float x, float y, Item item, int count) { - for (int i = 0 ; i < count; i++) { - addDrop(x, y, item); + if (item.isNone()) { + return; } + mDrops.add(new Drop(x, y, item, count)); } public int getSize() { -- 2.29.2