Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/menuanimations/src/orxonox/infos/HumanPlayer.cc @ 6518

Last change on this file since 6518 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

  • Property svn:eol-style set to native
File size: 6.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 "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 "controllers/HumanController.h"
37#include "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                this->humanHud_->destroy();
67
68            if (this->gametypeHud_)
69                this->gametypeHud_->destroy();
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->setSyncMode(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        {
165            if (this->getGametype() && this->getGametype()->getHUDTemplate() != "")
166                this->setGametypeHUDTemplate(this->getGametype()->getHUDTemplate());
167            else
168                this->setGametypeHUDTemplate("");
169        }
170    }
171
172    void HumanPlayer::updateHumanHUD()
173    {
174        if (this->humanHud_)
175        {
176            this->humanHud_->destroy();
177            this->humanHud_ = 0;
178        }
179
180        if (this->isLocalPlayer() && this->humanHudTemplate_ != "" && GameMode::showsGraphics())
181        {
182            this->humanHud_ = new OverlayGroup(this);
183            this->humanHud_->addTemplate(this->humanHudTemplate_);
184            this->humanHud_->setOwner(this);
185        }
186    }
187
188    void HumanPlayer::updateGametypeHUD()
189    {
190        if (this->gametypeHud_)
191        {
192            this->gametypeHud_->destroy();
193            this->gametypeHud_ = 0;
194        }
195
196        if (this->isLocalPlayer() && this->gametypeHudTemplate_ != "")
197        {
198            this->gametypeHud_ = new OverlayGroup(this);
199            this->gametypeHud_->addTemplate(this->gametypeHudTemplate_);
200            this->gametypeHud_->setOwner(this);
201        }
202    }
203}
Note: See TracBrowser for help on using the repository browser.