Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

Join_network.cpp

Go to the documentation of this file.
00001 
00010 #include "Join.h"
00011 
00012 void Join::networkLoop() {
00013         unsigned int i;
00014 
00015         GE_PlayerJoined *ge_playerJoined = NULL;
00016         GE_State *ge_state = NULL;
00017         GE_PlayerScore *ge_playerScore = NULL;
00018         GE_PlayerDisconnected *ge_playerDisconnected = NULL;
00019         GE_PlayerExtras *ge_playerExtras = NULL;
00020         GE_Info *ge_info = NULL;
00021 
00022         Player *p;
00023         int playerID;
00024         int type;
00025 
00026         //-----------------------------------------------------------------##
00027         // Network event handler
00028         //-----------------------------------------------------------------##
00029         while (i = gne->nextEventPending()) {
00030                 //console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Event pending [%d]", i);
00031 
00032                 switch(i) {
00033                         //-----------------------------------------------------------------#
00034                         // Player joined
00035                         //-----------------------------------------------------------------#
00036                         case E_PLAYER_JOINED:
00037                                 ge_playerJoined = (GE_PlayerJoined *)gne->getNextEvent();
00038                                 playerID = ge_playerJoined->getId();
00039                                 p = gne->getPlayerById(playerID);
00040                                 strcpy(g_players[playerID].name, p->getName());
00041 
00042                                 // set defaults for player
00043                                 g_players[playerID].life = 100.0f;
00044                                 g_players[playerID].frags = 0;
00045                                 g_players[playerID].kills = 0;
00046                                 g_players[playerID].deaths = 0;
00047                                 //if (playerID < g_myPlayerID)
00048                                 //      g_players[playerID].state = PLAYER_STATE_LOADING;
00049                                 //else
00050                                 g_players[playerID].state = PLAYER_STATE_INVISIBLE;
00051                                 g_players[playerID].ownsParticleSystem = -1;
00052                                 g_players[playerID].chatClr = getPlayerChatColor(playerID);
00053 
00054                                 if (playerID > g_myPlayerID) {
00055                                         m_sounds[JOIN_SOUND_ONLINE].play();
00056                                 }
00057 
00058                                 // I'm server, notify new player about level list
00071                                 console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Player %d joined", playerID);
00072 
00073                                 if (ge_playerJoined) delete ge_playerJoined;
00074                                 break;
00075 
00076                         //-----------------------------------------------------------------#
00077                         // Player disconnected!
00078                         //-----------------------------------------------------------------#
00079                         case E_PLAYER_DISCONNECTED:
00080                                 ge_playerDisconnected = (GE_PlayerDisconnected *)gne->getNextEvent();
00081                                 playerID = ge_playerDisconnected->getId();
00082                                 console.addEx(Vec3(1.0f, 0.0f, 0.0f), "'%s' disconnected!", g_players[playerID].name);
00083                                 g_players[playerID].state = PLAYER_STATE_OFFLINE;
00084 
00085                                 if (ge_playerDisconnected) delete ge_playerDisconnected;
00086                                 break;
00087 
00088                         //-----------------------------------------------------------------#
00089                         // Player state
00090                         //-----------------------------------------------------------------#
00091                         case E_PLAYER_STATE:
00092                                 ge_state = (GE_State *)gne->getNextEvent();
00093                                 playerID = ge_state->getId();
00094                                 g_players[playerID].state = ge_state->getState();
00095 
00096                                 // the new player has loaded, send levels to him
00097                                 if (gne->isServer() && g_players[playerID].state == PLAYER_STATE_INVISIBLE) {
00098                                         for (i=0; i<g_gameInfo.nLvls; i++) {
00099                                                 if (g_gameInfo.lvlList[i][0] == 1) {
00100                                                         //LOG_MISC(("Skickar lvl: %d",i));
00101                                                         gne->sendInfo(playerID, JOIN_NET_ADD, g_gameInfo.lvlList[i][1], g_gameInfo.lvlList[i][2]);
00102                                                         Sleep(25);
00103                                                 }
00104                                         }
00105 
00106                                         // and player models
00107                                         for (i=0; i<SPLAYER_MAX_PLAYERS; i++) {
00108                                                 if (i != playerID && g_players[i].state != PLAYER_STATE_OFFLINE) {
00109                                                         gne->sendInfo(playerID, JOIN_NET_PLAYER_MODEL, i, g_players[i].model);
00110                                                         //LOG_MISC(("Drar iväg spelar-model för %d",i));
00111                                                         Sleep(25);
00112                                                 }
00113                                         }
00114                                 }
00115 
00116                                 if (ge_state) delete ge_state;
00117                                 break;
00118 
00119                         //-----------------------------------------------------------------#
00120                         // Player score
00121                         //-----------------------------------------------------------------#
00122                         case E_PLAYER_SCORE:
00123                                 ge_playerScore = (GE_PlayerScore *)gne->getNextEvent();
00124                                 playerID = ge_playerScore->getId();
00125                                 g_players[playerID].frags = ge_playerScore->getFrags();
00126                                 g_players[playerID].kills = ge_playerScore->getKills();
00127                                 g_players[playerID].deaths = ge_playerScore->getDeaths();
00128 
00129                                 if (ge_playerScore) delete ge_playerScore;
00130                                 break;
00131 
00132                         //-----------------------------------------------------------------#
00133                         // Select galaxy (yes, same as Extras in game ;))
00134                         //-----------------------------------------------------------------#
00135                         case E_PLAYER_EXTRAS:
00136                                 ge_playerExtras = (GE_PlayerExtras *)gne->getNextEvent();
00137                                 playerID = ge_playerExtras->getId();
00138 
00139                                 //LOG_MISC(("EXTRAS EVENT"));
00140                                 //applyExtras(playerID, ge_playerExtras->getExtras(), ((ge_playerExtras->getValue() == 0) ? false : true));
00141 
00142                                 // galaxy / level selct
00143                                 if (playerID == JOIN_NET_SELECT && !gne->isServer()) {
00144                                         m_selectedGalaxy = ge_playerExtras->getExtras();
00145                                         m_selectedLevel = ge_playerExtras->getValue();
00146                                 }
00147                                 // add level
00148                                 else if (playerID == JOIN_NET_ADD && !gne->isServer()) {
00149                                         addLevel(ge_playerExtras->getExtras(), ge_playerExtras->getValue());
00150                                 }
00151                                 // remove level
00152                                 else if (playerID == JOIN_NET_REMOVE && !gne->isServer()) {
00153                                         removeLevel();
00154                                 }
00155                                 // start game :D
00156                                 else if (playerID == JOIN_NET_START) {
00157                                         gne->setPlayerState(g_myPlayerID,PLAYER_STATE_LOADING);
00158 
00159                                         g_gameInfo.currLvl[0] = ge_playerExtras->getExtras();
00160                                         g_gameInfo.currLvl[1] = ge_playerExtras->getValue();
00161                                         //console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Galaxy: %d, Level: %d", g_gameInfo.currLvl[0], g_gameInfo.currLvl[1]);
00162                                         g_music.stop();
00163                                         g_selectedScene = SCENE_PLAY;
00164                                 }
00165                                 // Set model
00166                                 else if (playerID >= 0) {
00167                                         g_players[playerID].model = ge_playerExtras->getExtras();
00168                                         //LOG_MISC(("PlayerID: %d, Model: %d",playerID, ge_playerExtras->getExtras()));
00169                                 }
00170 
00171                                 //console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Got some!");
00172 
00173                                 if (ge_playerExtras) delete ge_playerExtras;
00174                                 break;
00175 
00176                         //-----------------------------------------------------------------#
00177                         // Info
00178                         //-----------------------------------------------------------------#
00179                         case E_INFO:
00180                                 ge_info = (GE_Info *)gne->getNextEvent();
00181                                 type = ge_info->getValA();
00182 
00183                                 //LOG_MISC(("INFO EVENT"));
00184 
00185                                 if (type == JOIN_NET_ADD) {
00186                                         addLevel(ge_info->getValB(), ge_info->getValC());
00187                                 }
00188                                 else if (type == JOIN_NET_PLAYER_MODEL) {
00189                                         g_players[ge_info->getValB()].model = ge_info->getValC();
00190                                         //LOG_MISC(("2. PlayerID: %d, Model: %d",ge_info->getValB(), ge_info->getValC()));
00191                                 }
00192 
00193                         //-----------------------------------------------------------------#
00194                         // No valid event
00195                         //-----------------------------------------------------------------#
00196                         default:
00197                                 gne->getNextEvent();
00198                                 break;
00199                 }
00200         }
00201 }

Generated on Sun Jun 5 15:47:04 2005 for Defacto by  doxygen 1.4.3