From 445466cd94b52a4acdaa3e22b14533305fd73488 Mon Sep 17 00:00:00 2001 From: fredboy Date: Fri, 3 May 2024 19:18:30 +0700 Subject: [PATCH] Drop behavior fixes --- .../cavedroid/game/GamePhysics.java | 4 ++ .../cavedroid/game/mobs/Player.java | 48 ++++++++++++------- .../cavedroid/game/objects/Drop.kt | 4 +- 3 files changed, 37 insertions(+), 19 deletions(-) diff --git a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java index a0c8c37..248a47f 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java +++ b/core/src/ru/deadsoftware/cavedroid/game/GamePhysics.java @@ -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); } diff --git a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java index 70be5a6..47d48d8 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java +++ b/core/src/ru/deadsoftware/cavedroid/game/mobs/Player.java @@ -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; diff --git a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt index 151e486..d7c6a45 100644 --- a/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt +++ b/core/src/ru/deadsoftware/cavedroid/game/objects/Drop.kt @@ -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) -- 2.29.2