Fix pick-up merging: name-based add_item and backpack-count display
This commit is contained in:
15
player.py
15
player.py
@@ -167,10 +167,11 @@ class Inventory:
|
||||
|
||||
def add_item(self, item, count=1, silent=False):
|
||||
"""Adds an item to the inventory."""
|
||||
if item in self.items:
|
||||
self.items[item]["Count"] += count
|
||||
existing = next((e for e in self.items if e.name.lower() == item.name.lower()), None)
|
||||
if existing:
|
||||
self.items[existing]["Count"] += count
|
||||
if not silent:
|
||||
print(f"Added {count} more {item.name}. Total: {self.items[item]['Count']}.")
|
||||
print(f"Added {count} more {existing.name}. Total: {self.items[existing]['Count']}.")
|
||||
else:
|
||||
self.items[item] = {"Count": count, "object": item.itemType}
|
||||
if not silent:
|
||||
@@ -197,11 +198,11 @@ class Inventory:
|
||||
print(f'You are currently carrying:')
|
||||
found_un_equipped = False
|
||||
for item_obj, item_data in self.items.items():
|
||||
# Check if the item is in the backpack AND not currently equipped in any slot
|
||||
if item_obj not in self.equipped_items.values():
|
||||
equipped_count = sum(1 for eq in self.equipped_items.values() if eq is item_obj)
|
||||
backpack_count = item_data["Count"] - equipped_count
|
||||
if backpack_count > 0:
|
||||
found_un_equipped = True
|
||||
count = item_data["Count"]
|
||||
print(f'{item_obj.name} x {count}\n'
|
||||
print(f'{item_obj.name} x {backpack_count}\n'
|
||||
f' {item_obj.description}\n')
|
||||
if not found_un_equipped:
|
||||
print(" Your rucksack is empty.")
|
||||
|
||||
Reference in New Issue
Block a user