From 111e918c937f249eddfdc4fe034217d75991ca2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Tue, 5 Apr 2022 11:59:11 +0200 Subject: [PATCH] Work on item checking --- games/example.yml | 17 +++++++++++++++++ lib/game.py | 14 ++++++++++++-- lib/menu.py | 25 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/games/example.yml b/games/example.yml index 80932f4..76cd6d2 100644 --- a/games/example.yml +++ b/games/example.yml @@ -22,7 +22,24 @@ game: # here goes all the game logic description: "Order beer" text: "You order some &ebeer" add_inventory: "Beer" # add something to inventory + actions: + - do_something nothing: description: "Do nothing" text: "You sit and wait..." + do_something: + description: "Continue" + text: "You start to feel bored." + actions: + - drink + - leave + drink: + has_item: ["Beer"] # item names are case-sensitive + description: "Drink beer" + text: "You take a sip of your cold &eBeer" + actions: + - leave + leave: + description: "Leave" + text: "You decide to leave." diff --git a/lib/game.py b/lib/game.py index a3172e4..09ce019 100644 --- a/lib/game.py +++ b/lib/game.py @@ -3,7 +3,7 @@ from yaml.loader import SafeLoader from colorama import Fore, Back import re -from lib.menu import MenuManager +from lib.menu import HasItemDialogue, MenuManager from .save import SaveManager from .ascii import AsciiAnimation from time import sleep @@ -76,13 +76,23 @@ class Game: # the game class keeps information about the loaded game 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 = [] for option in self.nodes[self.current]["actions"]: try: actions_desc.append(self.nodes[option]["description"]) + if "has_item" in self.nodes[option].keys(): + need_item.append(self.nodes[option]["has_item"]) + else: + need_item.append(None) except: print(f"{Back.RED}{Fore.WHITE}{self.lang['no_action'].replace('$action',option)}{Fore.RESET}") exit(1) - m = MenuManager(actions_desc,self.parse_colors(self.nodes[self.current]["text"])) + m = "" + if((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) + else: + m = MenuManager(self.nodes[self.current]["actions"],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 diff --git a/lib/menu.py b/lib/menu.py index fe224e6..ae06ab8 100644 --- a/lib/menu.py +++ b/lib/menu.py @@ -41,3 +41,28 @@ class MenuManager: print(f"{Fore.RED}->{Fore.RESET} {selection}") else: print(f" {selection}") + +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): + 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): + # 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]}')") + else: + print(f" {Fore.CYAN}{selection}{Fore.RESET}") + else: + # we don't need to change color for an item user doesn't have + if(self.selected == i): + print(f"{Fore.RED}->{Fore.RESET} {selection}") + else: + print(f" {selection}")