void Game::HandleNetworkMessage( NetworkMessage* pMessage ) { switch(pMessage->msgid) { case FPS::MSG_PLAYER_MOVE_UPDATE: { // Get a pointer to the game specific network message. PlayerMoveUpdateMsg *msg = (PlayerMoveUpdateMsg *)pMessage; // Get a pointer to the player in question. PlayerObject *player = m_playerManager->GetPlayer( msg->PlayerID ); if(player == NULL) break; // If the player is disabled then chances are the local player has // just joined the game, and is receiving messages about a player // that it does not realise has already spawned. So just enable // that player and start updating it. if( player->GetEnabled() == false ) { player->SetEnabled( true ); player->SetVisible( true ); } // Update all the player's details. player->SetTranslation( msg->translation.x, msg->translation.y, msg->translation.z ); player->SetDrive( msg->drive ); player->SetStrafe( msg->strafe ); player->SetFire( msg->fire ); } break; case FPS::MSG_PLAYER_LOOK_UPDATE: { // Get a pointer to the game specific network message. PlayerLookUpdateMsg* msg = (PlayerLookUpdateMsg *)pMessage;; // Update the player's rotation and view tilt. if(m_playerManager->GetPlayer( msg->PlayerID )) { m_playerManager->GetPlayer( msg->PlayerID )->SetRotation( 0.0f, msg->rotationY, 0.0f ); m_playerManager->GetPlayer( msg->PlayerID )->SetViewTilt( msg->viewTilt ); } } break; } }
//----------------------------------------------------------------------------- // Handles the game specific network messages. Called by the network object. //----------------------------------------------------------------------------- void Game::HandleNetworkMessage( ReceivedMessage *msg ) { // Process the received messaged based on its type. switch( msg->msgid ) { case MSGID_CREATE_PLAYER: { // Add the new player to the player manager and the scene. PlayerObject *object = m_playerManager->AddPlayer( g_engine->GetNetwork()->GetPlayer( msg->dpnid ) ); g_engine->GetSceneManager()->AddObject( object ); // Check if the new player is the host player. if( object->GetID() == g_engine->GetNetwork()->GetHostID() ) { // Load the scene from the host player's selection. g_engine->GetSceneManager()->LoadScene( ( (PlayerData*)g_engine->GetNetwork()->GetPlayer( msg->dpnid )->data )->map, "./Assets/Scenes/" ); // Allow the network to receive game specific messages. g_engine->GetNetwork()->SetReceiveAllowed( true ); } break; } case MSGID_DESTROY_PLAYER: { // Remove the player from the player manager and the scene. SceneObject *object = m_playerManager->GetPlayer( msg->dpnid ); g_engine->GetSceneManager()->RemoveObject( &object ); m_playerManager->RemovePlayer( msg->dpnid ); break; } case MSGID_PLAYER_HEALTH: { // Get a pointer to the game specific network message. PlayerHealthMsg *phm = (PlayerHealthMsg*)msg; // Set the player's health. m_playerManager->GetPlayer( phm->dpnid )->SetHealth( phm->health ); // Check if the player has been killed. if( phm->health <= 0.0f ) m_playerManager->GetPlayer( phm->dpnid )->Kill(); break; } case MSGID_PLAYER_MOVE_UPDATE: { // Get a pointer to the game specific network message. PlayerMoveUpdateMsg *pmum = (PlayerMoveUpdateMsg*)msg; // Get a pointer to the player in question. PlayerObject *player = m_playerManager->GetPlayer( pmum->dpnid ); // If the player is disabled then chances are the local player has // just joined the game, and is receiving messages about a player // that it does not realise has already spawned. So just enable // that player and start updating it. if( player->GetEnabled() == false ) { player->SetEnabled( true ); player->SetVisible( true ); } // Update all the player's details. player->SetTranslation( pmum->translation.x, pmum->translation.y, pmum->translation.z ); player->SetDrive( pmum->drive ); player->SetStrafe( pmum->strafe ); player->SetFire( pmum->fire ); break; } case MSGID_PLAYER_LOOK_UPDATE: { // Get a pointer to the game specific network message. PlayerLookUpdateMsg *plum = (PlayerLookUpdateMsg*)msg; // Update the player's rotation and view tilt. m_playerManager->GetPlayer( plum->dpnid )->SetRotation( 0.0f, plum->rotationY, 0.0f ); m_playerManager->GetPlayer( plum->dpnid )->SetViewTilt( plum->viewTilt ); break; } case MSGID_PLAYER_SCORE: { // Get a pointer to the game specific network message. PlayerScoreMsg *psm = (PlayerScoreMsg*)msg; // Update the player's score. m_playerManager->GetPlayer( psm->dpnid )->SetFrags( psm->frags ); m_playerManager->GetPlayer( psm->dpnid )->SetDeaths( psm->deaths ); break; } case MSGID_SPAWN_POINT_REQUEST: { // Send a spawn point message back to the requesting player. SpawnPointMsg spm; spm.msgid = MSGID_SPAWN_POINT; spm.dpnid = msg->dpnid; spm.spawnPoint = g_engine->GetSceneManager()->GetSpawnPointID( g_engine->GetSceneManager()->GetRandomPlayerSpawnPoint() ); g_engine->GetNetwork()->Send( &spm, sizeof( SpawnPointMsg ), msg->dpnid ); break; } case MSGID_SPAWN_POINT: { // Get a pointer to the game specific network message. SpawnPointMsg *spm = (SpawnPointMsg*)msg; // Ensure this message is for the local player. if( spm->dpnid != g_engine->GetNetwork()->GetLocalID() ) break; // Spawn the local player using this spawn point. m_playerManager->SpawnLocalPlayer( spm->spawnPoint ); break; } case MSGID_SPAWN_PLAYER: { // Get a pointer to the game specific network message. SpawnPlayerMsg *spm = (SpawnPlayerMsg*)msg; // Spawn the player. m_playerManager->SpawnPlayer( spm->dpnid, spm->translation ); break; } case MSGID_TERMINATE_SESSION: { // Switch to the menu state. g_engine->ChangeState( STATE_MENU ); break; } } }