Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9305 was 9257, checked in by landauf, 12 years ago

renamed RVName to radarname
cast to RadarViewable instead of SpaceShip to set the name (in PlayerInfo)

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