Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10336 was 9945, checked in by landauf, 11 years ago

replaced tabs with spaces. no changes in code

  • Property svn:eol-style set to native
File size: 9.2 KB
RevLine 
[2072]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"
[8327]34// #include "network/ClientInformation.h"
[5735]35#include "gametypes/Gametype.h"
36#include "worldentities/ControllableEntity.h"
[5929]37#include "controllers/Controller.h"
[9257]38#include "interfaces/RadarViewable.h"
[2072]39
40namespace orxonox
41{
[9667]42    RegisterAbstractClass(PlayerInfo).inheritsFrom(Class(Info));
43
44    PlayerInfo::PlayerInfo(Context* context) : Info(context)
[2072]45    {
46        RegisterObject(PlayerInfo);
47
[8327]48        this->clientID_ = NETWORK_PEER_ID_UNKNOWN;
[2072]49        this->bHumanPlayer_ = false;
50        this->bLocalPlayer_ = false;
51        this->bReadyToSpawn_ = false;
[2662]52        this->bSetUnreadyAfterSpawn_ = true;
[2072]53        this->controller_ = 0;
54        this->controllableEntity_ = 0;
[6417]55        this->controllableEntityID_ = OBJECTID_UNKNOWN;
[2072]56
[2973]57        this->gtinfo_ = 0;
58        this->gtinfoID_ = OBJECTID_UNKNOWN;
59        this->updateGametypeInfo();
60
[2072]61        this->registerVariables();
[9257]62
[2072]63    }
64
65    PlayerInfo::~PlayerInfo()
66    {
[2171]67        if (this->BaseObject::isInitialized())
[2072]68        {
[3038]69            this->stopControl();
[2072]70
71            if (this->controller_)
72            {
[5929]73                this->controller_->destroy();
[2072]74                this->controller_ = 0;
75            }
[2662]76
77            if (this->getGametype())
78                this->getGametype()->playerLeft(this);
[2072]79        }
80    }
81
82    void PlayerInfo::registerVariables()
83    {
[3280]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));
[2072]87    }
88
89    void PlayerInfo::changedName()
90    {
[2662]91        SUPER(PlayerInfo, changedName);
92
[2171]93        if (this->isInitialized() && this->getGametype())
[2072]94            this->getGametype()->playerChangedName(this);
95    }
96
97    void PlayerInfo::changedGametype()
98    {
[2973]99        this->updateGametypeInfo();
100
[2171]101        if (this->isInitialized())
[2072]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
[2973]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
[2072]133    void PlayerInfo::createController()
134    {
[2839]135        if (this->controller_)
136        {
[5929]137            this->controller_->destroy();
[2839]138            this->controller_ = 0;
139        }
[9667]140        this->controller_ = this->defaultController_.fabricate(this->getContext());
[2072]141        assert(this->controller_);
142        this->controller_->setPlayer(this);
143        if (this->controllableEntity_)
[6417]144        {
[2072]145            this->controller_->setControllableEntity(this->controllableEntity_);
[6417]146            this->controllableEntity_->setController(this->controller_);
147        }
[2662]148        this->changedController();
[2072]149    }
150
[3038]151    void PlayerInfo::startControl(ControllableEntity* entity)
[2072]152    {
[3038]153        if (!entity || entity == this->controllableEntity_)
[2662]154            return;
155
[8706]156        while (this->previousControllableEntity_.size() > 0)
[6417]157            this->stopTemporaryControl();
[9257]158
[2072]159        if (this->controllableEntity_)
[3038]160            this->stopControl();
[2072]161
162        this->controllableEntity_ = entity;
[3038]163        this->controllableEntityID_ = entity->getObjectID();
[2072]164
[3038]165        entity->setPlayer(this);
[2072]166
[3038]167        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
168
[2072]169        if (this->controller_)
[6417]170        {
[2072]171            this->controller_->setControllableEntity(entity);
[6417]172            this->controllableEntity_->setController(this->controller_);
173        }
[2826]174
175        this->changedControllableEntity();
[9257]176
[9348]177        RadarViewable* radarviewable = orxonox_cast<RadarViewable*>(entity);
[9257]178        if (radarviewable != NULL)
179            radarviewable->setRadarName(this->getName());
[2072]180    }
181
[6417]182    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
183    {
184        if (!entity)
185            return;
186
[8706]187        this->controllableEntity_->destroyHud(); // HACK-ish
188        this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_));
[6417]189        this->controllableEntity_ = entity;
190        this->controllableEntityID_ = entity->getObjectID();
191
192        entity->setPlayer(this);
[7163]193        entity->setController(this->controller_);
[6417]194
195        if (this->controller_)
196            this->controller_->setControllableEntity(entity);
197
198        this->changedControllableEntity();
199    }
200
[3038]201    void PlayerInfo::stopControl()
[2072]202    {
[8706]203        while ( this->previousControllableEntity_.size() > 0)
[6417]204            this->stopTemporaryControl();
205
[3038]206        ControllableEntity* entity = this->controllableEntity_;
[2072]207
[3038]208        if (!entity)
209            return;
[2072]210
[6417]211        this->controllableEntity_->setController(0);
[3038]212        this->controllableEntity_ = 0;
213        this->controllableEntityID_ = OBJECTID_UNKNOWN;
[2826]214
[3038]215        if (this->controller_)
216            this->controller_->setControllableEntity(0);
217
[6417]218        if ( GameMode::isMaster() )
219            entity->removePlayer();
[3038]220
221        this->changedControllableEntity();
[2072]222    }
223
[8706]224    void PlayerInfo::pauseControl()
225    {
226        ControllableEntity* entity = this->controllableEntity_;
227
228        if (!entity)
229            return;
230
[9939]231        Controller* tmp =this->controllableEntity_->getController();
232        if (tmp == NULL)
233        {
[9945]234            orxout(verbose) <<  "PlayerInfo: pauseControl, Controller is NULL " << endl;
235            return;
[9939]236        }
237        tmp->setActive(false);
[8706]238        //this->controllableEntity_->getController()->setControllableEntity(NULL);
239        this->controllableEntity_->setController(0);
240    }
241
[6417]242    void PlayerInfo::stopTemporaryControl()
243    {
244        ControllableEntity* entity = this->controllableEntity_;
245
[8706]246        assert(this->controllableEntity_ != NULL);
247        if( !entity || this->previousControllableEntity_.size() == 0 )
[6417]248            return;
249
[7163]250        this->controllableEntity_->setController(0);
[8891]251        if(this->isHumanPlayer()) // TODO: Multiplayer?
252            this->controllableEntity_->destroyHud(); // HACK-ish
[9257]253
[8706]254//        this->controllableEntity_ = this->previousControllableEntity_.back();
255        do {
256            this->controllableEntity_ = this->previousControllableEntity_.back();
257        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
[6417]258        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
[8706]259        this->previousControllableEntity_.pop_back();
[6417]260
[8706]261        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
[6417]262            this->controller_->setControllableEntity(this->controllableEntity_);
263
[8706]264         // HACK-ish
[8891]265        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
[8706]266            this->controllableEntity_->createHud();
267
[6417]268        if ( GameMode::isMaster() )
269            entity->removePlayer();
270
271        this->changedControllableEntity();
272    }
273
[2072]274    void PlayerInfo::networkcallback_changedcontrollableentityID()
275    {
[2171]276        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
[2072]277        {
278            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
[3325]279            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
[2072]280            this->startControl(entity);
281        }
282        else
283        {
[3038]284            this->stopControl();
[2072]285        }
286    }
[2973]287
[6417]288
[2973]289    void PlayerInfo::networkcallback_changedgtinfoID()
290    {
291        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
292        {
[3325]293            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
[2973]294
295            if (!this->gtinfo_)
296                this->gtinfoID_ = OBJECTID_UNKNOWN;
297        }
298    }
[2072]299}
Note: See TracBrowser for help on using the repository browser.