Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/archive/tutorialHS09/src/orxonox/PlayerManager.cc @ 9630

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

merged libraries2 back to trunk

  • 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            COUT(3) << "client disconnected" << std::endl;
77
78            // remove from clients-map
79            PlayerInfo* player = this->clients_[clientID];
80            this->clients_.erase(clientID);
81
82            if (LevelManager::getInstancePtr() && LevelManager::getInstance().getActiveLevel())
83                LevelManager::getInstance().getActiveLevel()->playerLeft(player);
84
85            // delete PlayerInfo instance
86            if (player)
87                delete player;
88        }
89    }
90
91
92    PlayerInfo* PlayerManager::getClient(unsigned int clientID) const
93    {
94        if (GameMode::isMaster())
95        {
96            std::map<unsigned int, PlayerInfo*>::const_iterator it = this->clients_.find(clientID);
97            if (it != this->clients_.end())
98                return it->second;
99        }
100        else
101        {
102            for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
103                if (it->getClientID() == clientID)
104                    return (*it);
105        }
106        return 0;
107    }
108}
Note: See TracBrowser for help on using the repository browser.