Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/particles2/src/orxonox/infos/PlayerInfo.cc @ 6087

Last change on this file since 6087 was 6087, checked in by scheusso, 14 years ago

changes in PlayerInfo and Rocket (and scene)
not yet working in network though

  • Property svn:eol-style set to native
File size: 8.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
39namespace orxonox
40{
41    PlayerInfo::PlayerInfo(BaseObject* creator) : Info(creator)
42    {
43        RegisterObject(PlayerInfo);
44
45        this->clientID_ = CLIENTID_UNKNOWN;
46        this->bHumanPlayer_ = false;
47        this->bLocalPlayer_ = false;
48        this->bReadyToSpawn_ = false;
49        this->bSetUnreadyAfterSpawn_ = true;
50        this->controller_ = 0;
51        this->controllableEntity_ = 0;
52        this->controllableEntityID_ = OBJECTID_UNKNOWN;
53        this->temporaryControllableEntity_ = 0;
54        this->temporaryControllableEntityID_ = OBJECTID_UNKNOWN;
55
56        this->gtinfo_ = 0;
57        this->gtinfoID_ = OBJECTID_UNKNOWN;
58        this->updateGametypeInfo();
59
60        this->registerVariables();
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->temporaryControllableEntityID_, VariableDirection::ToClient, new NetworkCallback<PlayerInfo>(this, &PlayerInfo::networkcallback_changedcontrollableentityID));
85        registerVariable(this->bReadyToSpawn_,        VariableDirection::ToServer);
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);
141        assert(this->controller_);
142        this->controller_->setPlayer(this);
143        if (this->controllableEntity_)
144            this->controller_->setControllableEntity(this->controllableEntity_);
145        this->changedController();
146    }
147
148    void PlayerInfo::startControl(ControllableEntity* entity)
149    {
150        if (!entity || entity == this->controllableEntity_)
151            return;
152
153        if (this->controllableEntity_)
154            this->stopControl();
155
156        this->controllableEntity_ = entity;
157        this->controllableEntityID_ = entity->getObjectID();
158
159        entity->setPlayer(this);
160
161        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
162
163        if (this->controller_)
164            this->controller_->setControllableEntity(entity);
165
166        this->changedControllableEntity();
167    }
168   
169    void PlayerInfo::startTemporaryControl(ControllableEntity* entity)
170    {
171        if (!entity)
172            return;
173       
174        assert( this->temporaryControllableEntity_==0 );
175
176        this->temporaryControllableEntity_ = entity;
177        this->temporaryControllableEntityID_ = entity->getObjectID();
178
179        entity->setPlayer(this);
180
181        this->bReadyToSpawn_ &= (!this->bSetUnreadyAfterSpawn_);
182
183        if (this->controller_)
184            this->controller_->setControllableEntity(entity);
185
186        this->changedControllableEntity();
187    }
188
189    void PlayerInfo::stopControl()
190    {
191        ControllableEntity* entity = this->controllableEntity_;
192
193        if (!entity)
194            return;
195
196        this->controllableEntity_ = 0;
197        this->controllableEntityID_ = OBJECTID_UNKNOWN;
198
199        if (this->controller_)
200            this->controller_->setControllableEntity(0);
201
202        entity->removePlayer();
203
204        this->changedControllableEntity();
205    }
206   
207    void PlayerInfo::stopTemporaryControl()
208    {
209        ControllableEntity* entity = this->temporaryControllableEntity_;
210
211        if (!entity)
212            return;
213
214        this->temporaryControllableEntity_ = 0;
215        this->temporaryControllableEntityID_ = OBJECTID_UNKNOWN;
216
217        if ( this->controllableEntity_ && this->controller_)
218            this->controller_->setControllableEntity(this->controllableEntity_);
219
220        entity->removePlayer();
221       
222        this->changedControllableEntity();
223    }
224   
225    void PlayerInfo::networkcallback_changedcontrollableentityID()
226    {
227        if (this->controllableEntityID_ != OBJECTID_UNKNOWN)
228        {
229            Synchronisable* temp = Synchronisable::getSynchronisable(this->controllableEntityID_);
230            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
231            this->startControl(entity);
232        }
233        else
234        {
235            this->stopControl();
236        }
237    }
238
239    void PlayerInfo::networkcallback_changedtemporarycontrollableentityID()
240    {
241        CCOUT(0) << "changedtemporarycontrollableentityid" << endl;
242        if (this->temporaryControllableEntityID_ != OBJECTID_UNKNOWN)
243        {
244            Synchronisable* temp = Synchronisable::getSynchronisable(this->temporaryControllableEntityID_);
245            ControllableEntity* entity = orxonox_cast<ControllableEntity*>(temp);
246            this->startTemporaryControl(entity);
247        }
248        else
249        {
250            this->stopTemporaryControl();
251        }
252    }
253
254
255    void PlayerInfo::networkcallback_changedgtinfoID()
256    {
257        CCOUT(0) << "changedcontrollableentityid" << endl;
258        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
259        {
260            this->gtinfo_ = orxonox_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
261
262            if (!this->gtinfo_)
263                this->gtinfoID_ = OBJECTID_UNKNOWN;
264        }
265    }
266}
Note: See TracBrowser for help on using the repository browser.