Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/infos/PlayerInfo.cc @ 12012

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

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 9.5 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 <cassert>
30
31#include "PlayerInfo.h"
32
33#include "core/CoreIncludes.h"
34// #include "network/ClientInformation.h"
35#include "gametypes/Gametype.h"
36#include "worldentities/ControllableEntity.h"
37#include "controllers/Controller.h"
38#include "interfaces/RadarViewable.h"
39
40namespace orxonox
41{
42    RegisterAbstractClass(PlayerInfo).inheritsFrom<Info>();
43
44    PlayerInfo::PlayerInfo(Context* context) : Info(context)
45    {
46        RegisterObject(PlayerInfo);
47
48        this->clientID_ = NETWORK_PEER_ID_UNKNOWN;
49        this->bHumanPlayer_ = false;
50        this->bLocalPlayer_ = false;
51        this->bReadyToSpawn_ = false;
52        this->bSetUnreadyAfterSpawn_ = true;
53        this->controller_ = nullptr;
54        this->controllableEntity_ = nullptr;
55        this->controllableEntityID_ = OBJECTID_UNKNOWN;
56
57        this->gtinfo_ = nullptr;
58        this->gtinfoID_ = OBJECTID_UNKNOWN;
59        this->updateGametypeInfo(this->getGametype());
60
61        this->registerVariables();
62
63    }
64
65    PlayerInfo::~PlayerInfo()
66    {
67        if (this->BaseObject::isInitialized())
68        {
69            this->stopControl();
70
71            if (this->controller_)
72            {
73                this->controller_->destroy();
74                this->controller_ = nullptr;
75            }
76
77            if (this->getGametype())
78                this->getGametype()->playerLeft(this);
79        }
80    }
81
82    void PlayerInfo::registerVariables()
83    {
84        registerVariable(this->name_,                 VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::changedName));
85        registerVariable(this->controllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
86        registerVariable(this->gtinfoID_,             VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedgtinfoID));
87    }
88
89    void PlayerInfo::changedName()
90    {
91        SUPER(PlayerInfo, changedName);
92
93        if (this->isInitialized() && this->getGametype())
94            this->getGametype()->playerChangedName(this);
95    }
96
97    void PlayerInfo::switchGametype(Gametype* gametype)
98    {
99        Gametype* oldGametype = this->getGametype();
100        this->setGametype(StrongPtr<Gametype>(gametype));
101        Gametype* newGametype = this->getGametype();
102
103
104        this->updateGametypeInfo(newGametype);
105
106        if (this->isInitialized())
107        {
108            if (oldGametype)
109            {
110                if (newGametype)
111                    oldGametype->playerSwitched(this, newGametype);
112                else
113                    oldGametype->playerLeft(this);
114            }
115
116            if (newGametype)
117            {
118                if (oldGametype)
119                    newGametype->playerSwitchedBack(this, oldGametype);
120                else
121                    newGametype->playerEntered(this);
122            }
123        }
124    }
125
126    void PlayerInfo::updateGametypeInfo(Gametype* gametype)
127    {
128        this->gtinfo_ = nullptr;
129        this->gtinfoID_ = OBJECTID_UNKNOWN;
130
131        if (gametype && gametype->getGametypeInfo())
132        {
133            this->gtinfo_ = gametype->getGametypeInfo();
134            this->gtinfoID_ = this->gtinfo_->getObjectID();
135        }
136    }
137
138    void PlayerInfo::createController()
139    {
140        if (this->controller_)
141        {
142            this->controller_->destroy();
143            this->controller_ = nullptr;
144        }
145        this->controller_ = this->defaultController_.fabricate(this->getContext());
146        assert(this->controller_);
147        this->controller_->setPlayer(this);
148        if (this->controllableEntity_)
149        {
150            this->controller_->setControllableEntity(this->controllableEntity_);
151            this->controllableEntity_->setController(this->controller_);
152        }
153        this->changedController();
154    }
155
156    void PlayerInfo::startControl(ControllableEntity* entity)
157    {
158        if (!entity || entity == this->controllableEntity_)
159            return;
160
161        while (this->previousControllableEntity_.size() > 0)
162            this->stopTemporaryControl();
163
164        if (this->controllableEntity_)
165            this->stopControl();
166
167        this->controllableEntity_ = entity;
168        this->controllableEntityID_ = entity->getObjectID();
169
170        entity->setPlayer(this);
171
172        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
173
174        if (this->controller_)
175        {
176            this->controller_->setControllableEntity(entity);
177            this->controllableEntity_->setController(this->controller_);
178        }
179
180        this->changedControllableEntity();
181
182        RadarViewable* radarviewable = orxonox_cast<RadarViewable*>(entity);
183        if (radarviewable != nullptr)
184            radarviewable->setRadarName(this->getName());
185    }
186
187    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
188    {
189        if (!entity)
190            return;
191
192        this->controllableEntity_->destroyHud(); // HACK-ish
193        this->previousControllableEntity_.push_back(this->controllableEntity_);
194        this->controllableEntity_ = entity;
195        this->controllableEntityID_ = entity->getObjectID();
196
197        entity->setPlayer(this);
198        entity->setController(this->controller_);
199
200        if (this->controller_)
201            this->controller_->setControllableEntity(entity);
202
203        this->changedControllableEntity();
204
205         // HACK-ish
206        if(this->isHumanPlayer())
207            entity->createHud();
208    }
209
210    void PlayerInfo::stopControl()
211    {
212        while ( this->previousControllableEntity_.size() > 0)
213            this->stopTemporaryControl();
214
215        ControllableEntity* entity = this->controllableEntity_;
216
217        if (!entity)
218            return;
219
220        this->controllableEntity_->setController(nullptr);
221        this->controllableEntity_ = nullptr;
222        this->controllableEntityID_ = OBJECTID_UNKNOWN;
223
224        if (this->controller_)
225            this->controller_->setControllableEntity(nullptr);
226
227        if ( GameMode::isMaster() )
228            entity->removePlayer();
229
230        this->changedControllableEntity();
231    }
232
233    void PlayerInfo::pauseControl()
234    {
235        ControllableEntity* entity = this->controllableEntity_;
236
237        if (!entity)
238            return;
239
240        Controller* tmp =this->controllableEntity_->getController();
241        if (tmp == nullptr)
242        {
243            orxout(verbose) <<  "PlayerInfo: pauseControl, Controller is nullptr " << endl;
244            return;
245        }
246        tmp->setActive(false);
247        //this->controllableEntity_->getController()->setControllableEntity(nullptr);
248        this->controllableEntity_->setController(nullptr);
249    }
250
251    void PlayerInfo::stopTemporaryControl()
252    {
253        ControllableEntity* entity = this->controllableEntity_;
254
255        assert(this->controllableEntity_ != nullptr);
256        if( !entity || this->previousControllableEntity_.size() == 0 )
257            return;
258
259        entity->destroyHud(); // HACK-ish
260
261        this->controllableEntity_->setController(nullptr);
262        if(this->isHumanPlayer()) // TODO: Multiplayer?
263            this->controllableEntity_->destroyHud(); // HACK-ish
264
265//        this->controllableEntity_ = this->previousControllableEntity_.back();
266        do {
267            this->controllableEntity_ = this->previousControllableEntity_.back();
268        } while(this->controllableEntity_ == nullptr && this->previousControllableEntity_.size() > 0);
269        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
270        this->previousControllableEntity_.pop_back();
271
272        if ( this->controllableEntity_ != nullptr && this->controller_ != nullptr)
273            this->controller_->setControllableEntity(this->controllableEntity_);
274
275         // HACK-ish
276        if(this->controllableEntity_ != nullptr && this->isHumanPlayer())
277            this->controllableEntity_->createHud();
278
279        if ( GameMode::isMaster() )
280            entity->removePlayer();
281
282        this->changedControllableEntity();
283    }
284
285    void PlayerInfo::networkcallback_changedcontrollableentityID()
286    {
287        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
288        {
289            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
290            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
291            this->startControl(entity);
292        }
293        else
294        {
295            this->stopControl();
296        }
297    }
298
299
300    void PlayerInfo::networkcallback_changedgtinfoID()
301    {
302        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
303        {
304            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
305
306            if (!this->gtinfo_)
307                this->gtinfoID_ = OBJECTID_UNKNOWN;
308        }
309    }
310}
Note: See TracBrowser for help on using the repository browser.