Use directly equipped items outside the inventory

This commit is contained in:
Yohann D'ANELLO 2020-12-18 17:57:42 +01:00
parent 9475725228
commit 9aa684fb77
3 changed files with 15 additions and 2 deletions

View File

@ -29,7 +29,7 @@ class Item(Entity):
The item is dropped from the inventory onto the floor The item is dropped from the inventory onto the floor
""" """
if self.held: if self.held:
self.held_by.inventory.remove(self) self.held_by.remove_from_inventory(self)
self.held_by.map.add_entity(self) self.held_by.map.add_entity(self)
self.move(self.held_by.y, self.held_by.x) self.move(self.held_by.y, self.held_by.x)
self.held = False self.held = False
@ -46,8 +46,8 @@ class Item(Entity):
""" """
if self.held_by.equipped_item: if self.held_by.equipped_item:
self.held_by.equipped_item.unequip() self.held_by.equipped_item.unequip()
self.held_by.equipped_item = self
self.held_by.remove_from_inventory(self) self.held_by.remove_from_inventory(self)
self.held_by.equipped_item = self
def unequip(self) -> None: def unequip(self) -> None:
""" """

View File

@ -75,6 +75,17 @@ class Player(InventoryHolder, FightingEntity):
self.current_xp += xp self.current_xp += xp
self.level_up() self.level_up()
def remove_from_inventory(self, obj: Item) -> None:
"""
Remove the given item from the inventory, even if the item is equipped.
"""
if obj == self.equipped_item:
self.equipped_item = None
elif obj == self.equipped_armor:
self.equipped_armor = None
else:
return super().remove_from_inventory(obj)
# noinspection PyTypeChecker,PyUnresolvedReferences # noinspection PyTypeChecker,PyUnresolvedReferences
def check_move(self, y: int, x: int, move_if_possible: bool = False) \ def check_move(self, y: int, x: int, move_if_possible: bool = False) \
-> bool: -> bool:

View File

@ -127,6 +127,8 @@ class Game:
elif key == KeyValues.INVENTORY: elif key == KeyValues.INVENTORY:
self.state = GameMode.INVENTORY self.state = GameMode.INVENTORY
self.display_actions(DisplayActions.UPDATE) self.display_actions(DisplayActions.UPDATE)
elif key == KeyValues.USE and self.player.equipped_item:
self.player.equipped_item.use()
elif key == KeyValues.SPACE: elif key == KeyValues.SPACE:
self.state = GameMode.MAINMENU self.state = GameMode.MAINMENU
elif key == KeyValues.CHAT: elif key == KeyValues.CHAT: