Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9137 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
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"
[9016]38#include "worldentities/pawns/SpaceShip.h"
[2072]39
40namespace orxonox
41{
42    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
43    {
44        RegisterObject(PlayerInfo);
45
[8327]46        this->clientID_ = NETWORK_PEER_ID_UNKNOWN;
[2072]47        this->bHumanPlayer_ = false;
48        this->bLocalPlayer_ = false;
49        this->bReadyToSpawn_ = false;
[2662]50        this->bSetUnreadyAfterSpawn_ = true;
[2072]51        this->controller_ = 0;
52        this->controllableEntity_ = 0;
[6417]53        this->controllableEntityID_ = OBJECTID_UNKNOWN;
[2072]54
[2973]55        this->gtinfo_ = 0;
56        this->gtinfoID_ = OBJECTID_UNKNOWN;
57        this->updateGametypeInfo();
58
[2072]59        this->registerVariables();
[9016]60       
[2072]61    }
62
63    PlayerInfo::~PlayerInfo()
64    {
[2171]65        if (this->BaseObject::isInitialized())
[2072]66        {
[3038]67            this->stopControl();
[2072]68
69            if (this->controller_)
70            {
[5929]71                this->controller_->destroy();
[2072]72                this->controller_ = 0;
73            }
[2662]74
75            if (this->getGametype())
76                this->getGametype()->playerLeft(this);
[2072]77        }
78    }
79
80    void PlayerInfo::registerVariables()
81    {
[3280]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));
[2072]85    }
86
87    void PlayerInfo::changedName()
88    {
[2662]89        SUPER(PlayerInfo, changedName);
90
[2171]91        if (this->isInitialized() && this->getGametype())
[2072]92            this->getGametype()->playerChangedName(this);
93    }
94
95    void PlayerInfo::changedGametype()
96    {
[2973]97        this->updateGametypeInfo();
98
[2171]99        if (this->isInitialized())
[2072]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
[2973]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
[2072]131    void PlayerInfo::createController()
132    {
[2839]133        if (this->controller_)
134        {
[5929]135            this->controller_->destroy();
[2839]136            this->controller_ = 0;
137        }
[2072]138        this->controller_ = this->defaultController_.fabricate(this);
139        assert(this->controller_);
140        this->controller_->setPlayer(this);
141        if (this->controllableEntity_)
[6417]142        {
[2072]143            this->controller_->setControllableEntity(this->controllableEntity_);
[6417]144            this->controllableEntity_->setController(this->controller_);
145        }
[2662]146        this->changedController();
[2072]147    }
148
[3038]149    void PlayerInfo::startControl(ControllableEntity* entity)
[2072]150    {
[3038]151        if (!entity || entity == this->controllableEntity_)
[2662]152            return;
153
[8706]154        while (this->previousControllableEntity_.size() > 0)
[6417]155            this->stopTemporaryControl();
[8706]156       
[2072]157        if (this->controllableEntity_)
[3038]158            this->stopControl();
[2072]159
160        this->controllableEntity_ = entity;
[3038]161        this->controllableEntityID_ = entity->getObjectID();
[2072]162
[3038]163        entity->setPlayer(this);
[2072]164
[3038]165        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
166
[2072]167        if (this->controller_)
[6417]168        {
[2072]169            this->controller_->setControllableEntity(entity);
[6417]170            this->controllableEntity_->setController(this->controller_);
171        }
[2826]172
173        this->changedControllableEntity();
[9016]174                SpaceShip* spaceship = dynamic_cast<SpaceShip*>(entity);
175        if (spaceship != NULL)
176        {
177            spaceship->setRVName(this->getName());
178        }
[2072]179    }
180
[6417]181    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
182    {
183        if (!entity)
184            return;
185
[8706]186        this->controllableEntity_->destroyHud(); // HACK-ish
187        this->previousControllableEntity_.push_back(WeakPtr<ControllableEntity>(this->controllableEntity_));
[6417]188        this->controllableEntity_ = entity;
189        this->controllableEntityID_ = entity->getObjectID();
190
191        entity->setPlayer(this);
[7163]192        entity->setController(this->controller_);
[6417]193
194        if (this->controller_)
195            this->controller_->setControllableEntity(entity);
196
197        this->changedControllableEntity();
198    }
199
[3038]200    void PlayerInfo::stopControl()
[2072]201    {
[8706]202        while ( this->previousControllableEntity_.size() > 0)
[6417]203            this->stopTemporaryControl();
204
[3038]205        ControllableEntity* entity = this->controllableEntity_;
[2072]206
[3038]207        if (!entity)
208            return;
[2072]209
[6417]210        this->controllableEntity_->setController(0);
[3038]211        this->controllableEntity_ = 0;
212        this->controllableEntityID_ = OBJECTID_UNKNOWN;
[2826]213
[3038]214        if (this->controller_)
215            this->controller_->setControllableEntity(0);
216
[6417]217        if ( GameMode::isMaster() )
218            entity->removePlayer();
[3038]219
220        this->changedControllableEntity();
[2072]221    }
222
[8706]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
[6417]235    void PlayerInfo::stopTemporaryControl()
236    {
237        ControllableEntity* entity = this->controllableEntity_;
238
[8706]239        assert(this->controllableEntity_ != NULL);
240        if( !entity || this->previousControllableEntity_.size() == 0 )
[6417]241            return;
242
[7163]243        this->controllableEntity_->setController(0);
[8891]244        if(this->isHumanPlayer()) // TODO: Multiplayer?
245            this->controllableEntity_->destroyHud(); // HACK-ish
[7163]246       
[8706]247//        this->controllableEntity_ = this->previousControllableEntity_.back();
248        do {
249            this->controllableEntity_ = this->previousControllableEntity_.back();
250        } while(this->controllableEntity_ == NULL && this->previousControllableEntity_.size() > 0);
[6417]251        this->controllableEntityID_ = this->controllableEntity_->getObjectID();
[8706]252        this->previousControllableEntity_.pop_back();
[6417]253
[8706]254        if ( this->controllableEntity_ != NULL && this->controller_ != NULL)
[6417]255            this->controller_->setControllableEntity(this->controllableEntity_);
256
[8706]257         // HACK-ish
[8891]258        if(this->controllableEntity_ != NULL && this->isHumanPlayer())
[8706]259            this->controllableEntity_->createHud();
260
[6417]261        if ( GameMode::isMaster() )
262            entity->removePlayer();
263
264        this->changedControllableEntity();
265    }
266
[2072]267    void PlayerInfo::networkcallback_changedcontrollableentityID()
268    {
[2171]269        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
[2072]270        {
271            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
[3325]272            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
[2072]273            this->startControl(entity);
274        }
275        else
276        {
[3038]277            this->stopControl();
[2072]278        }
279    }
[2973]280
[6417]281
[2973]282    void PlayerInfo::networkcallback_changedgtinfoID()
283    {
284        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
285        {
[3325]286            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
[2973]287
288            if (!this->gtinfo_)
289                this->gtinfoID_ = OBJECTID_UNKNOWN;
290        }
291    }
[2072]292}
Note: See TracBrowser for help on using the repository browser.