| 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 "PlayerManager.h" | 
|---|
| 30 |  | 
|---|
| 31 | #include "core/CoreIncludes.h" | 
|---|
| 32 | #include "core/GameMode.h" | 
|---|
| 33 | #include "core/singleton/ScopedSingletonIncludes.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "Level.h" | 
|---|
| 36 | #include "LevelManager.h" | 
|---|
| 37 | #include "infos/HumanPlayer.h" | 
|---|
| 38 |  | 
|---|
| 39 | namespace orxonox | 
|---|
| 40 | { | 
|---|
| 41 | ManageScopedSingleton(PlayerManager, ScopeID::ROOT, false); | 
|---|
| 42 |  | 
|---|
| 43 | RegisterAbstractClass(PlayerManager).inheritsFrom<ClientConnectionListener>(); | 
|---|
| 44 |  | 
|---|
| 45 | PlayerManager::PlayerManager() | 
|---|
| 46 | { | 
|---|
| 47 | RegisterObject(PlayerManager); | 
|---|
| 48 |  | 
|---|
| 49 | //         this->getConnectedClients(); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | PlayerManager::~PlayerManager() | 
|---|
| 53 | { | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void PlayerManager::clientConnected(unsigned int clientID) | 
|---|
| 57 | { | 
|---|
| 58 | if (GameMode::isMaster()) | 
|---|
| 59 | { | 
|---|
| 60 | if (clientID != 0) | 
|---|
| 61 | orxout(internal_info) << "client connected" << endl; | 
|---|
| 62 |  | 
|---|
| 63 | // create new HumanPlayer instance | 
|---|
| 64 | HumanPlayer* player = new HumanPlayer(nullptr); | 
|---|
| 65 | player->setClientID(clientID); | 
|---|
| 66 |  | 
|---|
| 67 | // add to clients-map | 
|---|
| 68 | assert(!this->clients_[clientID]); | 
|---|
| 69 | this->clients_[clientID] = player; | 
|---|
| 70 |  | 
|---|
| 71 | if (LevelManager::exists() && LevelManager::getInstance().getActiveLevel()) | 
|---|
| 72 | LevelManager::getInstance().getActiveLevel()->playerEntered(player); | 
|---|
| 73 | } | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void PlayerManager::clientDisconnected(unsigned int clientID) | 
|---|
| 77 | { | 
|---|
| 78 | if (GameMode::isMaster()) | 
|---|
| 79 | { | 
|---|
| 80 | if (clientID != 0) | 
|---|
| 81 | orxout(internal_info) << "client disconnected" << endl; | 
|---|
| 82 |  | 
|---|
| 83 | // remove from clients-map | 
|---|
| 84 | PlayerInfo* player = this->clients_[clientID]; | 
|---|
| 85 | this->clients_.erase(clientID); | 
|---|
| 86 |  | 
|---|
| 87 | if (LevelManager::exists() && LevelManager::getInstance().getActiveLevel()) | 
|---|
| 88 | LevelManager::getInstance().getActiveLevel()->playerLeft(player); | 
|---|
| 89 |  | 
|---|
| 90 | // delete PlayerInfo instance | 
|---|
| 91 | if (player) | 
|---|
| 92 | player->destroy(); | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void PlayerManager::disconnectAllClients() | 
|---|
| 97 | { | 
|---|
| 98 | for( std::map<unsigned int, PlayerInfo*>::iterator it = this->clients_.begin(); it != this->clients_.end(); ) | 
|---|
| 99 | this->clientDisconnected( (it++)->first ); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 |  | 
|---|
| 103 | PlayerInfo* PlayerManager::getClient(unsigned int clientID) const | 
|---|
| 104 | { | 
|---|
| 105 | if (GameMode::isMaster()) | 
|---|
| 106 | { | 
|---|
| 107 | std::map<unsigned int, PlayerInfo*>::const_iterator it = this->clients_.find(clientID); | 
|---|
| 108 | if (it != this->clients_.end()) | 
|---|
| 109 | return it->second; | 
|---|
| 110 | } | 
|---|
| 111 | else | 
|---|
| 112 | { | 
|---|
| 113 | for (PlayerInfo* info : ObjectList<PlayerInfo>()) | 
|---|
| 114 | if (info->getClientID() == clientID) | 
|---|
| 115 | return info; | 
|---|
| 116 | } | 
|---|
| 117 | return nullptr; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|