Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/orxonox/infos/HumanPlayer.cc @ 12073

Last change on this file since 12073 was 12028, checked in by merholzl, 6 years ago

added scriptable controller

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