Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added scriptable controller

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