Files
ages/agame/room.py

54 lines
1.6 KiB
Python
Raw Normal View History

import dataclasses
from typing import MutableMapping, Optional, Sequence, Union, TYPE_CHECKING
from agame.item import ItemInst
from agame.util import search_item_name
if TYPE_CHECKING:
from agame.action import Action
__all__ = ("Room",)
@dataclasses.dataclass
class Room:
id: str
name: str
desc: Union[str, Sequence[str]]
items: MutableMapping[str, ItemInst]
# Actions that are executed upon teleport or entrance to this room.
teleport_actions: Sequence["Action"]
def __init__(
self,
id: str,
name: str,
desc: Union[str, Sequence[str]],
items: Union[Sequence[ItemInst], MutableMapping[str, ItemInst]],
teleport_actions: Sequence["Action"] = [],
):
self.id = id
self.name = name
self.desc = desc
if isinstance(items, MutableMapping):
self.items = items
else:
self.items = {item.id: item for item in items}
self.teleport_actions = teleport_actions
def search_item_name(self, item_name: str) -> Optional[ItemInst]:
"""
Searches all item instances in the room for the given item name, also
checking synonyms. Returns the first item instance found, or none if no
synonyms or names were found to match.
"""
return search_item_name(self.items.values(), item_name)
def remove(self, item_id: str) -> Optional[ItemInst]:
"""
Removes an item with the given ID from the room, returning it.
If it's not present in the room, `None` is returned.
"""
return self.items.pop(item_id, None)