<s> """<STR_LIT>""" <EOL> import copy , sys <EOL> import pygame as pg <EOL> from . . import tools , collision <EOL> from . . import constants as c <EOL> from . . components import person , textbox , portal <EOL> from . import player_menu <EOL> from . . import tilerender <EOL> from . . import setup <EOL> if sys . version_info [ <NUM_LIT:0> ] == <NUM_LIT:2> : <EOL> range = xrange <EOL> class LevelState ( tools . _State ) : <EOL> def __init__ ( self , name , battles = False ) : <EOL> super ( LevelState , self ) . __init__ ( ) <EOL> self . name = name <EOL> self . tmx_map = setup . TMX [ name ] <EOL> self . allow_battles = battles <EOL> self . music_title = None <EOL> self . previous_music = None <EOL> self . music = None <EOL> self . volume = None <EOL> self . portal = None <EOL> def startup ( self , current_time , game_data ) : <EOL> """<STR_LIT>""" <EOL> self . game_data = game_data <EOL> self . music , self . volume = self . set_music ( ) <EOL> self . current_time = current_time <EOL> self . state = '<STR_LIT>' <EOL> self . reset_dialogue = ( ) <EOL> self . switch_to_battle = False <EOL> self . use_portal = False <EOL> self . allow_input = False <EOL> self . cut_off_bottom_map = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . renderer = tilerender . Renderer ( self . tmx_map ) <EOL> self . map_image = self . renderer . make_2x_map ( ) <EOL> self . viewport = self . make_viewport ( self . map_image ) <EOL> self . level_surface = self . make_level_surface ( self . map_image ) <EOL> self . level_rect = self . level_surface . get_rect ( ) <EOL> self . portals = self . make_level_portals ( ) <EOL> self . player = self . make_player ( ) <EOL> self . blockers = self . make_blockers ( ) <EOL> self . sprites = self . make_sprites ( ) <EOL> self . collision_handler = collision . CollisionHandler ( self . player , <EOL> self . blockers , <EOL> self . sprites , <EOL> self . portals , <EOL> self ) <EOL> self . dialogue_handler = textbox . TextHandler ( self ) <EOL> self . state_dict = self . make_state_dict ( ) <EOL> self . menu_screen = player_menu . Player_Menu ( game_data , self ) <EOL> self . transition_rect = setup . SCREEN . get_rect ( ) <EOL> self . transition_alpha = <NUM_LIT:255> <EOL> def set_music ( self ) : <EOL> """<STR_LIT>""" <EOL> music_dict = { c . TOWN : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . OVERWORLD : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . CASTLE : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . DUNGEON : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . DUNGEON2 : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . DUNGEON3 : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . DUNGEON4 : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . DUNGEON5 : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . HOUSE : ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> c . BROTHER_HOUSE : ( '<STR_LIT>' , <NUM_LIT> ) } <EOL> if self . game_data [ '<STR_LIT>' ] and ( self . name == c . TOWN or self . name == c . CASTLE ) : <EOL> self . music_title = '<STR_LIT>' <EOL> return setup . MUSIC [ '<STR_LIT>' ] , <NUM_LIT> <EOL> elif self . name in music_dict : <EOL> music = music_dict [ self . name ] [ <NUM_LIT:0> ] <EOL> volume = music_dict [ self . name ] [ <NUM_LIT:1> ] <EOL> self . music_title = music <EOL> return setup . MUSIC [ music ] , volume <EOL> else : <EOL> return None , None <EOL> def make_viewport ( self , map_image ) : <EOL> """<STR_LIT>""" <EOL> map_rect = map_image . get_rect ( ) <EOL> return setup . SCREEN . get_rect ( bottom = map_rect . bottom ) <EOL> def make_level_surface ( self , map_image ) : <EOL> """<STR_LIT>""" <EOL> map_rect = map_image . get_rect ( ) <EOL> map_width = map_rect . width <EOL> if self . name in self . cut_off_bottom_map : <EOL> map_height = map_rect . height - <NUM_LIT:32> <EOL> else : <EOL> map_height = map_rect . height <EOL> size = map_width , map_height <EOL> return pg . Surface ( size ) . convert ( ) <EOL> def make_player ( self ) : <EOL> """<STR_LIT>""" <EOL> last_state = self . previous <EOL> if last_state == '<STR_LIT>' : <EOL> player = person . Player ( self . game_data [ '<STR_LIT>' ] , self . game_data ) <EOL> player . rect . x = self . game_data [ '<STR_LIT>' ] [ <NUM_LIT:0> ] * <NUM_LIT:32> <EOL> player . rect . y = self . game_data [ '<STR_LIT>' ] [ <NUM_LIT:1> ] * <NUM_LIT:32> <EOL> else : <EOL> for object in self . renderer . tmx_data . getObjects ( ) : <EOL> properties = object . __dict__ <EOL> if properties [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> if last_state == properties [ '<STR_LIT:state>' ] : <EOL> posx = properties [ '<STR_LIT:x>' ] * <NUM_LIT:2> <EOL> posy = ( properties [ '<STR_LIT:y>' ] * <NUM_LIT:2> ) - <NUM_LIT:32> <EOL> player = person . Player ( properties [ '<STR_LIT>' ] , <EOL> self . game_data ) <EOL> player . rect . x = posx <EOL> player . rect . y = posy <EOL> return player <EOL> def make_blockers ( self ) : <EOL> """<STR_LIT>""" <EOL> blockers = [ ] <EOL> for object in self . renderer . tmx_data . getObjects ( ) : <EOL> properties = object . __dict__ <EOL> if properties [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> left = properties [ '<STR_LIT:x>' ] * <NUM_LIT:2> <EOL> top = ( ( properties [ '<STR_LIT:y>' ] ) * <NUM_LIT:2> ) - <NUM_LIT:32> <EOL> blocker = pg . Rect ( left , top , <NUM_LIT:32> , <NUM_LIT:32> ) <EOL> blockers . append ( blocker ) <EOL> return blockers <EOL> def make_sprites ( self ) : <EOL> """<STR_LIT>""" <EOL> sprites = pg . sprite . Group ( ) <EOL> for object in self . renderer . tmx_data . getObjects ( ) : <EOL> properties = object . __dict__ <EOL> if properties [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> if '<STR_LIT>' in properties : <EOL> direction = properties [ '<STR_LIT>' ] <EOL> else : <EOL> direction = '<STR_LIT>' <EOL> if properties [ '<STR_LIT:type>' ] == '<STR_LIT>' and direction == '<STR_LIT:left>' : <EOL> index = <NUM_LIT:1> <EOL> else : <EOL> index = <NUM_LIT:0> <EOL> if '<STR_LIT>' in properties : <EOL> item = properties [ '<STR_LIT>' ] <EOL> else : <EOL> item = None <EOL> if '<STR_LIT:id>' in properties : <EOL> id = properties [ '<STR_LIT:id>' ] <EOL> else : <EOL> id = None <EOL> if '<STR_LIT>' in properties : <EOL> battle = properties [ '<STR_LIT>' ] <EOL> else : <EOL> battle = None <EOL> if '<STR_LIT:state>' in properties : <EOL> sprite_state = properties [ '<STR_LIT:state>' ] <EOL> else : <EOL> sprite_state = None <EOL> x = properties [ '<STR_LIT:x>' ] * <NUM_LIT:2> <EOL> y = ( ( properties [ '<STR_LIT:y>' ] ) * <NUM_LIT:2> ) - <NUM_LIT:32> <EOL> sprite_dict = { '<STR_LIT>' : person . Person ( '<STR_LIT>' , <EOL> x , y , direction ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , <EOL> x , y , direction , <EOL> '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , <EOL> x , y , direction , <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , x , y , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , <EOL> x , y , direction ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , <EOL> x , y , direction , <EOL> '<STR_LIT>' , index ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , x , y , direction ) , <EOL> '<STR_LIT>' : person . Person ( '<STR_LIT>' , x , y , direction ) , <EOL> '<STR_LIT>' : person . Chest ( x , y , id ) } <EOL> sprite = sprite_dict [ properties [ '<STR_LIT:type>' ] ] <EOL> if sprite_state : <EOL> sprite . state = sprite_state <EOL> if sprite . name == '<STR_LIT>' : <EOL> if self . game_data [ '<STR_LIT>' ] and not self . game_data [ '<STR_LIT>' ] : <EOL> sprite . item = self . game_data [ '<STR_LIT>' ] <EOL> else : <EOL> sprite . item = item <EOL> elif sprite . name == '<STR_LIT>' : <EOL> if not self . game_data [ '<STR_LIT>' ] : <EOL> sprite . item = self . game_data [ '<STR_LIT>' ] <EOL> else : <EOL> sprite . item = item <EOL> sprite . battle = battle <EOL> self . assign_dialogue ( sprite , properties ) <EOL> self . check_for_opened_chest ( sprite ) <EOL> if sprite . name == '<STR_LIT>' and self . game_data [ '<STR_LIT>' ] : <EOL> pass <EOL> else : <EOL> sprites . add ( sprite ) <EOL> return sprites <EOL> def assign_dialogue ( self , sprite , property_dict ) : <EOL> """<STR_LIT>""" <EOL> dialogue_list = [ ] <EOL> for i in range ( int ( property_dict [ '<STR_LIT>' ] ) ) : <EOL> dialogue_list . append ( property_dict [ '<STR_LIT>' + str ( i ) ] ) <EOL> sprite . dialogue = dialogue_list <EOL> if sprite . name == '<STR_LIT>' : <EOL> quest_in_process_dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> if self . game_data [ '<STR_LIT>' ] : <EOL> if self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> else : <EOL> sprite . dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> elif self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = quest_in_process_dialogue <EOL> elif not self . game_data [ '<STR_LIT>' ] : <EOL> self . reset_dialogue = ( sprite , quest_in_process_dialogue ) <EOL> elif sprite . name == '<STR_LIT>' : <EOL> if self . game_data [ '<STR_LIT>' ] : <EOL> if self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> else : <EOL> sprite . dialogue = [ '<STR_LIT>' ] <EOL> elif self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> elif sprite . name == '<STR_LIT>' : <EOL> retrieved_crown_dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> thank_you_dialogue = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> if self . game_data [ '<STR_LIT>' ] and not self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = retrieved_crown_dialogue <EOL> self . reset_dialogue = ( sprite , thank_you_dialogue ) <EOL> elif self . game_data [ '<STR_LIT>' ] : <EOL> sprite . dialogue = thank_you_dialogue <EOL> def check_for_opened_chest ( self , sprite ) : <EOL> if sprite . name == '<STR_LIT>' : <EOL> if not self . game_data [ '<STR_LIT>' . format ( sprite . id ) ] : <EOL> sprite . dialogue = [ '<STR_LIT>' ] <EOL> sprite . item = None <EOL> sprite . index = <NUM_LIT:1> <EOL> def make_state_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> state_dict = { '<STR_LIT>' : self . running_normally , <EOL> '<STR_LIT>' : self . handling_dialogue , <EOL> '<STR_LIT>' : self . goto_menu , <EOL> '<STR_LIT>' : self . transition_in , <EOL> '<STR_LIT>' : self . transition_out , <EOL> '<STR_LIT>' : self . slow_fade_out } <EOL> return state_dict <EOL> def make_level_portals ( self ) : <EOL> """<STR_LIT>""" <EOL> portal_group = pg . sprite . Group ( ) <EOL> for object in self . renderer . tmx_data . getObjects ( ) : <EOL> properties = object . __dict__ <EOL> if properties [ '<STR_LIT:name>' ] == '<STR_LIT>' : <EOL> posx = properties [ '<STR_LIT:x>' ] * <NUM_LIT:2> <EOL> posy = ( properties [ '<STR_LIT:y>' ] * <NUM_LIT:2> ) - <NUM_LIT:32> <EOL> new_state = properties [ '<STR_LIT:type>' ] <EOL> portal_group . add ( portal . Portal ( posx , posy , new_state ) ) <EOL> return portal_group <EOL> def running_normally ( self , surface , keys , current_time ) : <EOL> """<STR_LIT>""" <EOL> self . check_for_dialogue ( ) <EOL> self . player . update ( keys , current_time ) <EOL> self . sprites . update ( current_time ) <EOL> self . collision_handler . update ( keys , current_time ) <EOL> self . check_for_battle ( ) <EOL> self . check_for_portals ( ) <EOL> self . check_for_end_of_game ( ) <EOL> self . dialogue_handler . update ( keys , current_time ) <EOL> self . check_for_menu ( keys ) <EOL> self . viewport_update ( ) <EOL> self . draw_level ( surface ) <EOL> def check_for_portals ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . use_portal and not self . done : <EOL> self . player . location = self . player . get_tile_location ( ) <EOL> self . update_game_data ( ) <EOL> self . next = self . portal <EOL> self . state = '<STR_LIT>' <EOL> def check_for_battle ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . switch_to_battle and self . allow_battles and not self . done : <EOL> self . player . location = self . player . get_tile_location ( ) <EOL> self . update_game_data ( ) <EOL> self . next = '<STR_LIT>' <EOL> self . state = '<STR_LIT>' <EOL> def check_for_menu ( self , keys ) : <EOL> """<STR_LIT>""" <EOL> if keys [ pg . K_RETURN ] and self . allow_input : <EOL> if self . player . state == '<STR_LIT>' : <EOL> self . state = '<STR_LIT>' <EOL> self . allow_input = False <EOL> if not keys [ pg . K_RETURN ] : <EOL> self . allow_input = True <EOL> def update_game_data ( self ) : <EOL> """<STR_LIT>""" <EOL> self . game_data [ '<STR_LIT>' ] = self . player . location <EOL> self . game_data [ '<STR_LIT>' ] = self . player . direction <EOL> self . game_data [ '<STR_LIT>' ] = self . name <EOL> self . set_new_start_pos ( ) <EOL> def check_for_end_of_game ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . game_data [ '<STR_LIT>' ] : <EOL> self . next = c . CREDITS <EOL> self . state = '<STR_LIT>' <EOL> def set_new_start_pos ( self ) : <EOL> """<STR_LIT>""" <EOL> location = copy . deepcopy ( self . game_data [ '<STR_LIT>' ] ) <EOL> direction = self . game_data [ '<STR_LIT>' ] <EOL> if self . next == '<STR_LIT>' : <EOL> pass <EOL> elif direction == '<STR_LIT>' : <EOL> location [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> elif direction == '<STR_LIT>' : <EOL> location [ <NUM_LIT:1> ] -= <NUM_LIT:1> <EOL> elif direction == '<STR_LIT:left>' : <EOL> location [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> elif direction == '<STR_LIT:right>' : <EOL> location [ <NUM_LIT:0> ] -= <NUM_LIT:1> <EOL> def handling_dialogue ( self , surface , keys , current_time ) : <EOL> """<STR_LIT>""" <EOL> self . dialogue_handler . update ( keys , current_time ) <EOL> self . draw_level ( surface ) <EOL> def goto_menu ( self , surface , keys , * args ) : <EOL> """<STR_LIT>""" <EOL> self . menu_screen . update ( surface , keys ) <EOL> self . menu_screen . draw ( surface ) <EOL> def check_for_dialogue ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . dialogue_handler . textbox : <EOL> self . state = '<STR_LIT>' <EOL> def transition_out ( self , surface , * args ) : <EOL> """<STR_LIT>""" <EOL> transition_image = pg . Surface ( self . transition_rect . size ) <EOL> transition_image . fill ( c . TRANSITION_COLOR ) <EOL> transition_image . set_alpha ( self . transition_alpha ) <EOL> self . draw_level ( surface ) <EOL> surface . blit ( transition_image , self . transition_rect ) <EOL> self . transition_alpha += c . TRANSITION_SPEED <EOL> if self . transition_alpha >= <NUM_LIT:255> : <EOL> self . transition_alpha = <NUM_LIT:255> <EOL> self . done = True <EOL> def slow_fade_out ( self , surface , * args ) : <EOL> """<STR_LIT>""" <EOL> transition_image = pg . Surface ( self . transition_rect . size ) <EOL> transition_image . fill ( c . TRANSITION_COLOR ) <EOL> transition_image . set_alpha ( self . transition_alpha ) <EOL> self . draw_level ( surface ) <EOL> surface . blit ( transition_image , self . transition_rect ) <EOL> self . transition_alpha += <NUM_LIT:2> <EOL> if self . transition_alpha >= <NUM_LIT:255> : <EOL> self . transition_alpha = <NUM_LIT:255> <EOL> self . done = True <EOL> def transition_in ( self , surface , * args ) : <EOL> """<STR_LIT>""" <EOL> self . viewport_update ( ) <EOL> transition_image = pg . Surface ( self . transition_rect . size ) <EOL> transition_image . fill ( c . TRANSITION_COLOR ) <EOL> transition_image . set_alpha ( self . transition_alpha ) <EOL> self . draw_level ( surface ) <EOL> surface . blit ( transition_image , self . transition_rect ) <EOL> self . transition_alpha -= c . TRANSITION_SPEED <EOL> if self . transition_alpha <= <NUM_LIT:0> : <EOL> self . state = '<STR_LIT>' <EOL> self . transition_alpha = <NUM_LIT:0> <EOL> def update ( self , surface , keys , current_time ) : <EOL> """<STR_LIT>""" <EOL> state_function = self . state_dict [ self . state ] <EOL> state_function ( surface , keys , current_time ) <EOL> def viewport_update ( self ) : <EOL> """<STR_LIT>""" <EOL> self . viewport . center = self . player . rect . center <EOL> self . viewport . clamp_ip ( self . level_rect ) <EOL> def draw_level ( self , surface ) : <EOL> """<STR_LIT>""" <EOL> self . level_surface . blit ( self . map_image , self . viewport , self . viewport ) <EOL> self . level_surface . blit ( self . player . image , self . player . rect ) <EOL> self . sprites . draw ( self . level_surface ) <EOL> surface . blit ( self . level_surface , ( <NUM_LIT:0> , <NUM_LIT:0> ) , self . viewport ) <EOL> self . dialogue_handler . draw ( surface ) </s>
