From 997475aef1a8fcae7f4422becc9478d6334553b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Wed, 6 Apr 2022 09:42:47 +0200 Subject: [PATCH] Hopefully item checking is complete --- games/example.yml | 5 ++++- lib/game.py | 26 ++++++++++++++++++-------- lib/lang/cz.yml | 1 + lib/lang/en.yml | 1 + lib/menu.py | 16 ++++++++++++++-- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/games/example.yml b/games/example.yml index 76cd6d2..8728446 100644 --- a/games/example.yml +++ b/games/example.yml @@ -21,12 +21,15 @@ game: # here goes all the game logic beer: description: "Order beer" text: "You order some &ebeer" - add_inventory: "Beer" # add something to inventory + add_item: "Beer" # add something to inventory actions: - do_something nothing: description: "Do nothing" text: "You sit and wait..." + actions: + - drink + - leave do_something: description: "Continue" text: "You start to feel bored." diff --git a/lib/game.py b/lib/game.py index 09ce019..05a3fc9 100644 --- a/lib/game.py +++ b/lib/game.py @@ -70,13 +70,22 @@ class Game: # the game class keeps information about the loaded game def print_text(self): # Prints out the current prompt system("cls||clear") + if "add_item" in self.nodes[self.current].keys(): # if there is an add_inventory key in the node, + # add item to inventory + item = self.nodes[self.current]['add_item'] + self.inventory.append(item) + print(self.inventory) + system("clear||cls") + print(f"{self.lang['acquire'].replace('$item',f'{Fore.CYAN}{item}{Fore.RESET}')}") + sleep(3) + system("clear||cls") animated = re.search(r"(?!{).+(?=})",self.nodes[self.current]["text"]) # find the animated text if(animated != None): self.print_animated(animated.group(0)) self.nodes[self.current]["text"] = self.nodes[self.current]["text"].replace("{"+animated.group(0)+"}","") # remove the animated text from the text prompt if("actions" in self.nodes[self.current].keys()): - actions_desc = [] - need_item = [] + actions_desc = [] # has descriptions of text prompts, so that we don't need to find them in MenuManager + need_item = [] # helps implement a check for needing an item for option in self.nodes[self.current]["actions"]: try: actions_desc.append(self.nodes[option]["description"]) @@ -88,15 +97,16 @@ class Game: # the game class keeps information about the loaded game print(f"{Back.RED}{Fore.WHITE}{self.lang['no_action'].replace('$action',option)}{Fore.RESET}") exit(1) m = "" - if((element == None for element in need_item) is False): + if(all(element == None for element in need_item) is False): # we need to check if user has item - m = HasItemDialogue(self.nodes[self.current]["actions"],self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item) + m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item) + print(self.inventory) + while need_item[m.selected] != None and all(element not in self.inventory for element in need_item[m.selected]): # until user selects an available prompt, re-prompt again + m = HasItemDialogue(actions_desc,self.parse_colors(self.nodes[self.current]["text"]),self.inventory,need_item) else: - m = MenuManager(self.nodes[self.current]["actions"],self.parse_colors(self.nodes[self.current]["text"])) + m = MenuManager(actions_desc,self.parse_colors(self.nodes[self.current]["text"])) sel = m.selected - if "add_item" in self.nodes[self.current]: # if there is an add_inventory key in the node, - # add item to inventory - self.inventory.append(self.nodes[self.current]["add_inventory"]) + self.current = self.nodes[self.current]["actions"][sel] self.save.currentPrompt = self.current # save the current prompt self.print_text() diff --git a/lib/lang/cz.yml b/lib/lang/cz.yml index 8c3d22e..fb6a532 100644 --- a/lib/lang/cz.yml +++ b/lib/lang/cz.yml @@ -9,6 +9,7 @@ quit: 'Vypnout' quitting: 'Vypínám' continue: 'Pokračovat' new_game: 'Nová hra' +acquire: 'Získal jsi $item' lang: 'Jazyk' back: 'Zpět' diff --git a/lib/lang/en.yml b/lib/lang/en.yml index c4c8615..c3aa17a 100644 --- a/lib/lang/en.yml +++ b/lib/lang/en.yml @@ -9,6 +9,7 @@ quit: 'Quit' quitting: 'Quitting' continue: 'Continue' new_game: 'New game' +acquire: 'You acquired $item' lang: 'Language' back: 'Back' diff --git a/lib/menu.py b/lib/menu.py index ae06ab8..f572008 100644 --- a/lib/menu.py +++ b/lib/menu.py @@ -35,6 +35,7 @@ class MenuManager: self.show_menu() def show_menu(self): + system("cls||clear") print(self.additional) for selection in self.selections: if(self.selected == self.selections.index(selection)): @@ -47,17 +48,26 @@ class HasItemDialogue(MenuManager): Custom handler for dialogue, that requires to check if the user has an item ''' def __init__(self, selections: list, additional: str,inv:list,need_item:list): + system("cls||clear") self.inventory = inv self.need_items = need_item super().__init__(selections, additional) def show_menu(self): print(self.additional) + for i,selection in enumerate(self.selections): - if(self.need_items[i] != None and self.need_items[i] not in self.inv): + if(self.need_items[i] != None and all(element not in self.inventory for element in self.need_items[i])): + c = "" + for i,item in enumerate(self.need_items[i]): + if item not in self.inventory: + if i == len(self.need_items)-self.need_items.count(None)-1: # last item, don't add a comma + c+=f"{item} " + else: + c+=f"{item}, " # user does not have the needed item if(self.selected == i): - print(f"{Fore.RED}-> {Fore.CYAN}{selection}{Fore.RESET} (Need '{self.need_items[i]}')") + print(f"{Fore.RED}-> {Fore.CYAN}{selection}{Fore.RESET} (Need {c})") else: print(f" {Fore.CYAN}{selection}{Fore.RESET}") else: @@ -66,3 +76,5 @@ class HasItemDialogue(MenuManager): print(f"{Fore.RED}->{Fore.RESET} {selection}") else: print(f" {selection}") + def make_selection(self) -> int: + keyboard.remove_all_hotkeys()