Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added several new classes

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