Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/PlayerManager.cc @ 2219

Last change on this file since 2219 was 2171, checked in by landauf, 16 years ago

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