Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10294 was 9945, checked in by landauf, 11 years ago

replaced tabs with spaces. no changes in code

  • Property svn:eol-style set to native
File size: 9.2 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(Class(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();
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::changedGametype()
98    {
99        this->updateGametypeInfo();
100
101        if (this->isInitialized())
102        {
103            if (this->getOldGametype())
104            {
105                if (this->getGametype())
106                    this->getOldGametype()->playerSwitched(this, this->getGametype());
107                else
108                    this->getOldGametype()->playerLeft(this);
109            }
110
111            if (this->getGametype())
112            {
113                if (this->getOldGametype())
114                    this->getGametype()->playerSwitchedBack(this, this->getOldGametype());
115                else
116                    this->getGametype()->playerEntered(this);
117            }
118        }
119    }
120
121    void PlayerInfo::updateGametypeInfo()
122    {
123        this->gtinfo_ = 0;
124        this->gtinfoID_ = OBJECTID_UNKNOWN;
125
126        if (this->getGametype() && this->getGametype()->getGametypeInfo())
127        {
128            this->gtinfo_ = this->getGametype()->getGametypeInfo();
129            this->gtinfoID_ = this->gtinfo_->getObjectID();
130        }
131    }
132
133    void PlayerInfo::createController()
134    {
135        if (this->controller_)
136        {
137            this->controller_->destroy();
138            this->controller_ = 0;
139        }
140        this->controller_ = this->defaultController_.fabricate(this->getContext());
141        assert(this->controller_);
142        this->controller_->setPlayer(this);
143        if (this->controllableEntity_)
144        {
145            this->controller_->setControllableEntity(this->controllableEntity_);
146            this->controllableEntity_->setController(this->controller_);
147        }
148        this->changedController();
149    }
150
151    void PlayerInfo::startControl(ControllableEntity* entity)
152    {
153        if (!entity || entity == this->controllableEntity_)
154            return;
155
156        while (this->previousControllableEntity_.size() > 0)
157            this->stopTemporaryControl();
158
159        if (this->controllableEntity_)
160            this->stopControl();
161
162        this->controllableEntity_ = entity;
163        this->controllableEntityID_ = entity->getObjectID();
164
165        entity->setPlayer(this);
166
167        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
168
169        if (this->controller_)
170        {
171            this->controller_->setControllableEntity(entity);
172            this->controllableEntity_->setController(this->controller_);
173        }
174
175        this->changedControllableEntity();
176
177        RadarViewable* radarviewable = orxonox_cast<RadarViewable*>(entity);
178        if (radarviewable != NULL)
179            radarviewable->setRadarName(this->getName());
180    }
181
182    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
183    {
184        if (!entity)
185            return;
186
187        this->controllableEntity_->destroyHud(); // HACK-ish
188        this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_));
189        this->controllableEntity_ = entity;
190        this->controllableEntityID_ = entity->getObjectID();
191
192        entity->setPlayer(this);
193        entity->setController(this->controller_);
194
195        if (this->controller_)
196            this->controller_->setControllableEntity(entity);
197
198        this->changedControllableEntity();
199    }
200
201    void PlayerInfo::stopControl()
202    {
203        while ( this->previousControllableEntity_.size() > 0)
204            this->stopTemporaryControl();
205
206        ControllableEntity* entity = this->controllableEntity_;
207
208        if (!entity)
209            return;
210
211        this->controllableEntity_->setController(0);
212        this->controllableEntity_ = 0;
213        this->controllableEntityID_ = OBJECTID_UNKNOWN;
214
215        if (this->controller_)
216            this->controller_->setControllableEntity(0);
217
218        if ( GameMode::isMaster() )
219            entity->removePlayer();
220
221        this->changedControllableEntity();
222    }
223
224    void PlayerInfo::pauseControl()
225    {
226        ControllableEntity* entity = this->controllableEntity_;
227
228        if (!entity)
229            return;
230
231        Controller* tmp =this->controllableEntity_->getController();
232        if (tmp == NULL)
233        {
234            orxout(verbose) <<  "PlayerInfo: pauseControl, Controller is NULL " << endl;
235            return;
236        }
237        tmp->setActive(false);
238        //this->controllableEntity_->getController()->setControllableEntity(NULL);
239        this->controllableEntity_->setController(0);
240    }
241
242    void PlayerInfo::stopTemporaryControl()
243    {
244        ControllableEntity* entity = this->controllableEntity_;
245
246        assert(this->controllableEntity_ != NULL);
247        if( !entity || this->previousControllableEntity_.size() == 0 )
248            return;
249
250        this->controllableEntity_->setController(0);
251        if(this->isHumanPlayer()) // TODO: Multiplayer?
252            this->controllableEntity_->destroyHud(); // HACK-ish
253
254//        this->controllableEntity_ = this->previousControllableEntity_.back();
255        do {
256            this->controllableEntity_ = this->previousControllableEntity_.back();
257        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
258        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
259        this->previousControllableEntity_.pop_back();
260
261        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
262            this->controller_->setControllableEntity(this->controllableEntity_);
263
264         // HACK-ish
265        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
266            this->controllableEntity_->createHud();
267
268        if ( GameMode::isMaster() )
269            entity->removePlayer();
270
271        this->changedControllableEntity();
272    }
273
274    void PlayerInfo::networkcallback_changedcontrollableentityID()
275    {
276        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
277        {
278            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
279            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
280            this->startControl(entity);
281        }
282        else
283        {
284            this->stopControl();
285        }
286    }
287
288
289    void PlayerInfo::networkcallback_changedgtinfoID()
290    {
291        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
292        {
293            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
294
295            if (!this->gtinfo_)
296                this->gtinfoID_ = OBJECTID_UNKNOWN;
297        }
298    }
299}
Note: See TracBrowser for help on using the repository browser.