2021-11-02 17:40:54 +01:00
import yaml
from yaml . loader import SafeLoader
2022-03-24 18:20:39 +01:00
from colorama import Fore , Back
2022-01-25 17:16:07 +01:00
import re
2022-03-24 18:20:39 +01:00
2022-04-05 11:59:11 +02:00
from lib . menu import HasItemDialogue , MenuManager
2022-02-04 19:35:38 +01:00
from . save import SaveManager
2022-01-26 09:23:30 +01:00
from . ascii import AsciiAnimation
2022-01-25 17:16:07 +01:00
from time import sleep
2022-03-24 18:20:39 +01:00
from os import system
2021-11-02 17:40:54 +01:00
2022-02-04 19:35:38 +01:00
class Game : # the game class keeps information about the loaded game
2021-11-26 22:13:30 +01:00
def __init__ ( self , data : dict ) :
self . name = data [ " meta " ] [ " name " ]
self . author = data [ " meta " ] [ " creator " ]
2021-11-02 17:40:54 +01:00
self . current = " start "
self . nodes = { }
2022-01-26 16:07:31 +01:00
self . inventory = [ ]
2022-02-04 19:35:38 +01:00
self . id = data [ " meta " ] [ " id " ]
2022-04-17 20:03:52 +02:00
self . save = SaveManager ( self . id )
2021-11-26 22:13:30 +01:00
for k in data [ " game " ] :
self . nodes . update ( { k : data [ " game " ] [ k ] } )
2021-11-02 17:40:54 +01:00
2022-02-04 19:35:38 +01:00
def main_menu ( self ) : # displays the main menu
l = self . save . load ( )
if not l :
# New game
2022-03-24 18:20:39 +01:00
m = MenuManager ( [ self . lang [ ' start ' ] , self . lang [ ' options ' ] , self . lang [ ' quit ' ] ] , f " { self . name } \n { self . lang [ ' game_by ' ] . replace ( ' $author ' , self . author ) } " )
selection = m . selected
2022-02-23 19:28:50 +01:00
system ( " cls||clear " )
2022-03-24 18:20:39 +01:00
if ( selection == 0 ) : # start new game
2022-02-23 19:28:50 +01:00
self . print_text ( )
2022-03-24 18:20:39 +01:00
elif ( selection == 1 ) :
2022-02-23 19:28:50 +01:00
self . settings_menu ( )
2022-03-24 18:20:39 +01:00
elif ( selection == 2 ) :
2022-03-23 14:22:12 +01:00
print ( self . lang [ ' quitting ' ] )
2022-02-23 19:28:50 +01:00
exit ( )
2022-02-04 19:35:38 +01:00
else : # Display continue
2022-04-17 20:03:52 +02:00
m = MenuManager ( [ self . lang [ ' continue ' ] , self . lang [ ' new_game ' ] , self . lang [ ' options ' ] , self . lang [ ' quit ' ] ] , f " { self . name } \n { self . lang [ ' game_by ' ] . replace ( ' $author ' , self . author ) } " )
2022-03-24 18:20:39 +01:00
selection = m . selected
2022-02-04 19:35:38 +01:00
system ( " cls||clear " )
2022-03-24 18:20:39 +01:00
if ( selection == 0 ) :
2022-02-04 19:35:38 +01:00
self . current = self . save . currentPrompt
self . inventory = self . save . inventory
self . print_text ( )
2022-03-24 18:20:39 +01:00
elif ( selection == 1 ) :
2022-02-04 19:35:38 +01:00
self . print_text ( )
2022-03-24 18:20:39 +01:00
elif ( selection == 2 ) :
2022-02-23 19:28:50 +01:00
self . settings_menu ( )
2022-03-24 18:20:39 +01:00
elif ( selection == 3 ) :
print ( self . lang [ ' quitting ' ] )
2022-02-04 19:35:38 +01:00
exit ( )
def settings_menu ( self ) : # displays the settings menu
2022-03-24 18:20:39 +01:00
m = MenuManager ( [ self . lang [ ' lang ' ] , self . lang [ ' back ' ] ] , self . lang [ ' options ' ] )
selection = m . selected
if ( selection == 0 ) :
m = MenuManager ( [ " English " , " Česky " , self . lang [ ' back ' ] ] , self . lang [ ' lang ' ] )
selection = m . selected
2022-02-04 19:35:38 +01:00
system ( " cls||clear " )
2022-03-24 18:20:39 +01:00
if ( selection == 0 ) :
2022-02-04 19:35:38 +01:00
with open ( " ./saves/lang " , " w " ) as f :
f . write ( " en " )
2022-03-24 18:20:39 +01:00
elif ( selection == 1 ) :
2022-02-04 19:35:38 +01:00
with open ( " ./saves/lang " , " w " ) as f :
f . write ( " cz " )
self . settings_menu ( )
2022-02-23 19:28:50 +01:00
else :
self . main_menu ( )
2021-11-02 17:40:54 +01:00
2022-02-04 19:35:38 +01:00
def print_text ( self ) : # Prints out the current prompt
2022-02-23 19:28:50 +01:00
system ( " cls||clear " )
2022-04-06 09:42:47 +02:00
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 ' ]
2022-04-06 10:03:21 +02:00
if item not in self . inventory :
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 " )
2022-01-25 17:16:07 +01:00
animated = re . search ( r " (?! { ).+(?=}) " , self . nodes [ self . current ] [ " text " ] ) # find the animated text
if ( animated != None ) :
self . print_animated ( animated . group ( 0 ) )
2022-01-25 17:30:09 +01:00
self . nodes [ self . current ] [ " text " ] = self . nodes [ self . current ] [ " text " ] . replace ( " { " + animated . group ( 0 ) + " } " , " " ) # remove the animated text from the text prompt
2021-11-26 22:13:30 +01:00
if ( " actions " in self . nodes [ self . current ] . keys ( ) ) :
2022-04-06 09:42:47 +02:00
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
2022-03-24 18:20:39 +01:00
for option in self . nodes [ self . current ] [ " actions " ] :
try :
actions_desc . append ( self . nodes [ option ] [ " description " ] )
2022-04-05 11:59:11 +02:00
if " has_item " in self . nodes [ option ] . keys ( ) :
need_item . append ( self . nodes [ option ] [ " has_item " ] )
else :
need_item . append ( None )
2022-04-06 10:03:21 +02:00
except Exception :
2022-03-24 18:20:39 +01:00
print ( f " { Back . RED } { Fore . WHITE } { self . lang [ ' no_action ' ] . replace ( ' $action ' , option ) } { Fore . RESET } " )
exit ( 1 )
2022-04-05 11:59:11 +02:00
m = " "
2022-04-06 10:03:21 +02:00
actions_desc . extend ( [ self . lang [ ' inventory ' ] , self . lang [ ' quit ' ] ] )
2022-04-06 09:42:47 +02:00
if ( all ( element == None for element in need_item ) is False ) :
2022-04-06 10:03:21 +02:00
need_item . extend ( [ None , None ] )
2022-04-05 11:59:11 +02:00
# we need to check if user has item
2022-04-06 09:42:47 +02:00
m = HasItemDialogue ( actions_desc , self . parse_colors ( self . nodes [ self . current ] [ " text " ] ) , self . inventory , need_item )
2022-04-06 10:03:21 +02:00
# TODO: Remove item from inventory after using it?
2022-04-06 09:42:47 +02:00
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 )
2022-04-17 19:02:11 +02:00
if m . selected < = len ( actions_desc ) - 3 and " has_item " in self . nodes [ self . nodes [ self . current ] [ " actions " ] [ m . selected ] ] . keys ( ) :
2022-04-12 15:16:38 +02:00
for item in need_item [ m . selected ] :
self . inventory . remove ( item )
2022-04-05 11:59:11 +02:00
else :
2022-04-06 09:42:47 +02:00
m = MenuManager ( actions_desc , self . parse_colors ( self . nodes [ self . current ] [ " text " ] ) )
2022-03-24 18:20:39 +01:00
sel = m . selected
2022-04-06 10:03:21 +02:00
if ( sel == len ( actions_desc ) - 2 ) : # show inventory
self . show_inventory ( )
elif ( sel == len ( actions_desc ) - 1 ) : # Save & quit
self . save . currentPrompt = self . current # save the current prompt
self . save . inventory = self . inventory
self . save . save ( )
exit ( 0 )
else :
self . current = self . nodes [ self . current ] [ " actions " ] [ sel ]
self . print_text ( )
2022-03-24 18:20:39 +01:00
else :
print ( self . parse_colors ( self . nodes [ self . current ] [ " text " ] ) )
print ( " " )
2022-01-25 17:16:07 +01:00
2022-04-06 10:03:21 +02:00
def show_inventory ( self ) :
if len ( self . inventory ) == 0 :
MenuManager ( [ self . lang [ " return " ] ] , f " YOUR INVENTORY \n " )
else :
s = " "
for i , item in enumerate ( self . inventory ) :
if ( i == len ( self . inventory ) ) : # last item
s + = f " - { item } "
else :
s + = f " - { item } \n "
MenuManager ( [ self . lang [ " return " ] ] , f " YOUR INVENTORY \n { s } " )
self . print_text ( )
2022-03-24 18:20:39 +01:00
def print_animated ( self , animid ) : # prints the first found occurence of an ascii animation
2022-01-26 09:23:30 +01:00
animation = AsciiAnimation ( )
animation . load_ascii ( animid )
2022-04-17 20:03:52 +02:00
animation . play ( )
2022-01-25 17:30:09 +01:00
print ( )
2022-01-25 17:16:07 +01:00
2022-02-04 19:35:38 +01:00
def parse_colors ( self , text : str ) - > str : # Converts color codes into terminal colors
2021-12-02 11:20:39 +01:00
newText = text . replace ( " &b " , Fore . CYAN ) . replace ( " &c " , Fore . RED ) . replace ( " &e " , Fore . YELLOW ) . replace ( " &a " , Fore . GREEN ) . replace ( " &9 " , Fore . BLUE ) . replace ( " &r " , Fore . RESET ) . replace ( " &f " , Fore . WHITE ) . replace ( " &5 " , Fore . MAGENTA ) . replace ( " \n " , Fore . RESET + " \n " ) # replace color codes and newlines with colorama
2021-11-26 22:13:30 +01:00
newText + = Fore . RESET # reset color at the end of the text
return newText
2021-11-03 10:23:37 +01:00
2022-03-23 14:22:12 +01:00
def load ( file_path , lang ) : # starts to load the game from YAML
2021-11-26 22:13:30 +01:00
try :
with open ( file_path ) as f :
data = yaml . load ( f , Loader = SafeLoader )
g = Game ( data )
2022-02-04 19:35:38 +01:00
g . lang = lang
2021-11-26 22:13:30 +01:00
return g
except Exception as e :
2022-03-23 14:22:12 +01:00
print ( f " { Back . RED } { Fore . WHITE } { g . lang [ ' error_loading ' ] } { Fore . RESET } { Back . RESET } " )
2021-11-26 22:13:30 +01:00
print ( e )
2022-03-23 14:22:12 +01:00
return None