From d8654e5db511f29d67e3c2e520f8d2f4af22492a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 12 Jan 2022 16:53:09 -0500 Subject: [PATCH] Actually consume consumable items, completing part 8 --- actions.py | 5 ----- components/consumable.py | 9 +++++++++ entity.py | 4 ++-- input_handlers.py | 3 +-- message_log.py | 2 +- 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/actions.py b/actions.py index 62de5a8..3667daa 100644 --- a/actions.py +++ b/actions.py @@ -82,11 +82,6 @@ class ItemAction(Action): self.item.consumable.activate(self) -class EscapeAction(Action): - def perform(self) -> None: - raise SystemExit() - - class DropItem(ItemAction): def perform(self) -> None: self.entity.inventory.drop(self.item) diff --git a/components/consumable.py b/components/consumable.py index c40cd13..af161a6 100644 --- a/components/consumable.py +++ b/components/consumable.py @@ -4,6 +4,7 @@ from typing import Optional, TYPE_CHECKING import actions import color +import components.inventory from components.base_component import BaseComponent from exceptions import Impossible @@ -25,6 +26,13 @@ class Consumable(BaseComponent): """ raise NotImplementedError() + def consume(self) -> None: + """Remove the consumed item from its containing inventory.""" + entity = self.parent + inventory = entity.parent + if isinstance(inventory, components.inventory.Inventory): + inventory.items.remove(entity) + class HealingConsumable(Consumable): def __init__(self, amount: int): @@ -39,5 +47,6 @@ class HealingConsumable(Consumable): f"You consume the {self.parent.name}, and recover {amount_recovered} HP!", color.health_recovered ) + self.consume() else: raise Impossible(f"Your health is already full.") diff --git a/entity.py b/entity.py index b947d4b..b88df62 100644 --- a/entity.py +++ b/entity.py @@ -1,7 +1,7 @@ from __future__ import annotations import copy -from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING +from typing import Optional, Tuple, Type, TypeVar, TYPE_CHECKING, Union from render_order import RenderOrder @@ -20,7 +20,7 @@ class Entity: A generic object to represent players, enemies, items, etc. """ - parent: GameMap + parent: Union[GameMap, Inventory] def __init__( self, diff --git a/input_handlers.py b/input_handlers.py index 935fa6d..11db90b 100644 --- a/input_handlers.py +++ b/input_handlers.py @@ -8,7 +8,6 @@ import actions from actions import ( Action, BumpAction, - EscapeAction, PickupAction, WaitAction ) @@ -241,7 +240,7 @@ class MainGameEventHandler(EventHandler): action = WaitAction(player) elif key == tcod.event.K_ESCAPE: - action = EscapeAction(player) + raise SystemExit() elif key == tcod.event.K_v: self.engine.event_handler = HistoryViewer(self.engine) diff --git a/message_log.py b/message_log.py index 3c3138c..8d59b3a 100644 --- a/message_log.py +++ b/message_log.py @@ -16,7 +16,7 @@ class Message: def full_text(self) -> str: """The full text of this message, including the count if necessary.""" if self.count > 1: - return f"{self.plain_text} (x{self.count}" + return f"{self.plain_text} (x{self.count})" return self.plain_text