Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/dockingsystem2/src/orxonox/infos/PlayerInfo.cc @ 8804

Last change on this file since 8804 was 8544, checked in by sven, 13 years ago

Spaceships attach now to the dock, fixed DockingController.

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