DEADSOFTWARE

Drop behavior fixes
authorfredboy <fredboy@protonmail.com>
Fri, 3 May 2024 12:18:30 +0000 (19:18 +0700)
committerfredboy <fredboy@protonmail.com>
Fri, 3 May 2024 12:18:30 +0000 (19:18 +0700)
core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java
core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java
core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt

index a0c8c378ef3c35e6006e99dab47b192fb669d75c..248a47ff92b7c353e63a1badaa27f7c0adbdace5 100644 (file)
@@ -113,6 +113,10 @@ public class GamePhysics {
     private Rectangle getShiftedMagnetingPlayerRect(Drop drop) {
         final Player player = mMobsController.getPlayer();
 
+        if (player.canPickUpDrop(drop) < 0) {
+            return null;
+        }
+
         if (drop.canMagnetTo(player)) {
             return getShiftedPlayerRect(0);
         }
index 70be5a6b5333214b406b0bd5cda5920551c1801a..47d48d8a415374d61279a2c2af1c04d3da291e6a 100644 (file)
@@ -62,11 +62,6 @@ public class Player extends Mob {
         }
     }
 
-    @CheckForNull
-    public Item inventory(int i) {
-        return inventory.get(i).getItem();
-    }
-    
     public void respawn(GameWorld gameWorld, GameItemsHolder itemsHolder) {
         Vector2 pos = getSpawnPoint(gameWorld, itemsHolder);
         this.x = pos.x;
@@ -90,24 +85,43 @@ public class Player extends Mob {
         return inventory.get(slot);
     }
 
-    public void pickUpDrop(Drop drop) {
-        for (InventoryItem invItem : inventory) {
+    /**
+     * @return index of inventory where this drop could be placed or -1 if cant pick up
+     */
+    public int canPickUpDrop(Drop drop) {
+        for (int i = 0; i < 36; i++) {
+            final InventoryItem invItem = inventory.get(i);
+
             if (!invItem.getItem().isTool()
                     && invItem.getItem() == drop.getItem()
                     && invItem.getAmount() < invItem.getItem().getParams().getMaxStack()) {
-                invItem.setAmount(invItem.getAmount() + 1);
-                drop.setPickedUp(true);
-                return;
+                return i;
             }
-        }
 
-        for (int i = 0; i < inventory.size(); i++) {
-            if (inventory(i) == null || inventory(i).getParams().getKey().equals(GameItemsHolder.FALLBACK_ITEM_KEY)) {
-                inventory.set(i, drop.getItem().toInventoryItem());
-                drop.setPickedUp(true);
-                break;
+            if (invItem.getItem().isNone()) {
+                return i;
             }
         }
+
+        return -1;
+    }
+
+    public void pickUpDrop(Drop drop) {
+        int index = canPickUpDrop(drop);
+
+        if (index < 0) {
+            return;
+        }
+
+        final InventoryItem invItem = inventory.get(index);
+
+        if (invItem.getItem().equals(drop.getItem())) {
+            invItem.setAmount(invItem.getAmount() + 1);
+            drop.setPickedUp(true);
+        } else if (invItem.getItem().isNone()) {
+            inventory.set(index, drop.getItem().toInventoryItem());
+            drop.setPickedUp(true);
+        }
     }
 
     private Vector2 getSpawnPoint(GameWorld gameWorld, GameItemsHolder itemsHolder) {
@@ -263,7 +277,7 @@ public class Player extends Mob {
     }
 
     private void drawItem(SpriteBatch spriteBatch, float x, float y, float anim) {
-        final Item item = inventory(slot);
+        final Item item = inventory.get(slot).getItem();
 
         if (item == null || item.isNone()) {
             return;
index 151e48609fbc98a1c4bd95671b4530790f9abc9a..d7c6a45d6773eb5f3af9a6db481cf05d49400ef3 100644 (file)
@@ -47,9 +47,9 @@ class Drop(
     }
 
     companion object {
-        private const val MAGNET_DISTANCE = 16f
+        private const val MAGNET_DISTANCE = 4f
 
-        const val MAGNET_VELOCITY = 128f
+        const val MAGNET_VELOCITY = 256f
         const val DROP_SIZE = 8f
 
         private fun getInitialVelocity(): Vector2 = Vector2(0f, -1f)