Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/campaignHS15/src/orxonox/infos/PlayerInfo.cc @ 10875

Last change on this file since 10875 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 9.3 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_ = 0;
54        this->controllableEntity_ = 0;
55        this->controllableEntityID_ = OBJECTID_UNKNOWN;
56
57        this->gtinfo_ = 0;
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_ = 0;
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_ = 0;
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_ = 0;
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 != NULL)
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
206    void PlayerInfo::stopControl()
207    {
208        while ( this->previousControllableEntity_.size() > 0)
209            this->stopTemporaryControl();
210
211        ControllableEntity* entity = this->controllableEntity_;
212
213        if (!entity)
214            return;
215
216        this->controllableEntity_->setController(0);
217        this->controllableEntity_ = 0;
218        this->controllableEntityID_ = OBJECTID_UNKNOWN;
219
220        if (this->controller_)
221            this->controller_->setControllableEntity(0);
222
223        if ( GameMode::isMaster() )
224            entity->removePlayer();
225
226        this->changedControllableEntity();
227    }
228
229    void PlayerInfo::pauseControl()
230    {
231        ControllableEntity* entity = this->controllableEntity_;
232
233        if (!entity)
234            return;
235
236        Controller* tmp =this->controllableEntity_->getController();
237        if (tmp == NULL)
238        {
239            orxout(verbose) <<  "PlayerInfo: pauseControl, Controller is NULL " << endl;
240            return;
241        }
242        tmp->setActive(false);
243        //this->controllableEntity_->getController()->setControllableEntity(NULL);
244        this->controllableEntity_->setController(0);
245    }
246
247    void PlayerInfo::stopTemporaryControl()
248    {
249        ControllableEntity* entity = this->controllableEntity_;
250
251        assert(this->controllableEntity_ != NULL);
252        if( !entity || this->previousControllableEntity_.size() == 0 )
253            return;
254
255        this->controllableEntity_->setController(0);
256        if(this->isHumanPlayer()) // TODO: Multiplayer?
257            this->controllableEntity_->destroyHud(); // HACK-ish
258
259//        this->controllableEntity_ = this->previousControllableEntity_.back();
260        do {
261            this->controllableEntity_ = this->previousControllableEntity_.back();
262        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
263        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
264        this->previousControllableEntity_.pop_back();
265
266        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
267            this->controller_->setControllableEntity(this->controllableEntity_);
268
269         // HACK-ish
270        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
271            this->controllableEntity_->createHud();
272
273        if ( GameMode::isMaster() )
274            entity->removePlayer();
275
276        this->changedControllableEntity();
277    }
278
279    void PlayerInfo::networkcallback_changedcontrollableentityID()
280    {
281        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
282        {
283            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
284            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
285            this->startControl(entity);
286        }
287        else
288        {
289            this->stopControl();
290        }
291    }
292
293
294    void PlayerInfo::networkcallback_changedgtinfoID()
295    {
296        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
297        {
298            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
299
300            if (!this->gtinfo_)
301                this->gtinfoID_ = OBJECTID_UNKNOWN;
302        }
303    }
304}
Note: See TracBrowser for help on using the repository browser.