DEADSOFTWARE

Drop items from hotbar
authorfredboy <fredboy@protonmail.com>
Thu, 9 May 2024 06:56:03 +0000 (13:56 +0700)
committerfredboy <fredboy@protonmail.com>
Thu, 9 May 2024 06:56:03 +0000 (13:56 +0700)
android/assets/json/texture_regions.json
android/assets/json/touch_buttons.json
android/assets/touch_gui.png
core/src/ru/deadsoftware/cavedroid/game/input/handler/keyboard/DropItemKeyboardInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/input/handler/mouse/HotbarMouseInputHandler.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Inventory.kt
core/src/ru/deadsoftware/cavedroid/game/mobs/player/Player.java
core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt
core/src/ru/deadsoftware/cavedroid/game/objects/DropController.java

index 6ccbffa8650704579c3e52349d8e1b0786068cd5..d0714239ccdd839e0fbd2408c06efc99d2125c1f 100644 (file)
       "y": 26,
       "w": 26,
       "h": 26
+    },
+    "inv": {
+      "x": 78,
+      "y": 26,
+      "w": 26,
+      "h": 26
     }
   },
   "allitems": {
index 4d8330104f285aa557d4d7c5065d287688e19bd7..81110960852f4d23dcf725050beab9a746a98634 100644 (file)
@@ -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
index b1944137a192cc296ac26d7233486d182bc61ec0..83521da12644b16933cf94a6d54ab39827b4770b 100644 (file)
Binary files a/android/assets/touch_gui.png and b/android/assets/touch_gui.png differ
index 4003271fd61301ac713fc65dc6fbb66897998d69..b46812c31fa31baf1792f554d8cb423825515578 100644 (file)
@@ -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
index 92f6a5a1a8ed4368393809cad2999a9688162363..6b86c8b5b0458889cd3fc5bd3537f9c239e6bb23 100644 (file)
@@ -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<MouseInputAction> {
 
     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)
             }
index 44ee6dd17e6bb94393475dc22c2778f4a2569338..36827c36bd997bdfea07808f98412d403a4e9f14 100644 (file)
@@ -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
index 58ddc025c849feaa37d91a71a5607d5e4b1c04ad..5fcfe319b80f886499cb42f17e53f7d29db5f5db 100644 (file)
@@ -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;
index 5b10fed5e75476531df51651b8c99022d82944d1..b6f21bb1d8e6c4509c43d82955a73768a1bb57d6 100644 (file)
@@ -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
index 639a2aa834365f9a27897f6bddfc9526213eab7f..e111f9cf9c0e541380c606ebddbf6daabc4d0ff9 100644 (file)
@@ -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() {