Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

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