Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/gametypes/Gametype.cc @ 1989

Last change on this file since 1989 was 1989, checked in by landauf, 16 years ago
  • added ControllableEntity, the baseclass of all players, vehicles and ships.
  • moved Template to core
  • some changes in Camera
  • added 6 constants to WorldEntity to identify relative directions
  • changed vom Radian to Degree as default angle unit
File size: 5.0 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 "Gametype.h"
31
32#include "core/CoreIncludes.h"
33#include "core/ConsoleCommand.h"
34#include "objects/infos/PlayerInfo.h"
35
36#include "network/Host.h"
37
38namespace orxonox
39{
40    SetConsoleCommand(Gametype, listPlayers, true);
41
42    CreateUnloadableFactory(Gametype);
43
44    Gametype::Gametype()
45    {
46        RegisterObject(Gametype);
47
48        this->getConnectedClients();
49
50        COUT(0) << "created Gametype" << std::endl;
51    }
52
53    Gametype* Gametype::getCurrentGametype()
54    {
55        for (ObjectList<Gametype>::iterator it = ObjectList<Gametype>::begin(); it != ObjectList<Gametype>::end(); ++it)
56            return (*it);
57
58        return 0;
59    }
60
61    PlayerInfo* Gametype::getClient(unsigned int clientID)
62    {
63        Gametype* gametype = Gametype::getCurrentGametype();
64        if (gametype)
65        {
66            std::map<unsigned int, PlayerInfo*>::const_iterator it = gametype->clients_.find(clientID);
67            if (it != gametype->clients_.end())
68                return it->second;
69        }
70        else
71        {
72            for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
73                if (it->getClientID() == clientID)
74                    return (*it);
75        }
76        return 0;
77    }
78
79    void Gametype::listPlayers()
80    {
81        Gametype* gametype = Gametype::getCurrentGametype();
82
83        if (gametype)
84        {
85            for (std::set<PlayerInfo*>::const_iterator it = gametype->players_.begin(); it != gametype->players_.end(); ++it)
86                COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl;
87        }
88        else
89        {
90            for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it)
91                COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl;
92        }
93    }
94
95    void Gametype::clientConnected(unsigned int clientID)
96    {
97        COUT(0) << "client connected" << std::endl;
98
99        // create new PlayerInfo instance
100        PlayerInfo* player = new PlayerInfo();
101        player->setClientID(clientID);
102
103        // add to clients-map
104        assert(!this->clients_[clientID]);
105        this->clients_[clientID] = player;
106    }
107
108    void Gametype::clientDisconnected(unsigned int clientID)
109    {
110        COUT(0) << "client disconnected" << std::endl;
111
112        // remove from clients-map
113        PlayerInfo* player = this->clients_[clientID];
114        this->clients_.erase(clientID);
115
116        // delete PlayerInfo instance
117        delete player;
118    }
119
120    void Gametype::addPlayer(PlayerInfo* player)
121    {
122        this->players_.insert(player);
123        this->playerJoined(player);
124
125        ControllableEntity* newpawn = this->defaultPawn_.fabricate();
126        player->startControl(newpawn);
127    }
128
129    void Gametype::removePlayer(PlayerInfo* player)
130    {
131        player->stopControl();
132        this->players_.erase(player);
133        this->playerLeft(player);
134    }
135
136    void Gametype::playerJoined(PlayerInfo* player)
137    {
138        std::string message = player->getName() + " entered the game";
139        COUT(0) << message << std::endl;
140        network::Host::Broadcast(message);
141    }
142
143    void Gametype::playerLeft(PlayerInfo* player)
144    {
145        std::string message = player->getName() + " left the game";
146        COUT(0) << message << std::endl;
147        network::Host::Broadcast(message);
148    }
149
150    void Gametype::playerChangedName(PlayerInfo* player)
151    {
152        if (this->players_.find(player) != this->players_.end())
153        {
154            if (player->getName() != player->getOldName())
155            {
156                std::string message = player->getOldName() + " changed name to " + player->getName();
157                COUT(0) << message << std::endl;
158                network::Host::Broadcast(message);
159            }
160        }
161    }
162}
Note: See TracBrowser for help on using the repository browser.