1
0

Actually consume consumable items, completing part 8

This commit is contained in:
Timothy Warren 2022-01-12 16:53:09 -05:00
parent c54ae75f08
commit d8654e5db5
5 changed files with 13 additions and 10 deletions

View File

@ -82,11 +82,6 @@ class ItemAction(Action):
self.item.consumable.activate(self) self.item.consumable.activate(self)
class EscapeAction(Action):
def perform(self) -> None:
raise SystemExit()
class DropItem(ItemAction): class DropItem(ItemAction):
def perform(self) -> None: def perform(self) -> None:
self.entity.inventory.drop(self.item) self.entity.inventory.drop(self.item)

View File

@ -4,6 +4,7 @@ from typing import Optional, TYPE_CHECKING
import actions import actions
import color import color
import components.inventory
from components.base_component import BaseComponent from components.base_component import BaseComponent
from exceptions import Impossible from exceptions import Impossible
@ -25,6 +26,13 @@ class Consumable(BaseComponent):
""" """
raise NotImplementedError() 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): class HealingConsumable(Consumable):
def __init__(self, amount: int): def __init__(self, amount: int):
@ -39,5 +47,6 @@ class HealingConsumable(Consumable):
f"You consume the {self.parent.name}, and recover {amount_recovered} HP!", f"You consume the {self.parent.name}, and recover {amount_recovered} HP!",
color.health_recovered color.health_recovered
) )
self.consume()
else: else:
raise Impossible(f"Your health is already full.") raise Impossible(f"Your health is already full.")

View File

@ -1,7 +1,7 @@
from __future__ import annotations from __future__ import annotations
import copy 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 from render_order import RenderOrder
@ -20,7 +20,7 @@ class Entity:
A generic object to represent players, enemies, items, etc. A generic object to represent players, enemies, items, etc.
""" """
parent: GameMap parent: Union[GameMap, Inventory]
def __init__( def __init__(
self, self,

View File

@ -8,7 +8,6 @@ import actions
from actions import ( from actions import (
Action, Action,
BumpAction, BumpAction,
EscapeAction,
PickupAction, PickupAction,
WaitAction WaitAction
) )
@ -241,7 +240,7 @@ class MainGameEventHandler(EventHandler):
action = WaitAction(player) action = WaitAction(player)
elif key == tcod.event.K_ESCAPE: elif key == tcod.event.K_ESCAPE:
action = EscapeAction(player) raise SystemExit()
elif key == tcod.event.K_v: elif key == tcod.event.K_v:
self.engine.event_handler = HistoryViewer(self.engine) self.engine.event_handler = HistoryViewer(self.engine)

View File

@ -16,7 +16,7 @@ class Message:
def full_text(self) -> str: def full_text(self) -> str:
"""The full text of this message, including the count if necessary.""" """The full text of this message, including the count if necessary."""
if self.count > 1: if self.count > 1:
return f"{self.plain_text} (x{self.count}" return f"{self.plain_text} (x{self.count})"
return self.plain_text return self.plain_text