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):
|
def add_item(self, item, count=1, silent=False):
|
||||||
"""Adds an item to the inventory."""
|
"""Adds an item to the inventory."""
|
||||||
if item in self.items:
|
existing = next((e for e in self.items if e.name.lower() == item.name.lower()), None)
|
||||||
self.items[item]["Count"] += count
|
if existing:
|
||||||
|
self.items[existing]["Count"] += count
|
||||||
if not silent:
|
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:
|
else:
|
||||||
self.items[item] = {"Count": count, "object": item.itemType}
|
self.items[item] = {"Count": count, "object": item.itemType}
|
||||||
if not silent:
|
if not silent:
|
||||||
@@ -197,11 +198,11 @@ class Inventory:
|
|||||||
print(f'You are currently carrying:')
|
print(f'You are currently carrying:')
|
||||||
found_un_equipped = False
|
found_un_equipped = False
|
||||||
for item_obj, item_data in self.items.items():
|
for item_obj, item_data in self.items.items():
|
||||||
# Check if the item is in the backpack AND not currently equipped in any slot
|
equipped_count = sum(1 for eq in self.equipped_items.values() if eq is item_obj)
|
||||||
if item_obj not in self.equipped_items.values():
|
backpack_count = item_data["Count"] - equipped_count
|
||||||
|
if backpack_count > 0:
|
||||||
found_un_equipped = True
|
found_un_equipped = True
|
||||||
count = item_data["Count"]
|
print(f'{item_obj.name} x {backpack_count}\n'
|
||||||
print(f'{item_obj.name} x {count}\n'
|
|
||||||
f' {item_obj.description}\n')
|
f' {item_obj.description}\n')
|
||||||
if not found_un_equipped:
|
if not found_un_equipped:
|
||||||
print(" Your rucksack is empty.")
|
print(" Your rucksack is empty.")
|
||||||
|
|||||||
Reference in New Issue
Block a user