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:
2021-11-28 17:27:05 -08:00
parent def9b584a8
commit 2357757b23
5 changed files with 173 additions and 13 deletions

View File

@@ -1,5 +1,10 @@
import abc
from typing import Optional
from typing import Optional, Sequence, Tuple, TYPE_CHECKING
from agame.action import Action
from agame.dialog import DialogOption
if TYPE_CHECKING:
from agame.game import Game
class Display(metaclass=abc.ABCMeta):
@@ -14,7 +19,7 @@ class Display(metaclass=abc.ABCMeta):
"Print a single line to the display."
@abc.abstractmethod
def input(self) -> str:
def input(self, prompt: Optional[str] = None) -> str:
"Get player input."
@abc.abstractmethod
@@ -28,5 +33,31 @@ class Display(metaclass=abc.ABCMeta):
user.
"""
def dialog_options(self, options: Sequence[DialogOption]) -> DialogOption:
"""
Displays, and gets, an answer from a list of dialog options.
Returns the selected DialogOption.next value and actions.
"""
valid_opts = range(1, len(options) + 1)
def print_opts():
for i, opt in enumerate(options):
self.line(f"{i}. {opt.text}")
print_opts()
while True:
got = self.input("? ")
try:
choice = int(got)
except ValueError:
print_opts()
continue
if choice in valid_opts:
break
index = choice - 1
selection = options[index]
return selection
def finish(self):
"Final cleanup code for this display."