From: fredboy Date: Thu, 9 May 2024 06:56:03 +0000 (+0700) Subject: Drop items from hotbar X-Git-Tag: alpha0.8.0~4 X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=398c9c46a58db1b26f502f9a55956ba03b90deaf;p=cavedroid.git Drop items from hotbar --- 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 b194413..83521da 100644 Binary files a/android/assets/touch_gui.png and b/android/assets/touch_gui.png differ diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt index 4003271..b46812c 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt @@ -25,32 +25,25 @@ class DropItemKeyboardInputHandler @Inject constructor( !mobsController.player.inventory.activeItem.item.isNone() } - private fun createDrop(item: Item, playerX: Float, playerY: Float) { - dropController.addDrop(playerX + ((DROP_DISTANCE - Drop.DROP_SIZE / 2) * mobsController.player.direction.basis), playerY, item) + 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 + ) } override fun handle(action: KeyboardInputAction) { val player = mobsController.player val currentItem = player.inventory.activeItem + val dropAmount = if (currentItem.item.isTool()) currentItem.amount else 1 - if (!currentItem.item.isTool()) { - createDrop(currentItem.item, player.x, player.y) - } else { - for (i in 1..currentItem.amount) { - createDrop(currentItem.item, player.x, player.y) - } - } - - player.inventory.decreaseCurrentItemAmount( - if (currentItem.item.isTool()) { - currentItem.amount - } else { - 1 - } - ) + createDrop(currentItem.item, player.x, player.y, dropAmount) + player.inventory.decreaseCurrentItemAmount(dropAmount) } companion object { - private const val DROP_DISTANCE = 20f + const val DROP_DISTANCE = 20f } } \ No newline at end of file diff --git a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt index 92f6a5a..6b86c8b 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt @@ -7,9 +7,13 @@ import ru.deadsoftware.cavedroid.game.ui.windows.GameWindowsManager import ru.deadsoftware.cavedroid.game.input.IGameInputHandler import ru.deadsoftware.cavedroid.game.input.action.MouseInputAction import ru.deadsoftware.cavedroid.game.input.action.keys.MouseInputActionKey +import ru.deadsoftware.cavedroid.game.input.handler.keyboard.DropItemKeyboardInputHandler.Companion.DROP_DISTANCE import ru.deadsoftware.cavedroid.game.input.isInsideHotbar import ru.deadsoftware.cavedroid.game.mobs.MobsController import ru.deadsoftware.cavedroid.game.mobs.player.Player +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.misc.Assets import javax.inject.Inject @@ -17,6 +21,7 @@ import javax.inject.Inject class HotbarMouseInputHandler @Inject constructor( private val gameWindowsManager: GameWindowsManager, private val mobsController: MobsController, + private val dropController: DropController, ) : IGameInputHandler { 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() {