Hopefully item checking is complete

This commit is contained in:
Matyáš Caras 2022-04-06 09:42:47 +02:00 committed by GitHub
parent 111e918c93
commit 997475aef1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 11 deletions

View file

@ -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."

View file

@ -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()

View file

@ -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'

View file

@ -9,6 +9,7 @@ quit: 'Quit'
quitting: 'Quitting'
continue: 'Continue'
new_game: 'New game'
acquire: 'You acquired $item'
lang: 'Language'
back: 'Back'

View file

@ -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()