Esempio n. 1
0
def handle_events():
    global loaded_indicator
    global entry_box
    global network_message_notifications
    
    if not loaded_indicator:
        load()
    
    if entry_box.visible:
        if pygame.K_ESCAPE in wotsuievents.keys_pressed:
            close_entry_box()
        
        elif pygame.K_RETURN in wotsuievents.keys_pressed:
            
            if versusclient.connected():
                versusclient.send_chat_message(entry_box.get_message())
            
            close_entry_box()
        
        else:
            entry_box.handle_events()    
        
    else:
        if pygame.K_t in wotsuievents.keys_pressed:
            entry_box.show()
    
    if versusclient.connected():
        remove_expired_network_message_notifications()
        get_new_network_message_notifications()
        layout_network_message_notifications()
        
        time_passed = gamestate.clock.get_time()
        
        for notification in network_message_notifications:
            
            if gamestate.drawing_mode == gamestate.DrawingModes.DIRTY_RECTS:
                gamestate.new_dirty_rects.append(
                    pygame.Rect(notification.position,
                    (notification.width, notification.height))
                )
            
            notification.draw(gamestate.screen)
            notification.update(time_passed)
    
    if gamestate.drawing_mode == gamestate.DrawingModes.DIRTY_RECTS:
        gamestate.new_dirty_rects.append(
            pygame.Rect(entry_box.position,
            (entry_box.width, entry_box.height))
        )
    
    entry_box.draw(gamestate.screen)
def unload():
    global loaded
    global exit_button
    global start_match_label
    global ip_address_input
    global player_status_ui_dictionary
    global join_match_button
    global assigned_positions
    global spectate_button
    global local_player_position
    global local_player_container_created
    
    exit_button = None
    loaded = False
    start_match_label = None
    network_message_label = None
    join_match_button = None
    assigned_positions = None
    spectate_button = None
    player_status_ui_dictionary = None
    local_player_position = None
    local_player_container_created = False
    
    if versusclient.connected():
        unregister_network_callbacks()
        
        #clean up any remaining messages to the client
        versusclient.get_network_messages()
        versusclient.listener.Pump()
        
        versusclient.listener.close()
        versusclient.unload()
        print("listener closed")
    
    if gamestate.hosting:
        #clean up any remaining messages to the server
        versusserver.server.Pump()
        
        versusserver.server.close()
        versusserver.server = None
        print("server closed")
        
        versusserver.broadcast.close()
        versusserver.broadcast = None
        print("server broadcasting closed")
        
        gamestate.hosting = False
def handle_events():
    global loaded
    global exit_button
    global start_match_label
    global player_status_ui_dictionary
    global join_match_button
    global local_player_container_created
    global local_player_position
    
    if loaded == False:
        load()
    
    if pygame.MOUSEBUTTONDOWN in wotsuievents.event_types:
        if exit_button.contains(wotsuievents.mouse_pos):
            exit_button.handle_selected()
        
        if start_match_label.active:
            if start_match_label.contains(wotsuievents.mouse_pos):
                start_match_label.handle_selected()
        
        if join_match_button.active:
            if join_match_button.contains(wotsuievents.mouse_pos):
                join_match_button.handle_selected()
        
        if spectate_button.active:
            if spectate_button.contains(wotsuievents.mouse_pos):
                spectate_button.handle_selected()
        
    if pygame.MOUSEBUTTONUP in wotsuievents.event_types:
        if exit_button.selected:
            exit_button.handle_deselected()
            
            if exit_button.contains(wotsuievents.mouse_pos):
                gamestate.mode = gamestate.Modes.ONLINEMENUPAGE
                unload()
        
        elif start_match_label.selected:
            if start_match_label.contains(wotsuievents.mouse_pos):
                start_match_label.handle_deselected()
                versusclient.listener.load_match_data()
        
        #TODO - inactivate join if selected and same for spectate
        elif join_match_button.selected:
            if join_match_button.contains(wotsuievents.mouse_pos):
                versusclient.listener.join_match()
                join_match_button.handle_deselected()
                join_match_button.inactivate()
                
                spectate_button.activate()
        
        elif spectate_button.selected:
            if spectate_button.contains(wotsuievents.mouse_pos):
                versusclient.listener.spectate_match()
                spectate_button.handle_deselected()
                spectate_button.inactivate()
                join_match_button.activate()
    
    if loaded:
        
        if versusclient.dummies_only() and start_match_label.active == False:
            start_match_label.activate()
            
        
        if local_player_container_created:
            players_ready = True
            
            local_player_position = versusclient.get_local_player_position()
            
            if player_status_ui_dictionary[local_player_position].player_ready() and \
            not versusclient.listener.player_positions_ready_dictionary[local_player_position]:
                
                versusclient.listener.player_ready()
            
            for player_status_ui in player_status_ui_dictionary.values():
                if hasattr(player_status_ui, "player_ready"):
                    players_ready = (getattr(player_status_ui, "player_ready")() and players_ready)
                else:
                    players_ready = False
            
            if players_ready:
                if start_match_label.active == False:
                    start_match_label.activate()
            else:
                if start_match_label.active:
                    start_match_label.inactivate()
        
        for player_status_ui in player_status_ui_dictionary.values():
            player_status_ui.handle_events()
            player_status_ui.draw(gamestate.screen)
        
        join_match_button.draw(gamestate.screen)
        exit_button.draw(gamestate.screen)
        start_match_label.draw(gamestate.screen)
        spectate_button.draw(gamestate.screen)
        
        #Network Dependent Event Handling
        if (versusclient.listener.connection_status == 
        versusclient.ConnectionStatus.DISCONNECTED):
            
            unload()
            gamestate.mode = gamestate.Modes.ONLINEMENUPAGE
        
        if versusclient.connected() or gamestate.hosting:
            if not versusclient.local_player_is_in_match():
                if not join_match_button.active:
                    join_match_button.activate()
                
                if spectate_button.active:
                    spectate_button.inactivate()
                
            else:
                
                if not spectate_button.active:
                    spectate_button.activate()
                
                if join_match_button.active:
                    join_match_button.inactivate()
            
            update_player_data()
            
            if (versusclient.listener.server_mode == 
            versusserver.ServerModes.LOADING_MATCH_DATA) or \
            (versusclient.listener.server_mode == versusserver.ServerModes.MATCH):
                gamestate.mode = gamestate.Modes.ONLINEMATCHLOADER
            
            versusclient.get_network_messages()
            versusclient.listener.Pump()