Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/orxonox/PlayerManager.cc @ 5820

Last change on this file since 5820 was 5820, checked in by landauf, 15 years ago

Changes in ClientConnectionListener:

  • Added static functions for connect/disconnect which notify all instances
  • getConnectedClients() also returned client "0" which is the ID of the server. This is important for the PlayerManager, but other classes (like for example TrafficControl) don't use getConnectedClients(), so I assumed I can safely remove this line. The 0-client gets now connected and disconnected in GSLevel (which also creates and destroys the PlayerManager). If the 0-client is also needed by other classes, I suggest moving it into the network.

The instance of HumanPlayer is now deleted if GSLevel gets deactivated. This also destroys the default HUD.

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
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 "Level.h"
34#include "infos/HumanPlayer.h"
35#include "LevelManager.h"
36
37namespace orxonox
38{
39    PlayerManager* PlayerManager::singletonPtr_s = 0;
40
41    PlayerManager::PlayerManager()
42    {
43        RegisterRootObject(PlayerManager);
44
45        this->getConnectedClients();
46    }
47
48    PlayerManager::~PlayerManager()
49    {
50    }
51
52    void PlayerManager::clientConnected(unsigned int clientID)
53    {
54        if (GameMode::isMaster())
55        {
56            if (clientID != 0)
57                COUT(3) << "client connected" << std::endl;
58
59            // create new HumanPlayer instance
60            HumanPlayer* player = new HumanPlayer(0);
61            player->setClientID(clientID);
62
63            // add to clients-map
64            assert(!this->clients_[clientID]);
65            this->clients_[clientID] = player;
66
67            if (LevelManager::getInstancePtr() && LevelManager::getInstance().getActiveLevel())
68                LevelManager::getInstance().getActiveLevel()->playerEntered(player);
69        }
70    }
71
72    void PlayerManager::clientDisconnected(unsigned int clientID)
73    {
74        if (GameMode::isMaster())
75        {
76            if (clientID != 0)
77                COUT(3) << "client disconnected" << std::endl;
78
79            // remove from clients-map
80            PlayerInfo* player = this->clients_[clientID];
81            this->clients_.erase(clientID);
82
83            if (LevelManager::getInstancePtr() && LevelManager::getInstance().getActiveLevel())
84                LevelManager::getInstance().getActiveLevel()->playerLeft(player);
85
86            // delete PlayerInfo instance
87            if (player)
88                player->destroy();
89        }
90    }
91
92
93    PlayerInfo* PlayerManager::getClient(unsigned int clientID) const
94    {
95        if (GameMode::isMaster())
96        {
97            std::map<unsigned int, PlayerInfo*>::const_iterator it = this->clients_.find(clientID);
98            if (it != this->clients_.end())
99                return it->second;
100        }
101        else
102        {
103            for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
104                if (it->getClientID() == clientID)
105                    return (*it);
106        }
107        return 0;
108    }
109}
Note: See TracBrowser for help on using the repository browser.