Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial2/src/orxonox/infos/PlayerInfo.cc @ 10177

Last change on this file since 10177 was 9016, checked in by jo, 13 years ago

Merging presentation2011 branch to trunk. Please check for possible bugs.

  • 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 "worldentities/pawns/SpaceShip.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                SpaceShip* spaceship = dynamic_cast<SpaceShip*>(entity);
175        if (spaceship != NULL)
176        {
177            spaceship->setRVName(this->getName());
178        }
179    }
180
181    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
182    {
183        if (!entity)
184            return;
185
186        this->controllableEntity_->destroyHud(); // HACK-ish
187        this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_));
188        this->controllableEntity_ = entity;
189        this->controllableEntityID_ = entity->getObjectID();
190
191        entity->setPlayer(this);
192        entity->setController(this->controller_);
193
194        if (this->controller_)
195            this->controller_->setControllableEntity(entity);
196
197        this->changedControllableEntity();
198    }
199
200    void PlayerInfo::stopControl()
201    {
202        while ( this->previousControllableEntity_.size() > 0)
203            this->stopTemporaryControl();
204
205        ControllableEntity* entity = this->controllableEntity_;
206
207        if (!entity)
208            return;
209
210        this->controllableEntity_->setController(0);
211        this->controllableEntity_ = 0;
212        this->controllableEntityID_ = OBJECTID_UNKNOWN;
213
214        if (this->controller_)
215            this->controller_->setControllableEntity(0);
216
217        if ( GameMode::isMaster() )
218            entity->removePlayer();
219
220        this->changedControllableEntity();
221    }
222
223    void PlayerInfo::pauseControl()
224    {
225        ControllableEntity* entity = this->controllableEntity_;
226
227        if (!entity)
228            return;
229
230        this->controllableEntity_->getController()->setActive(false);
231        //this->controllableEntity_->getController()->setControllableEntity(NULL);
232        this->controllableEntity_->setController(0);
233    }
234
235    void PlayerInfo::stopTemporaryControl()
236    {
237        ControllableEntity* entity = this->controllableEntity_;
238
239        assert(this->controllableEntity_ != NULL);
240        if( !entity || this->previousControllableEntity_.size() == 0 )
241            return;
242
243        this->controllableEntity_->setController(0);
244        if(this->isHumanPlayer()) // TODO: Multiplayer?
245            this->controllableEntity_->destroyHud(); // HACK-ish
246       
247//        this->controllableEntity_ = this->previousControllableEntity_.back();
248        do {
249            this->controllableEntity_ = this->previousControllableEntity_.back();
250        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
251        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
252        this->previousControllableEntity_.pop_back();
253
254        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
255            this->controller_->setControllableEntity(this->controllableEntity_);
256
257         // HACK-ish
258        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
259            this->controllableEntity_->createHud();
260
261        if ( GameMode::isMaster() )
262            entity->removePlayer();
263
264        this->changedControllableEntity();
265    }
266
267    void PlayerInfo::networkcallback_changedcontrollableentityID()
268    {
269        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
270        {
271            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
272            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
273            this->startControl(entity);
274        }
275        else
276        {
277            this->stopControl();
278        }
279    }
280
281
282    void PlayerInfo::networkcallback_changedgtinfoID()
283    {
284        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
285        {
286            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
287
288            if (!this->gtinfo_)
289                this->gtinfoID_ = OBJECTID_UNKNOWN;
290        }
291    }
292}
Note: See TracBrowser for help on using the repository browser.