Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/usability/src/orxonox/infos/PlayerInfo.cc @ 7998

Last change on this file since 7998 was 7163, checked in by dafrick, 14 years ago

Merged presentation3 branch into trunk.

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