| 1 | /* | 
|---|
| 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| 3 |  *                    > www.orxonox.net < | 
|---|
| 4 |  * | 
|---|
| 5 |  * | 
|---|
| 6 |  *   License notice: | 
|---|
| 7 |  * | 
|---|
| 8 |  *   This program is free software; you can redistribute it and/or | 
|---|
| 9 |  *   modify it under the terms of the GNU General Public License | 
|---|
| 10 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
| 11 |  *   of the License, or (at your option) any later version. | 
|---|
| 12 |  * | 
|---|
| 13 |  *   This program is distributed in the hope that it will be useful, | 
|---|
| 14 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 16 |  *   GNU General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  *   You should have received a copy of the GNU General Public License | 
|---|
| 19 |  *   along with this program; if not, write to the Free Software | 
|---|
| 20 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
| 21 |  * | 
|---|
| 22 |  *   Author: | 
|---|
| 23 |  *      Fabian 'x3n' Landau | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "LevelManager.h" | 
|---|
| 32 |  | 
|---|
| 33 | #include "core/CoreIncludes.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "objects/infos/Level.h" | 
|---|
| 36 | #include "objects/infos/HumanPlayer.h" | 
|---|
| 37 |  | 
|---|
| 38 | namespace orxonox | 
|---|
| 39 | { | 
|---|
| 40 |     LevelManager* LevelManager::singletonRef_s = 0; | 
|---|
| 41 |  | 
|---|
| 42 |     LevelManager::LevelManager() | 
|---|
| 43 |     { | 
|---|
| 44 |         RegisterRootObject(LevelManager); | 
|---|
| 45 |  | 
|---|
| 46 |         assert(singletonRef_s == 0); | 
|---|
| 47 |         singletonRef_s = this; | 
|---|
| 48 |  | 
|---|
| 49 |         this->getConnectedClients(); | 
|---|
| 50 |     } | 
|---|
| 51 |  | 
|---|
| 52 |     LevelManager::~LevelManager() | 
|---|
| 53 |     { | 
|---|
| 54 |         assert(singletonRef_s != 0); | 
|---|
| 55 |         singletonRef_s = 0; | 
|---|
| 56 |     } | 
|---|
| 57 |  | 
|---|
| 58 |     void LevelManager::requestActivity(Level* level) | 
|---|
| 59 |     { | 
|---|
| 60 |         this->levels_s.push_back(level); | 
|---|
| 61 |         if (this->levels_s.size() == 1) | 
|---|
| 62 |             this->activateNextLevel(); | 
|---|
| 63 |     } | 
|---|
| 64 |  | 
|---|
| 65 |     void LevelManager::releaseActivity(Level* level) | 
|---|
| 66 |     { | 
|---|
| 67 |         if (this->levels_s.size() > 0) | 
|---|
| 68 |         { | 
|---|
| 69 |             if (this->levels_s.front() == level) | 
|---|
| 70 |             { | 
|---|
| 71 |                 level->setActive(false); | 
|---|
| 72 |                 this->levels_s.pop_front(); | 
|---|
| 73 |                 this->activateNextLevel(); | 
|---|
| 74 |             } | 
|---|
| 75 |             else | 
|---|
| 76 |             { | 
|---|
| 77 |                 for (std::list<Level*>::iterator it = this->levels_s.begin(); it != this->levels_s.end(); ++it) | 
|---|
| 78 |                     if ((*it) == level) | 
|---|
| 79 |                         this->levels_s.erase(it); | 
|---|
| 80 |             } | 
|---|
| 81 |         } | 
|---|
| 82 |     } | 
|---|
| 83 |  | 
|---|
| 84 |     Level* LevelManager::getActiveLevel() | 
|---|
| 85 |     { | 
|---|
| 86 |         if (this->levels_s.size() > 0) | 
|---|
| 87 |             return this->levels_s.front(); | 
|---|
| 88 |         else | 
|---|
| 89 |             return 0; | 
|---|
| 90 |     } | 
|---|
| 91 |  | 
|---|
| 92 |     void LevelManager::activateNextLevel() | 
|---|
| 93 |     { | 
|---|
| 94 |         if (this->levels_s.size() > 0) | 
|---|
| 95 |         { | 
|---|
| 96 |             this->levels_s.front()->setActive(true); | 
|---|
| 97 |             for (std::map<unsigned int, PlayerInfo*>::iterator it = this->clients_.begin(); it != this->clients_.end(); ++it) | 
|---|
| 98 |                 this->levels_s.front()->playerEntered(it->second); | 
|---|
| 99 |         } | 
|---|
| 100 |     } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 |     void LevelManager::clientConnected(unsigned int clientID) | 
|---|
| 104 |     { | 
|---|
| 105 |         COUT(3) << "client connected" << std::endl; | 
|---|
| 106 |  | 
|---|
| 107 |         // create new HumanPlayer instance | 
|---|
| 108 |         HumanPlayer* player = new HumanPlayer(0); | 
|---|
| 109 |         player->setClientID(clientID); | 
|---|
| 110 |  | 
|---|
| 111 |         // add to clients-map | 
|---|
| 112 |         assert(!this->clients_[clientID]); | 
|---|
| 113 |         this->clients_[clientID] = player; | 
|---|
| 114 |  | 
|---|
| 115 |         if (this->getActiveLevel()) | 
|---|
| 116 |             this->getActiveLevel()->playerEntered(player); | 
|---|
| 117 |     } | 
|---|
| 118 |  | 
|---|
| 119 |     void LevelManager::clientDisconnected(unsigned int clientID) | 
|---|
| 120 |     { | 
|---|
| 121 |         COUT(3) << "client disconnected" << std::endl; | 
|---|
| 122 |  | 
|---|
| 123 |         // remove from clients-map | 
|---|
| 124 |         PlayerInfo* player = this->clients_[clientID]; | 
|---|
| 125 |         this->clients_.erase(clientID); | 
|---|
| 126 |  | 
|---|
| 127 |         if (this->getActiveLevel()) | 
|---|
| 128 |             this->getActiveLevel()->playerLeft(player); | 
|---|
| 129 |  | 
|---|
| 130 |         // delete PlayerInfo instance | 
|---|
| 131 |         if (player) | 
|---|
| 132 |             delete player; | 
|---|
| 133 |     } | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 |     PlayerInfo* LevelManager::getClient(unsigned int clientID) const | 
|---|
| 137 |     { | 
|---|
| 138 |         std::map<unsigned int, PlayerInfo*>::const_iterator it = this->clients_.find(clientID); | 
|---|
| 139 |         if (it != this->clients_.end()) | 
|---|
| 140 |             return it->second; | 
|---|
| 141 |         else | 
|---|
| 142 |             return 0; | 
|---|
| 143 |     } | 
|---|
| 144 | } | 
|---|