Work on item checking

This commit is contained in:
Matyáš Caras 2022-04-05 11:59:11 +02:00 committed by GitHub
parent efaf74111b
commit 111e918c93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 2 deletions

View file

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

View file

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

View file

@ -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}")