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

Play_network.cpp

Go to the documentation of this file.
00001 
00010 #include "Play.h"
00011 
00012 void Play::networkLoop() {
00013         unsigned int i;
00014 
00015         GE_PlayerJoined *ge_playerJoined = NULL;
00016         GE_Attack *ge_attack = NULL;
00017         GE_PlayerPos *ge_playerPos = NULL;
00018         GE_State *ge_state = NULL;
00019         GE_PlayerAction *ge_playerAction = NULL;
00020         GE_PlayerDeath *ge_playerDeath = NULL;
00021         GE_PlayerScore *ge_playerScore = NULL;
00022         GE_PlayerDisconnected *ge_playerDisconnected = NULL;
00023         GE_PlayerExtras *ge_playerExtras = NULL;
00024         GE_DealExtras *ge_dealExtras = NULL;
00025 
00026         //Player *p;
00027         int playerID;
00028         int type;
00029 
00030         float dist;
00031 
00032         //-----------------------------------------------------------------##
00033         // Network event handler
00034         //-----------------------------------------------------------------##
00035         while (i = gne->nextEventPending()) {
00036                 //console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Event pending [%d]", i);
00037 
00038                 switch(i) {
00039                         //-----------------------------------------------------------------#
00040                         // Player joined (this will not be here but in the menu instead)
00041                         //-----------------------------------------------------------------#
00073                         //-----------------------------------------------------------------#
00074                         // Player disconnected!
00075                         //-----------------------------------------------------------------#
00076                         case E_PLAYER_DISCONNECTED:
00077                                 ge_playerDisconnected = (GE_PlayerDisconnected *)gne->getNextEvent();
00078                                 playerID = ge_playerDisconnected->getId();
00079                                 console.addEx(Vec3(1.0f, 0.0f, 0.0f), "'%s' disconnected!", g_players[playerID].name);
00080                                 g_players[playerID].state = PLAYER_STATE_OFFLINE;
00081 
00082                                 if (ge_playerDisconnected) delete ge_playerDisconnected;
00083                                 break;
00084 
00085                         //-----------------------------------------------------------------#
00086                         // Attack
00087                         //-----------------------------------------------------------------#
00088                         case E_ATTACK:
00089                                 ge_attack = (GE_Attack *)gne->getNextEvent();
00090                                 type = ge_attack->getAttackType();
00091                                 if (type == WEAPON_GRENADE) {
00092                                         throwGrenade(ge_attack->getId(), ge_attack->getPos(), ge_attack->getDir(), 3.0f, 1.0f);
00093                                 }
00094                                 else {
00095                                         SWeapon weapon;
00096                                         Weapon::set(weapon, type);
00097                                         fireProjectile(ge_attack->getId(), ge_attack->getPos(), ge_attack->getDir(), weapon.power);
00098                                 }
00099 
00100                                 if (ge_attack) delete ge_attack;
00101                                 break;
00102 
00103                         //-----------------------------------------------------------------#
00104                         // Player position
00105                         //-----------------------------------------------------------------#
00106                         case E_PLAYER_POS:
00107                                 ge_playerPos = (GE_PlayerPos *)gne->getNextEvent();
00108                                 playerID = ge_playerPos->getId();
00109                                 g_players[playerID].rotation = ge_playerPos->getRotation();
00110                                 g_players[playerID].moving = ge_playerPos->getMove();
00111 
00112                                 if (g_players[playerID].rotation == PLAYER_ROTATION_NONE || g_players[playerID].moving == PLAYER_MOVING_NONE) {
00113                                         g_players[playerID].angle = ge_playerPos->getAngle();
00114                                         g_players[playerID].pos = Vec3(ge_playerPos->getX(), 0.0f, ge_playerPos->getY());
00115                                 }
00116 
00117                                 if (playerID == g_myPlayerID)
00118                                         setPlayerCam(g_myPlayerID);
00119 
00120                                 if (ge_playerPos) delete ge_playerPos;
00121                                 break;
00122 
00123                         //-----------------------------------------------------------------#
00124                         // Player state
00125                         //-----------------------------------------------------------------#
00126                         case E_PLAYER_STATE:
00127                                 ge_state = (GE_State *)gne->getNextEvent();
00128                                 playerID = ge_state->getId();
00129                                 g_players[playerID].state = ge_state->getState();
00130 
00131                                 if (ge_state) delete ge_state;
00132                                 break;
00133 
00134                         //-----------------------------------------------------------------#
00135                         // Player MDK (killed by onother player)
00136                         //-----------------------------------------------------------------#
00137                         case E_PLAYER_DEATH:
00138                                 ge_playerDeath = (GE_PlayerDeath *)gne->getNextEvent();
00139                                 playerID = ge_playerDeath->getId();
00140                                 type = ge_playerDeath->getKillerID();
00141 
00142                                 g_players[playerID].state = PLAYER_STATE_INVISIBLE;
00143 
00144                                 dist = (g_players[g_myPlayerID].pos - g_players[playerID].pos).length();
00145                                 if (dist < 1.0f) dist = 1.0f;
00146 
00147                                 m_sounds[PLAY_SOUND_DEATH].play((int)255/dist);
00148 
00149                                 this->createExplosion(g_players[playerID].pos, 1.5f, false);
00150 
00151                                 if (type == playerID) {
00152                                         g_players[playerID].frags--;
00153                                         g_players[playerID].deaths++;
00154 
00155                                         console.addEx(Vec3(0.0f, 1.0f, 1.0f), "'%s' got tired of life", g_players[playerID].name);
00156                                 }
00157                                 else {
00158                                         g_players[type].frags++;
00159                                         g_players[type].kills++;
00160                                         g_players[playerID].deaths++;
00161 
00162                                         console.addEx(Vec3(0.0f, 1.0f, 1.0f), "'%s' killed by '%s'", g_players[playerID].name, g_players[type].name);
00163                                 }
00164 
00165                                 if (gne->isServer()) {  // server
00166                                         g_players[playerID].spawnTime = g_time + 2000.0f;
00167 
00168                                         gne->setPlayerScore(playerID, g_players[playerID].frags, g_players[playerID].kills, g_players[playerID].deaths);
00169                                         gne->setPlayerScore(type, g_players[type].frags, g_players[type].kills, g_players[type].deaths);
00170                                 }
00171 
00172                                 if (ge_playerDeath) delete ge_playerDeath;
00173 
00174                                 break;
00175 
00176                         //-----------------------------------------------------------------#
00177                         // Player score
00178                         //-----------------------------------------------------------------#
00179                         case E_PLAYER_SCORE:
00180                                 ge_playerScore = (GE_PlayerScore *)gne->getNextEvent();
00181                                 playerID = ge_playerScore->getId();
00182                                 g_players[playerID].frags = ge_playerScore->getFrags();
00183                                 g_players[playerID].kills = ge_playerScore->getKills();
00184                                 g_players[playerID].deaths = ge_playerScore->getDeaths();
00185 
00186                                 if (ge_playerScore) delete ge_playerScore;
00187                                 break;
00188 
00189                         //-----------------------------------------------------------------#
00190                         // Deal extras
00191                         //-----------------------------------------------------------------#
00192                         case E_DEAL_EXTRAS:
00193                                 ge_dealExtras = (GE_DealExtras *)gne->getNextEvent();
00194                                 type = ge_dealExtras->getDEType();
00195 
00196                                 // add
00197                                 if (ge_dealExtras->getFlag() != 0) {
00198                                         newExtras(type, ge_dealExtras->getX(), ge_dealExtras->getY());
00199                                         //console.addEx(Vec3(0.0f, 1.0f, 1.0f), "New type %d extras", ge_dealExtras->getDEType());
00200                                 }
00201                                 // remove
00202                                 else {
00203                                         float x = ge_dealExtras->getX();
00204                                         float z = ge_dealExtras->getY();
00205                                         Vec3 v(x, 0.0f, z);
00206                                         float dist;
00207 
00208                                         for (i=0; i<PLAY_EXTRAS_SIMULTANEOUS; i++) {
00209                                                 if (m_extras[i].type == type) {
00210                                                         dist = (m_extras[i].pos - v).length();
00211                                                         if (dist < 0.01f) {
00212                                                                 m_extras[i].type = PLAY_EXTRAS_NONE;
00213                                                                 //console.addEx(Vec3(0.0f, 1.0f, 0.0f), "Removed type %d extras", ge_dealExtras->getDEType());
00214                                                                 break;
00215                                                         }
00216                                                 }
00217                                         }
00218                                 }
00219 
00220                                 if (ge_dealExtras) delete ge_dealExtras;
00221                                 break;
00222 
00223                         //-----------------------------------------------------------------#
00224                         // Extras
00225                         //-----------------------------------------------------------------#
00226                         case E_PLAYER_EXTRAS:
00227                                 ge_playerExtras = (GE_PlayerExtras *)gne->getNextEvent();
00228                                 playerID = ge_playerExtras->getId();
00229 
00230                                 //------------------------------
00231                                 // START GAME
00232                                 //------------------------------
00233                                 if (playerID == PLAY_NET_START_GAME) {
00234                                         g_gameInfo.gameState = GAME_STATE_RUNNING;
00235                                         //g_players[g_myPlayerID].state = PLAYER_STATE_INVISIBLE;
00236 
00237                                         m_timeStart = g_time;
00238                                         m_timeEnd = m_timeStart + 300000.0f;    // 5 min matches
00239 
00240                                         // set spawn time (on server)
00241                                         if (gne->isServer()) {
00242                                                 for (i=0; i<SPLAYER_MAX_PLAYERS; i++) {
00243                                                         if (g_players[i].state == PLAYER_STATE_READY) {
00244                                                                 g_players[i].state = PLAYER_STATE_INVISIBLE;
00245                                                                 g_players[i].spawnTime = g_time + 500.0f + (float)RANDOM_NUM*250.0f;
00246                                                         }
00247                                                 }
00248                                         }
00249 
00250                                 }
00251                                 //------------------------------
00252                                 // GAME OVER
00253                                 //------------------------------
00254                                 else if (playerID == PLAY_NET_GAME_OVER) {
00255                                         g_gameInfo.gameState = GAME_STATE_GAME_OVER;
00256                                         m_timeEnd = g_time;
00257                                         //g_players[g_myPlayerID].state = PLAYER_STATE_INVISIBLE;
00258 
00259                                         //console.addEx(Vec3(0.0f, 0.0f, 1.0f), "Game Over!");
00260                                         //g_selectedScene = SCENE_SCORES;
00261                                 }
00262                                 //------------------------------
00263                                 // Extras
00264                                 //------------------------------
00265                                 else {
00266                                         applyExtras(playerID, ge_playerExtras->getExtras(), ((ge_playerExtras->getValue() == 0) ? false : true));
00267 
00268                                         if (playerID == g_myPlayerID && ge_playerExtras->getValue() != 0) {
00269                                                 m_sounds[13-ge_playerExtras->getExtras()].play(255);
00270                                         }
00271                                 }
00272 
00273                                 //if (ge_playerExtras->getValue() != 0)
00274                                 //      console.addEx(Vec3(1.0f, 1.0f, 0.0f), "#%d got new %d type extras", ge_playerExtras->getId(), ge_playerExtras->getExtras());
00275                                 //else
00276                                 //      console.addEx(Vec3(1.0f, 1.0f, 0.0f), "#%d extras type %d remove", ge_playerExtras->getId(), ge_playerExtras->getExtras());
00277 
00278                                 if (ge_playerExtras) delete ge_playerExtras;
00279                                 break;
00280 
00281                         //-----------------------------------------------------------------#
00282                         // Player action (actions is sent also sent to sending client/server)
00283                         //-----------------------------------------------------------------#
00284                         case E_PLAYER_ACTION:
00285                                 ge_playerAction = (GE_PlayerAction *)gne->getNextEvent();
00286                                 playerID = ge_playerAction->getId();
00287                                 type = ge_playerAction->getAction();
00288 
00289                                 switch (type) {
00290                                         //---------------------------------------------------------
00291                                         // Player exploded
00292                                         //---------------------------------------------------------
00293                                         case PLAY_ACTION_PLAYER_EXPLODE:
00294                                                 g_players[playerID].state = PLAYER_STATE_INVISIBLE;
00295 
00296                                                 dist = (g_players[g_myPlayerID].pos - g_players[playerID].pos).length();
00297                                                 if (dist < 1.0f) dist = 1.0f;
00298 
00299                                                 m_sounds[PLAY_SOUND_DEATH].play((int)255/dist);
00300                                                 this->createExplosion(g_players[playerID].pos, 1.5f, false);
00301 
00302                                                 if (gne->isServer()) {  // server
00303                                                         g_players[playerID].spawnTime = g_time + 2000.0f;
00304                                                 }
00305                                                 break;
00306 
00307                                         //---------------------------------------------------------
00308                                         // Player drowned
00309                                         //---------------------------------------------------------
00310                                         case PLAY_ACTION_PLAYER_DROWN:
00311                                                 g_players[playerID].state = PLAYER_STATE_INVISIBLE;
00312 
00313                                                 dist = (g_players[g_myPlayerID].pos - g_players[playerID].pos).length();
00314                                                 if (dist < 1.0f) dist = 1.0f;
00315 
00316                                                 m_sounds[PLAY_SOUND_DROWN].play((int)255/dist);
00317                                                 createExplosion(g_players[playerID].pos, 1.5f, false);
00318 
00319                                                 console.addEx(Vec3(1.0f, 1.0f, 0.0f), "'%s' ended his life in the pool", g_players[playerID].name);
00320 
00321                                                 if (gne->isServer()) {  // server
00322                                                         g_players[playerID].spawnTime = g_time + 2000.0f;
00323 
00324                                                         g_players[playerID].frags--;
00325                                                         g_players[playerID].deaths++;
00326 
00327                                                         gne->setPlayerScore(playerID, g_players[playerID].frags, g_players[playerID].kills, g_players[playerID].deaths);
00328                                                 }
00329 
00330                                                 break;
00331 
00332                                         //---------------------------------------------------------
00333                                         // Spawn player
00334                                         //---------------------------------------------------------
00335                                         case PLAY_ACTION_PLAYER_SPAWN:
00336                                                 g_players[playerID].state = PLAYER_STATE_SPAWNING;
00337 
00338                                                 //console.addEx(Vec3(1.0f, 0.0f, 1.0f), "'%s' spawned", g_players[playerID].name);
00339 
00340                                                 // reset
00341                                                 g_players[playerID].dir = Vec3(0.0f, 0.0f, 1.0f);
00342                                                 g_players[playerID].angle = 0.0f;
00343                                                 g_players[playerID].life = 100.0f;
00344                                                 Weapon::set(g_players[playerID].weapon, WEAPON_GUN);
00345 
00346                                                 // particles
00347                                                 if (g_players[playerID].ownsParticleSystem > 0 && m_particleSystems[g_players[playerID].ownsParticleSystem].isAlive())
00348                                                         m_particleSystems[g_players[playerID].ownsParticleSystem].die();
00349                                                 g_players[playerID].ownsParticleSystem = startParticleSystem(g_players[playerID].pos + Vec3(0.0f, -0.25f, 0.0f), PARTICLESYSTEM_SPAWN);
00350 
00351                                                 dist = (g_players[g_myPlayerID].pos - g_players[playerID].pos).length();
00352                                                 if (dist < 1.0f) dist = 1.0f;
00353 
00354                                                 m_sounds[PLAY_SOUND_SPAWN].play((int)255/dist); // spawn sound
00355 
00356                                                 // update camera
00357                                                 if (playerID == g_myPlayerID)
00358                                                         setPlayerCam(playerID);
00359 
00360                                                 break;
00361 
00362                                         //---------------------------------------------------------
00363                                         // Spawn player
00364                                         //---------------------------------------------------------
00365                                         case PLAY_ACTION_PLAYER_SPAWNED:
00366                                                 g_players[playerID].state = PLAYER_STATE_USER;
00367                                                 g_players[playerID].spawnTime = 0.0f;
00368 
00369                                                 m_particleSystems[g_players[playerID].ownsParticleSystem].die();
00370                                                 g_players[playerID].ownsParticleSystem = -1;
00371 
00372                                                 break;
00373 
00374                                         //---------------------------------------------------------
00375                                         // Not defined
00376                                         //---------------------------------------------------------
00377                                         default:
00378                                                 // throw away
00379                                                 break;
00380                                 }
00381 
00382                                 if (ge_playerAction) delete ge_playerAction;
00383                                 break;
00384 
00385 
00386                         //-----------------------------------------------------------------#
00387                         // No valid event
00388                         //-----------------------------------------------------------------#
00389                         default:
00390                                 gne->getNextEvent();
00391                                 break;
00392                 }
00393         }
00394 }

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