Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10960 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 9.3 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{
[10624]42    RegisterAbstractClass(PlayerInfo).inheritsFrom<Info>();
[9667]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;
[10624]59        this->updateGametypeInfo(this->getGametype());
[2973]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
[10624]97    void PlayerInfo::switchGametype(Gametype* gametype)
[2072]98    {
[10624]99        Gametype* oldGametype = this->getGametype();
100        this->setGametype(StrongPtr<Gametype>(gametype));
101        Gametype* newGametype = this->getGametype();
[2973]102
[10624]103
104        this->updateGametypeInfo(newGametype);
105
[2171]106        if (this->isInitialized())
[2072]107        {
[10624]108            if (oldGametype)
[2072]109            {
[10624]110                if (newGametype)
111                    oldGametype->playerSwitched(this, newGametype);
[2072]112                else
[10624]113                    oldGametype->playerLeft(this);
[2072]114            }
115
[10624]116            if (newGametype)
[2072]117            {
[10624]118                if (oldGametype)
119                    newGametype->playerSwitchedBack(this, oldGametype);
[2072]120                else
[10624]121                    newGametype->playerEntered(this);
[2072]122            }
123        }
124    }
125
[10624]126    void PlayerInfo::updateGametypeInfo(Gametype* gametype)
[2973]127    {
128        this->gtinfo_ = 0;
129        this->gtinfoID_ = OBJECTID_UNKNOWN;
130
[10624]131        if (gametype && gametype->getGametypeInfo())
[2973]132        {
[10624]133            this->gtinfo_ = gametype->getGametypeInfo();
[2973]134            this->gtinfoID_ = this->gtinfo_->getObjectID();
135        }
136    }
137
[2072]138    void PlayerInfo::createController()
139    {
[2839]140        if (this->controller_)
141        {
[5929]142            this->controller_->destroy();
[2839]143            this->controller_ = 0;
144        }
[9667]145        this->controller_ = this->defaultController_.fabricate(this->getContext());
[2072]146        assert(this->controller_);
147        this->controller_->setPlayer(this);
148        if (this->controllableEntity_)
[6417]149        {
[2072]150            this->controller_->setControllableEntity(this->controllableEntity_);
[6417]151            this->controllableEntity_->setController(this->controller_);
152        }
[2662]153        this->changedController();
[2072]154    }
155
[3038]156    void PlayerInfo::startControl(ControllableEntity* entity)
[2072]157    {
[3038]158        if (!entity || entity == this->controllableEntity_)
[2662]159            return;
160
[8706]161        while (this->previousControllableEntity_.size() > 0)
[6417]162            this->stopTemporaryControl();
[9257]163
[2072]164        if (this->controllableEntity_)
[3038]165            this->stopControl();
[2072]166
167        this->controllableEntity_ = entity;
[3038]168        this->controllableEntityID_ = entity->getObjectID();
[2072]169
[3038]170        entity->setPlayer(this);
[2072]171
[3038]172        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
173
[2072]174        if (this->controller_)
[6417]175        {
[2072]176            this->controller_->setControllableEntity(entity);
[6417]177            this->controllableEntity_->setController(this->controller_);
178        }
[2826]179
180        this->changedControllableEntity();
[9257]181
[9348]182        RadarViewable* radarviewable = orxonox_cast<RadarViewable*>(entity);
[9257]183        if (radarviewable != NULL)
184            radarviewable->setRadarName(this->getName());
[2072]185    }
186
[6417]187    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
188    {
189        if (!entity)
190            return;
191
[8706]192        this->controllableEntity_->destroyHud(); // HACK-ish
[10624]193        this->previousControllableEntity_.push_back(this->controllableEntity_);
[6417]194        this->controllableEntity_ = entity;
195        this->controllableEntityID_ = entity->getObjectID();
196
197        entity->setPlayer(this);
[7163]198        entity->setController(this->controller_);
[6417]199
200        if (this->controller_)
201            this->controller_->setControllableEntity(entity);
202
203        this->changedControllableEntity();
204    }
205
[3038]206    void PlayerInfo::stopControl()
[2072]207    {
[8706]208        while ( this->previousControllableEntity_.size() > 0)
[6417]209            this->stopTemporaryControl();
210
[3038]211        ControllableEntity* entity = this->controllableEntity_;
[2072]212
[3038]213        if (!entity)
214            return;
[2072]215
[6417]216        this->controllableEntity_->setController(0);
[3038]217        this->controllableEntity_ = 0;
218        this->controllableEntityID_ = OBJECTID_UNKNOWN;
[2826]219
[3038]220        if (this->controller_)
221            this->controller_->setControllableEntity(0);
222
[6417]223        if ( GameMode::isMaster() )
224            entity->removePlayer();
[3038]225
226        this->changedControllableEntity();
[2072]227    }
228
[8706]229    void PlayerInfo::pauseControl()
230    {
231        ControllableEntity* entity = this->controllableEntity_;
232
233        if (!entity)
234            return;
235
[9939]236        Controller* tmp =this->controllableEntity_->getController();
237        if (tmp == NULL)
238        {
[9945]239            orxout(verbose) <<  "PlayerInfo: pauseControl, Controller is NULL " << endl;
240            return;
[9939]241        }
242        tmp->setActive(false);
[8706]243        //this->controllableEntity_->getController()->setControllableEntity(NULL);
244        this->controllableEntity_->setController(0);
245    }
246
[6417]247    void PlayerInfo::stopTemporaryControl()
248    {
249        ControllableEntity* entity = this->controllableEntity_;
250
[8706]251        assert(this->controllableEntity_ != NULL);
252        if( !entity || this->previousControllableEntity_.size() == 0 )
[6417]253            return;
254
[7163]255        this->controllableEntity_->setController(0);
[8891]256        if(this->isHumanPlayer()) // TODO: Multiplayer?
257            this->controllableEntity_->destroyHud(); // HACK-ish
[9257]258
[8706]259//        this->controllableEntity_ = this->previousControllableEntity_.back();
260        do {
261            this->controllableEntity_ = this->previousControllableEntity_.back();
262        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
[6417]263        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
[8706]264        this->previousControllableEntity_.pop_back();
[6417]265
[8706]266        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
[6417]267            this->controller_->setControllableEntity(this->controllableEntity_);
268
[8706]269         // HACK-ish
[8891]270        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
[8706]271            this->controllableEntity_->createHud();
272
[6417]273        if ( GameMode::isMaster() )
274            entity->removePlayer();
275
276        this->changedControllableEntity();
277    }
278
[2072]279    void PlayerInfo::networkcallback_changedcontrollableentityID()
280    {
[2171]281        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
[2072]282        {
283            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
[3325]284            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
[2072]285            this->startControl(entity);
286        }
287        else
288        {
[3038]289            this->stopControl();
[2072]290        }
291    }
[2973]292
[6417]293
[2973]294    void PlayerInfo::networkcallback_changedgtinfoID()
295    {
296        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
297        {
[3325]298            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
[2973]299
300            if (!this->gtinfo_)
301                this->gtinfoID_ = OBJECTID_UNKNOWN;
302        }
303    }
[2072]304}
Note: See TracBrowser for help on using the repository browser.