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

Scores.cpp

Go to the documentation of this file.
00001 
00007 #include "Scores.h"
00008 
00009 /********************************************************************
00010  * Constructor
00011  */
00012 Scores::Scores() {
00013 
00014 }
00015 
00016 /********************************************************************
00017  * Constructor
00018  */
00019 Scores::~Scores() {
00020         unload();
00021 }
00022 
00023 /********************************************************************
00024  * Load
00025  */
00026 bool Scores::load(Application *parent) {
00027         if (parent != NULL)
00028                 m_parent = parent;
00029 
00030         if (!gne && !gne->isRunning())
00031                 return false;
00032 
00033         g_players[g_myPlayerID].state = PLAYER_STATE_WAITING;
00034         gne->setPlayerState(g_myPlayerID, PLAYER_STATE_WAITING);
00035 
00036         LOG_SUCCESS(("Scores loaded"));
00037         m_loaded = true;
00038         return true;
00039 }
00040 
00041 /********************************************************************
00042  * Unload
00043  */
00044 bool Scores::unload() {
00045         if (!m_loaded)
00046                 return false;
00047 
00048         LOG_SUCCESS(("Scores unloaded"));
00049         m_loaded = false;
00050         return true;
00051 }
00052 
00053 /********************************************************************
00054  * Update
00055  */
00056 bool Scores::update() {
00057         if (!m_loaded)
00058                 return false;
00059 
00060         if (!gne || !gne->isRunning())
00061                 return false;
00062 
00063         unsigned int i;
00064 
00065         if (m_parent->keys[VK_SPACE] && g_players[g_myPlayerID].state == PLAYER_STATE_WAITING) {
00066                 g_players[g_myPlayerID].state = PLAYER_STATE_READY;
00067                 gne->setPlayerState(g_myPlayerID, PLAYER_STATE_READY);
00068                 m_parent->keys[VK_SPACE] = false;
00069         }
00070 
00071 
00072         GE_State *ge_state = NULL;
00073         GE_PlayerDisconnected *ge_playerDisconnected = NULL;
00074         GE_PlayerExtras *ge_playerExtras = NULL;
00075 
00076         //Player *p;
00077         int playerID;
00078         //int type;
00079 
00080         //-----------------------------------------------------------------##
00081         // Network event handler
00082         //-----------------------------------------------------------------##
00083         while (gne && (i = gne->nextEventPending())) {
00084 
00085                 switch(i) {
00086 
00087                         //-----------------------------------------------------------------#
00088                         // Player disconnected!
00089                         //-----------------------------------------------------------------#
00090                         case E_PLAYER_DISCONNECTED:
00091                                 ge_playerDisconnected = (GE_PlayerDisconnected *)gne->getNextEvent();
00092                                 playerID = ge_playerDisconnected->getId();
00093                                 console.addEx(Vec3(1.0f, 0.0f, 0.0f), "'%s' disconnected!", g_players[playerID].name);
00094                                 g_players[playerID].state = PLAYER_STATE_OFFLINE;
00095 
00096                                 if (ge_playerDisconnected) delete ge_playerDisconnected;
00097                                 break;
00098 
00099                         //-----------------------------------------------------------------#
00100                         // Player state
00101                         //-----------------------------------------------------------------#
00102                         case E_PLAYER_STATE:
00103                                 ge_state = (GE_State *)gne->getNextEvent();
00104                                 playerID = ge_state->getId();
00105                                 g_players[playerID].state = ge_state->getState();
00106 
00107                                 if (ge_state) delete ge_state;
00108                                 break;
00109 
00110                         //-----------------------------------------------------------------#
00111                         // Select galaxy (yes, same as Extras in game ;))
00112                         //-----------------------------------------------------------------#
00113                         case E_PLAYER_EXTRAS:
00114                                 ge_playerExtras = (GE_PlayerExtras *)gne->getNextEvent();
00115                                 playerID = ge_playerExtras->getId();
00116 
00117                                 // start game :D
00118                                 if (playerID == SCORES_NET_START) {
00119                                         gne->setPlayerState(g_myPlayerID,PLAYER_STATE_LOADING);
00120 
00121                                         g_gameInfo.currLvl[0] = ge_playerExtras->getExtras();
00122                                         g_gameInfo.currLvl[1] = ge_playerExtras->getValue();
00123                                         console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Galaxy: %d, Level: %d", g_gameInfo.currLvl[0], g_gameInfo.currLvl[1]);
00124                                         g_selectedScene = SCENE_PLAY;
00125                                 }
00126                                 else if (playerID == SCORES_NET_END) {
00127                                         g_gameInfo.gameState = GAME_STATE_END;
00128                                         gne->disconnect();
00129                                         Sleep(500);
00130                                         //gne->stopServer();
00131                                         if (gne) delete gne;
00132                                         gne = NULL;
00133                                         g_selectedScene = SCENE_MENU;
00134                                 }
00135                                 //console.addEx(Vec3(1.0f, 1.0f, 1.0f), "Got some!");
00136 
00137                                 if (ge_playerExtras) delete ge_playerExtras;
00138                                 break;
00139 
00140                         //-----------------------------------------------------------------#
00141                         // No valid event
00142                         //-----------------------------------------------------------------#
00143                         default:
00144                                 gne->getNextEvent();
00145                                 break;
00146                 }
00147         }
00148 
00149 
00150         //-----------------------------------------------------------------#
00151         // Server
00152         //-----------------------------------------------------------------#
00153         if (g_gameInfo.gameState == GAME_STATE_GAME_OVER && gne && gne->isServer()) {
00154                 bool allReady = true;
00155                 for (i=0; i<SPLAYER_MAX_PLAYERS; i++) {
00156                         if (g_players[i].state == PLAYER_STATE_WAITING) {
00157                                 allReady = false;
00158                                 break;
00159                         }
00160                 }
00161 
00162                 if (allReady) {
00163                         bool newLevel = true;
00164 
00165                         g_gameInfo.thisLevel++;
00166 
00167                         if (g_gameInfo.thisLevel > g_gameInfo.nLvls) {
00168                                 newLevel = false;
00169                         }
00170 
00171                         g_gameInfo.currLvl[0] = g_gameInfo.lvlList[g_gameInfo.thisLevel][1];
00172                         g_gameInfo.currLvl[1] = g_gameInfo.lvlList[g_gameInfo.thisLevel][2];
00173 
00174                         if (newLevel && g_gameInfo.lvlList[g_gameInfo.thisLevel][0] == 1) {
00175                                 g_gameInfo.gameState = GAME_STATE_WAITING;
00176                                 gne->setPlayerExtras(SCORES_NET_START,g_gameInfo.currLvl[0],g_gameInfo.currLvl[1]);
00177                                 m_parent->keys['S'] = false;
00178                         }
00179                         else {
00180                                 newLevel = false;
00181                         }
00182 
00183                         if (!newLevel) {
00184                                 g_gameInfo.gameState = GAME_STATE_END;
00185                                 LOG_MISC(("the end"));
00186                                 gne->setPlayerExtras(SCORES_NET_END,0,0);
00187                         }
00188                 }
00189 
00190                 return true;
00191         }
00192 
00193         return true;
00194 }
00195 
00196 /********************************************************************
00197  * Draw frame
00198  */
00199 bool Scores::drawFrame() {
00200         if (!m_loaded)
00201                 return false;
00202 
00203         unsigned int i;
00204 
00205         m_parent->defaultFont->startTextMode();
00206 
00207         if (g_players[g_myPlayerID].state == PLAYER_STATE_WAITING) {
00208                 m_parent->defaultFont->setColor(0.0f, 0.0f, 1.0f, 1.0f);
00209                 m_parent->defaultFont->print((float)g_width/2.0f - 80.0f, 50.0f, "Press space to continue...");
00210         }
00211         else {
00212                 m_parent->defaultFont->setColor(1.0f, 0.0f, 0.0f, 1.0f);
00213                 m_parent->defaultFont->print((float)g_width/2.0f - 85.0f, 50.0f, "Waiting for other players...");
00214         }
00215 
00216         int x = g_width/2.0f - 200.0f;
00217         int y = 140.0f;
00218 
00219         m_parent->defaultFont->setColor(1.0f, 1.0f, 1.0f, 1.0f);
00220         m_parent->defaultFont->print(x, y, "[Name]               [Frags]      [Kills]      [Deaths]");
00221 
00222         y += 25.0f;
00223         
00224         SPlayer *player;
00225         for (i=0; i<SPLAYER_MAX_PLAYERS; i++) {
00226                 if (g_players[i].state != PLAYER_STATE_OFFLINE) {
00227                         player = &g_players[i];
00228 
00229                         m_parent->defaultFont->setColor(1.0f, 1.0f, 0.0f, 1.0f);
00230                         m_parent->defaultFont->print(x, y, "%s", player->name);
00231 
00232                         m_parent->defaultFont->setColor(0.0f, 0.0f, 1.0f, 1.0f);
00233                         m_parent->defaultFont->print(x+22.0f*7.65f, y, "[%d]", player->frags);
00234 
00235                         m_parent->defaultFont->setColor(0.0f, 1.0f, 0.0f, 1.0f);
00236                         m_parent->defaultFont->print(x+35.5f*7.65f, y, "[%d]", player->kills);
00237 
00238                         m_parent->defaultFont->setColor(1.0f, 0.0f, 0.0f, 1.0f);
00239                         m_parent->defaultFont->print(x+49.25f*7.65f, y, "[%d]", player->deaths);
00240 
00241                         y += 25.0f;
00242                 }
00243         }
00244 
00245         m_parent->defaultFont->endTextMode();
00246 
00247         return true;
00248 }

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