Hopefully item checking is complete
This commit is contained in:
parent
111e918c93
commit
997475aef1
5 changed files with 38 additions and 11 deletions
|
@ -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."
|
||||
|
|
26
lib/game.py
26
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()
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -9,6 +9,7 @@ quit: 'Quit'
|
|||
quitting: 'Quitting'
|
||||
continue: 'Continue'
|
||||
new_game: 'New game'
|
||||
acquire: 'You acquired $item'
|
||||
|
||||
lang: 'Language'
|
||||
back: 'Back'
|
||||
|
|
16
lib/menu.py
16
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()
|
||||
|
|
Reference in a new issue