Update example game some, update engine some

* Add RevealAction/UnrevealAction for revealing/hiding items in a room
* Add a lot of checks for items being revealed when it's attempted to be
  triggered
* Implement TeleportAction (mostly)
* For all Check* family of actions, the `yes` and `no` values may be
  just be a single action instead of an array of actions
* Change up how room descriptions and stuff work, mostly so that you can
  specify multiple lines in an array so you can preserve paragraph
  breaks when displayed.
* Example game has some more content

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2021-11-18 16:26:16 -08:00
parent 1304b27944
commit 7f86aafc05
10 changed files with 446 additions and 121 deletions

View File

@@ -19,6 +19,8 @@ __all__ = (
"Var",
"CheckVarAction",
"SetVarAction",
"RevealItemAction",
"UnrevealItemAction",
)
@@ -79,8 +81,10 @@ class TeleportAction(Action):
room_id: str
def act(self, game: "Game"):
# TODO
raise NotImplementedError("TODO - implement teleport action")
assert (
self.room_id in game.database.rooms
), f"could not find room with id {self.room_id}"
game.room = game.database.rooms[self.room_id]
@dataclasses.dataclass
@@ -126,6 +130,24 @@ class CheckInvItemsAction(Action):
yes: Sequence[Action]
no: Sequence[Action]
def __init__(
self,
item_ids: Union[str, Sequence[str]],
yes: Union[Action, Sequence[Action]] = [],
no: Union[Action, Sequence[Action]] = [],
):
self.item_ids = item_ids
# Put the "yes" action into an array if necessary
if isinstance(yes, Action):
self.yes = [yes]
else:
self.yes = yes
# Put the "no" action into an array if necessary
if isinstance(no, Action):
self.no = [no]
else:
self.no = no
def act(self, game: "Game"):
# If the item_ids are just a string, use that as a list.
items = [self.item_ids] if isinstance(self.item_ids, str) else self.item_ids
@@ -143,10 +165,28 @@ class CheckRoomItemsAction(Action):
appropriate action sequence.
"""
item_ids: str
item_ids: Union[str, Sequence[str]]
yes: Sequence[Action]
no: Sequence[Action]
def __init__(
self,
item_ids: Union[str, Sequence[str]],
yes: Union[Action, Sequence[Action]] = [],
no: Union[Action, Sequence[Action]] = [],
):
self.item_ids = item_ids
# Put the "yes" action into an array if necessary
if isinstance(yes, Action):
self.yes = [yes]
else:
self.yes = yes
# Put the "no" action into an array if necessary
if isinstance(no, Action):
self.no = [no]
else:
self.no = no
def act(self, game: "Game"):
items = [self.item_ids] if isinstance(self.item_ids, str) else self.item_ids
for item_id in items:
@@ -225,10 +265,37 @@ class CheckVarAction(Action):
# The action to execute when this is false.
no: Sequence[Action]
def __init__(
self,
var_id: str,
compare: Compare = Compare.EQUALS,
against: Any = True,
yes: Union[Action, Sequence[Action]] = [],
no: Union[Action, Sequence[Action]] = [],
):
self.var_id = var_id
self.compare = compare
self.against = against
# Put the "yes" action into an array if necessary
if isinstance(yes, Action):
self.yes = [yes]
else:
self.yes = yes
# Put the "no" action into an array if necessary
if isinstance(no, Action):
self.no = [no]
else:
self.no = no
def act(self, game: "Game"):
assert self.var_id in game.vars, f"variable '{self.var_id}' does not exist"
val = game.vars.get(self.var_id, None)
compare_val = None
if isinstance(self.against, Var):
assert (
self.against.id in game.vars
), f"variable '{self.against.id}' does not exist"
compare_val = game.vars.get(self.against.id, None)
else:
compare_val = self.against
@@ -288,7 +355,38 @@ class SetVarAction(Action):
value: Any
def act(self, game: "Game"):
assert self.var_id in game.vars, f"variable '{self.var_id}' does not exist"
value = (
game.vars.get(self.value.id) if isinstance(self.value, Var) else self.value
)
game.vars[self.var_id] = value
@dataclasses.dataclass
class RevealItemAction(Action):
"""
Reveal an item in the current room.
"""
item_id: str
def act(self, game: "Game"):
assert (
self.item_id in game.room.items
), f"item id {self.item_id} does not exist in the current room {game.room.id}"
game.room.items[self.item_id].revealed = True
@dataclasses.dataclass
class UnrevealItemAction(Action):
"""
Unreveal an item in the current room.
"""
item_id: str
def act(self, game: "Game"):
assert (
self.item_id in game.room.items
), f"item id {self.item_id} does not exist in the current room {game.room.id}"
game.room.items[self.item_id].revealed = False