Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/infos/HumanPlayer.cc @ 5732

Last change on this file since 5732 was 3280, checked in by rgrieder, 15 years ago

Merged most of the core4 revisions back to the trunk except for:

  • orxonox_cast
  • all the radical changes in the input library
  • Property svn:eol-style set to native
File size: 6.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 "HumanPlayer.h"
30
31#include "core/CoreIncludes.h"
32#include "core/ConfigValueIncludes.h"
33#include "core/GameMode.h"
34#include "network/ClientInformation.h"
35#include "network/Host.h"
36#include "objects/controllers/HumanController.h"
37#include "objects/gametypes/Gametype.h"
38#include "overlays/OverlayGroup.h"
39
40namespace orxonox
41{
42    CreateUnloadableFactory(HumanPlayer);
43
44    HumanPlayer::HumanPlayer(BaseObject* creator) : PlayerInfo(creator)
45    {
46        RegisterObject(HumanPlayer);
47
48        this->server_initialized_ = GameMode::isMaster();
49        this->client_initialized_ = false;
50
51        this->bHumanPlayer_ = true;
52        this->defaultController_ = Class(HumanController);
53
54        this->humanHud_ = 0;
55        this->gametypeHud_ = 0;
56
57        this->setConfigValues();
58        this->registerVariables();
59    }
60
61    HumanPlayer::~HumanPlayer()
62    {
63        if (this->BaseObject::isInitialized())
64        {
65            if (this->humanHud_)
66                delete this->humanHud_;
67
68            if (this->gametypeHud_)
69                delete this->gametypeHud_;
70        }
71    }
72
73    void HumanPlayer::setConfigValues()
74    {
75        SetConfigValue(nick_, "Player").callback(this, &HumanPlayer::configvaluecallback_changednick);
76        SetConfigValue(hudtemplate_, "defaultHUD").callback(this, &HumanPlayer::configvaluecallback_changedHUDTemplate);
77    }
78
79    void HumanPlayer::registerVariables()
80    {
81        registerVariable(this->synchronize_nick_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_changednick));
82
83        registerVariable(this->clientID_,           VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_clientIDchanged));
84        registerVariable(this->server_initialized_, VariableDirection::ToClient, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_server_initialized));
85        registerVariable(this->client_initialized_, VariableDirection::ToServer, new NetworkCallback<HumanPlayer>(this, &HumanPlayer::networkcallback_client_initialized));
86    }
87
88    void HumanPlayer::configvaluecallback_changednick()
89    {
90        if (this->isLocalPlayer())
91        {
92            this->synchronize_nick_ = this->nick_;
93
94            if (GameMode::isMaster())
95                this->setName(this->nick_);
96        }
97    }
98
99    void HumanPlayer::configvaluecallback_changedHUDTemplate()
100    {
101        this->setHumanHUDTemplate(this->hudtemplate_);
102    }
103
104    void HumanPlayer::networkcallback_changednick()
105    {
106        this->setName(this->synchronize_nick_);
107    }
108
109    void HumanPlayer::networkcallback_clientIDchanged()
110    {
111        if (this->clientID_ == Host::getPlayerID())
112        {
113            this->bLocalPlayer_ = true;
114            this->synchronize_nick_ = this->nick_;
115            this->client_initialized_ = true;
116
117            if (!GameMode::isMaster())
118                this->setObjectMode(ObjectDirection::Bidirectional);
119            else
120                this->setName(this->nick_);
121
122            this->createController();
123            this->updateHumanHUD();
124        }
125    }
126
127    void HumanPlayer::networkcallback_server_initialized()
128    {
129        this->client_initialized_ = true;
130    }
131
132    void HumanPlayer::networkcallback_client_initialized()
133    {
134        if (this->getGametype())
135            this->getGametype()->playerEntered(this);
136    }
137
138    bool HumanPlayer::isInitialized() const
139    {
140        return (this->server_initialized_ && this->client_initialized_);
141    }
142
143    float HumanPlayer::getPing() const
144    {
145        return static_cast<float>(ClientInformation::findClient(this->getClientID())->getRTT());
146    }
147
148    float HumanPlayer::getPacketLossRatio() const
149    {
150        return static_cast<float>(ClientInformation::findClient(this->getClientID())->getPacketLoss());
151    }
152
153    void HumanPlayer::setClientID(unsigned int clientID)
154    {
155        this->clientID_ = clientID;
156        this->networkcallback_clientIDchanged();
157    }
158
159    void HumanPlayer::changedGametype()
160    {
161        PlayerInfo::changedGametype();
162
163        if (this->isInitialized() && this->isLocalPlayer())
164            if (this->getGametype()->getHUDTemplate() != "")
165                this->setGametypeHUDTemplate(this->getGametype()->getHUDTemplate());
166    }
167
168    void HumanPlayer::updateHumanHUD()
169    {
170        if (this->humanHud_)
171        {
172            delete this->humanHud_;
173            this->humanHud_ = 0;
174        }
175
176        if (this->isLocalPlayer() && this->humanHudTemplate_ != "")
177        {
178            this->humanHud_ = new OverlayGroup(this);
179            this->humanHud_->addTemplate(this->humanHudTemplate_);
180            this->humanHud_->setOwner(this);
181        }
182    }
183
184    void HumanPlayer::updateGametypeHUD()
185    {
186        if (this->gametypeHud_)
187        {
188            delete this->gametypeHud_;
189            this->gametypeHud_ = 0;
190        }
191
192        if (this->isLocalPlayer() && this->gametypeHudTemplate_ != "")
193        {
194            this->gametypeHud_ = new OverlayGroup(this);
195            this->gametypeHud_->addTemplate(this->gametypeHudTemplate_);
196            this->gametypeHud_->setOwner(this);
197        }
198    }
199}
Note: See TracBrowser for help on using the repository browser.