Add dialog actions
Rudimentary user input for dialogs has been added. * Displays must add support for showing dialog options, and receiving user input for dialog selections. * A selected dialog can optionally perform actions, and then directs the game into the next dialog state. * Dialog options may be conditionally shown with variables. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import dataclasses
|
||||
import time
|
||||
from typing import Any, Optional, Sequence, Union, TYPE_CHECKING
|
||||
from typing import Any, Mapping, Optional, Sequence, Union, TYPE_CHECKING
|
||||
import enum
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from agame.game import Game
|
||||
from agame.dialog import DialogOption
|
||||
|
||||
|
||||
__all__ = (
|
||||
@@ -24,6 +25,7 @@ __all__ = (
|
||||
"SetVarAction",
|
||||
"RevealItemAction",
|
||||
"UnrevealItemAction",
|
||||
"DialogAction",
|
||||
)
|
||||
|
||||
|
||||
@@ -441,3 +443,32 @@ class UnrevealItemAction(Action):
|
||||
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
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class DialogAction(Action):
|
||||
"""
|
||||
Displays a new dialog.
|
||||
"""
|
||||
|
||||
dialog: Mapping[str, Sequence["DialogOption"]]
|
||||
start: str
|
||||
|
||||
def act(self, game: "Game"):
|
||||
selection_id: Optional[str] = self.start
|
||||
|
||||
while selection_id:
|
||||
# Figure out which options should appear
|
||||
options = [
|
||||
opt
|
||||
for opt in self.dialog[selection_id]
|
||||
# this is a logical implication
|
||||
# required_var -> (game.vars[opt.required_var])
|
||||
if not opt.required_var
|
||||
or (opt.required_var and game.vars[opt.required_var])
|
||||
]
|
||||
# Get the selection
|
||||
selection = game.display.dialog_options(options)
|
||||
selection_id = selection.next
|
||||
# Do dialog actions
|
||||
game.do_actions(selection.actions)
|
||||
|
||||
Reference in New Issue
Block a user