Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sfxSebastian/src/orxonox/infos/PlayerInfo.cc @ 9902

Last change on this file since 9902 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 9.1 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        this->controllableEntity_->getController()->setActive(false);
232        //this->controllableEntity_->getController()->setControllableEntity(NULL);
233        this->controllableEntity_->setController(0);
234    }
235
236    void PlayerInfo::stopTemporaryControl()
237    {
238        ControllableEntity* entity = this->controllableEntity_;
239
240        assert(this->controllableEntity_ != NULL);
241        if( !entity || this->previousControllableEntity_.size() == 0 )
242            return;
243
244        this->controllableEntity_->setController(0);
245        if(this->isHumanPlayer()) // TODO: Multiplayer?
246            this->controllableEntity_->destroyHud(); // HACK-ish
247
248//        this->controllableEntity_ = this->previousControllableEntity_.back();
249        do {
250            this->controllableEntity_ = this->previousControllableEntity_.back();
251        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
252        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
253        this->previousControllableEntity_.pop_back();
254
255        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
256            this->controller_->setControllableEntity(this->controllableEntity_);
257
258         // HACK-ish
259        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
260            this->controllableEntity_->createHud();
261
262        if ( GameMode::isMaster() )
263            entity->removePlayer();
264
265        this->changedControllableEntity();
266    }
267
268    void PlayerInfo::networkcallback_changedcontrollableentityID()
269    {
270        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
271        {
272            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
273            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
274            this->startControl(entity);
275        }
276        else
277        {
278            this->stopControl();
279        }
280    }
281
282
283    void PlayerInfo::networkcallback_changedgtinfoID()
284    {
285        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
286        {
287            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
288
289            if (!this->gtinfo_)
290                this->gtinfoID_ = OBJECTID_UNKNOWN;
291        }
292    }
293}
Note: See TracBrowser for help on using the repository browser.