Пример #1
0
void StartGameProtocol::update()
{
    if (m_state == NONE)
    {
        // if no synchronization protocol exists, create one
        m_listener->requestStart(new SynchronizationProtocol());
        Log::info("StartGameProtocol", "SynchronizationProtocol started.");
        // race startup sequence
        NetworkWorld::getInstance<NetworkWorld>()->start(); // builds it and starts
        race_manager->setNumKarts(m_game_setup->getPlayerCount());
        race_manager->setNumPlayers(m_game_setup->getPlayerCount());
        race_manager->setNumLocalPlayers(1);
        std::vector<NetworkPlayerProfile*> players = m_game_setup->getPlayers();
        std::sort(players.begin(), players.end(), sort_karts);
        // have to add self first
        for (unsigned int i = 0; i < players.size(); i++)
        {
            bool is_me = (players[i]->user_profile == Online::CurrentUser::get()->getProfile());
            if (is_me)
            {
                NetworkPlayerProfile* profile = players[i];
                RemoteKartInfo rki(profile->race_id, profile->kart_name,
                    profile->user_profile->getUserName(), profile->race_id, !is_me);
                rki.setGlobalPlayerId(profile->race_id);
                rki.setLocalPlayerId(is_me?0:1);
                rki.setHostId(profile->race_id);
                PlayerProfile* profileToUse = unlock_manager->getCurrentPlayer();
                assert(profileToUse);
                InputDevice* device = input_manager->getDeviceList()->getLatestUsedDevice();
                int new_player_id = 0;
                if (StateManager::get()->getActivePlayers().size() >= 1) // more than one player, we're the first
                    new_player_id = 0;
                else
                    new_player_id = StateManager::get()->createActivePlayer( profileToUse, device , players[i]->user_profile);
                device->setPlayer(StateManager::get()->getActivePlayer(new_player_id));
                input_manager->getDeviceList()->setSinglePlayer(StateManager::get()->getActivePlayer(new_player_id));

                race_manager->setPlayerKart(i, rki);
                race_manager->setLocalKartInfo(new_player_id, profile->kart_name);
                Log::info("StartGameProtocol", "Self player device added.");            // self config
                NetworkWorld::getInstance()->m_self_kart = profile->kart_name;
            }
        }
        for (unsigned int i = 0; i < players.size(); i++)
        {
            bool is_me = (players[i]->user_profile == Online::CurrentUser::get()->getProfile());
            NetworkPlayerProfile* profile = players[i];
            RemoteKartInfo rki(profile->race_id, profile->kart_name,
                profile->user_profile->getUserName(), profile->race_id, !is_me);
            rki.setGlobalPlayerId(profile->race_id);
            // on the server, the race id must be the local one.
            rki.setLocalPlayerId(m_listener->isServer()?profile->race_id:(is_me?0:1));
            rki.setHostId(profile->race_id);
            Log::info("StartGameProtocol", "Creating kart %s for Player#%d with race_id %d", profile->kart_name.c_str(), i, profile->race_id);

            if (!is_me)
            {
                StateManager::get()->createActivePlayer( NULL, NULL , players[i]->user_profile);

                race_manager->setPlayerKart(i, rki);
            }
        }
        race_manager->computeRandomKartList();
        Log::info("StartGameProtocol", "Player configuration ready.");
        m_state = SYNCHRONIZATION_WAIT;
/*
        KartSelectionScreen* s = KartSelectionScreen::getInstance();
        s->setMultiplayer(false);
        s->setFromOverworld(false);
        StateManager::get()->pushScreen( s );*/
    }
    else if (m_state == SYNCHRONIZATION_WAIT)
    {
        SynchronizationProtocol* protocol = static_cast<SynchronizationProtocol*>
            (m_listener->getProtocol(PROTOCOL_SYNCHRONIZATION));
        if (protocol)
        {
            // now the synchronization protocol exists.
            Log::info("StartGameProtocol", "Starting the race loading.");
            race_manager->startSingleRace("jungle", 1, false);
            World::getWorld()->setNetworkWorld(true);
            m_state = LOADING;
        }
    }
    else if (m_state == LOADING)
    {
        if (m_ready)
        {
            m_state = READY;
        }
    }
    else if (m_state == READY)
    {
        // set karts into the network game setup
        NetworkManager::getInstance()->getGameSetup()->bindKartsToProfiles();
        m_state = EXITING;
        m_listener->requestTerminate(this);
    }
}
Пример #2
0
/** Setup the actual game. It first starts the SynchronisationProtocol,
 *  and then 
 */
void StartGameProtocol::setup()
{
    m_state       = NONE;
    m_ready_count = 0;
    m_ready       = false;
    Log::info("SynchronizationProtocol", "Ready !");

    Protocol *p = new SynchronizationProtocol();
    p->requestStart();
    Log::info("StartGameProtocol", "SynchronizationProtocol started.");

    // Race startup sequence
    // ---------------------
    // builds it and starts
    NetworkWorld::getInstance<NetworkWorld>()->start();
    race_manager->setNumKarts(m_game_setup->getPlayerCount());
    race_manager->setNumPlayers(m_game_setup->getPlayerCount());
    race_manager->setNumLocalPlayers(1);

    // Create the kart information for the race manager:
    // -------------------------------------------------
    std::vector<NetworkPlayerProfile*> players = m_game_setup->getPlayers();
    for (unsigned int i = 0; i < players.size(); i++)
    {
        NetworkPlayerProfile* profile = players[i];
        bool is_me =  profile->getGlobalPlayerId()
                   == STKHost::get()->getGameSetup()->getLocalMasterID();
        RemoteKartInfo rki(profile->getGlobalPlayerId(),
                           profile->getKartName(),
                           profile->getName(),
                           /*hostid*/profile->getGlobalPlayerId(),
                           !is_me);
        rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty());
        rki.setLocalPlayerId(0);
        // FIXME: for now (only one local player) the global player id
        // can be used as host id.
        rki.setHostId(profile->getGlobalPlayerId());

        // Inform the race manager about the data for this kart.
        race_manager->setPlayerKart(i, rki);

        if(is_me)
        {
            PlayerProfile* profile_to_use = PlayerManager::getCurrentPlayer();
            assert(profile_to_use);
            InputDevice* device = input_manager->getDeviceManager()
                                ->getLatestUsedDevice();
            int new_player_id = 0;

            // more than one player, we're the first
            if (StateManager::get()->getActivePlayers().size() >= 1)
                new_player_id = 0;
            else
                new_player_id = StateManager::get()
                              ->createActivePlayer(profile_to_use, device);
            StateManager::ActivePlayer *ap =
                           StateManager::get()->getActivePlayer(new_player_id);
            device->setPlayer(ap);
            input_manager->getDeviceManager()->setSinglePlayer(ap);
            race_manager->setLocalKartInfo(new_player_id,
                                           profile->getKartName());
            NetworkWorld::getInstance()->setSelfKart(profile->getKartName());
        }   // if is_me
        else
        {
            StateManager::get()->createActivePlayer( NULL, NULL );
            race_manager->setPlayerKart(i, rki);
        }
    }   // for i in players

    race_manager->computeRandomKartList();
    Log::info("StartGameProtocol", "Player configuration ready.");
    m_state = SYNCHRONIZATION_WAIT;

}   // setup