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,4 +1,7 @@
|
||||
import platform
|
||||
from typing import Optional, Sequence, Tuple
|
||||
from agame.action import Action
|
||||
from agame.dialog import DialogOption
|
||||
from .display import Display
|
||||
|
||||
if platform.system() in ("Linux", "Darwin", "Unix"):
|
||||
@@ -14,5 +17,31 @@ class BasicDisplay(Display):
|
||||
def line(self, line: str = ""):
|
||||
print(line)
|
||||
|
||||
def input(self) -> str:
|
||||
return input("> ")
|
||||
def input(self, prompt: Optional[str] = None) -> str:
|
||||
if prompt is None:
|
||||
prompt = "> "
|
||||
return input(prompt)
|
||||
|
||||
def wait_for_ack(self, prompt: Optional[str] = "Press enter to continue..."):
|
||||
input(prompt or "")
|
||||
|
||||
def dialog_options(self, options: Sequence[DialogOption]) -> DialogOption:
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user