Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2995

Last change on this file since 2995 was 2973, checked in by landauf, 16 years ago

changed type of gametype-HUD and default-HUD to PlayerInfo (instead of Gametype and ControllableEntity respectively). The owner of the Pawn-HUD remains Pawn.

  • Property svn:eol-style set to native
File size: 18.1 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:
[2662]25 *      Reto Grieder
[2072]26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
[2662]32#include <OgreSceneManager.h>
33
[2072]34#include "core/CoreIncludes.h"
[2662]35#include "core/ConfigValueIncludes.h"
[2896]36#include "core/GameMode.h"
[2072]37#include "core/XMLPort.h"
38#include "core/Template.h"
39
[2662]40#include "objects/Scene.h"
[2072]41#include "objects/infos/PlayerInfo.h"
42#include "objects/worldentities/Camera.h"
43#include "objects/worldentities/CameraPosition.h"
44#include "overlays/OverlayGroup.h"
45
46namespace orxonox
47{
48    CreateFactory(ControllableEntity);
49
[2662]50    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
[2072]51    {
52        RegisterObject(ControllableEntity);
53
[2662]54        this->bHasLocalController_ = false;
55        this->bHasHumanController_ = false;
56
[2072]57        this->server_overwrite_ = 0;
58        this->client_overwrite_ = 0;
59        this->player_ = 0;
[2171]60        this->playerID_ = OBJECTID_UNKNOWN;
[2072]61        this->hud_ = 0;
62        this->camera_ = 0;
63        this->bDestroyWhenPlayerLeft_ = false;
[2662]64        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
65        this->bMouseLook_ = false;
66        this->mouseLookSpeed_ = 200;
[2072]67
[2662]68        this->server_position_         = Vector3::ZERO;
69        this->client_position_         = Vector3::ZERO;
70        this->server_linear_velocity_  = Vector3::ZERO;
71        this->client_linear_velocity_  = Vector3::ZERO;
72        this->server_orientation_      = Quaternion::IDENTITY;
73        this->client_orientation_      = Quaternion::IDENTITY;
74        this->server_angular_velocity_ = Vector3::ZERO;
75        this->client_angular_velocity_ = Vector3::ZERO;
[2072]76
[2662]77
78        this->setConfigValues();
79        this->setPriority( priority::very_high );
[2072]80        this->registerVariables();
81    }
82
83    ControllableEntity::~ControllableEntity()
84    {
85        if (this->isInitialized())
86        {
[2662]87            this->bDestroyWhenPlayerLeft_ = false;
[2072]88
[2662]89            if (this->bHasLocalController_ && this->bHasHumanController_)
90                this->stopLocalHumanControl();
91
92            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
93                this->getPlayer()->stopControl(this, false);
94
[2072]95            if (this->hud_)
96                delete this->hud_;
97
98            if (this->camera_)
99                delete this->camera_;
100
[2662]101            for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
102                delete (*it);
103
104            if (this->getScene()->getSceneManager())
105                this->getScene()->getSceneManager()->destroySceneNode(this->cameraPositionRootNode_->getName());
[2072]106        }
107    }
108
109    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
110    {
111        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
112
113        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
114        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
115
116        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
117    }
118
[2662]119    void ControllableEntity::setConfigValues()
120    {
121        SetConfigValue(mouseLookSpeed_, 3.0f);
122    }
123
[2072]124    void ControllableEntity::addCameraPosition(CameraPosition* position)
125    {
[2826]126        if (!position->getIsAbsolute())
127        {
128            if (position->getAllowMouseLook())
129                position->attachToNode(this->cameraPositionRootNode_);
130            else
131                this->attach(position);
132        }
[2662]133        else
[2826]134        {
135            WorldEntity* parent = this->getParent();
136            if (parent)
137                parent->attach(position);
138        }
[2072]139        this->cameraPositions_.push_back(position);
140    }
141
142    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
143    {
144        unsigned int i = 0;
145        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
146        {
147            if (i == index)
148                return (*it);
149            ++i;
150        }
151        return 0;
152    }
153
154    void ControllableEntity::switchCamera()
155    {
156        if (this->camera_)
157        {
158            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
159            {
160                this->cameraPositions_.front()->attachCamera(this->camera_);
161            }
162            else if (this->cameraPositions_.size() > 0)
163            {
164                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
165                {
166                    if ((*it) == this->camera_->getParent())
167                    {
168                        ++it;
169                        if (it != this->cameraPositions_.end())
170                            (*it)->attachCamera(this->camera_);
171                        else
172                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
173                        break;
174                    }
175                }
176            }
177            else
178            {
[2662]179                this->camera_->attachToNode(this->cameraPositionRootNode_);
[2072]180            }
181        }
182    }
183
[2662]184    void ControllableEntity::mouseLook()
185    {
186        this->bMouseLook_ = !this->bMouseLook_;
187
188        if (!this->bMouseLook_)
189            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
190    }
191
192    void ControllableEntity::rotateYaw(const Vector2& value)
193    {
194        if (this->bMouseLook_)
195            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
196    }
197
198    void ControllableEntity::rotatePitch(const Vector2& value)
199    {
200        if (this->bMouseLook_)
201            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
202    }
203
204    void ControllableEntity::rotateRoll(const Vector2& value)
205    {
206        if (this->bMouseLook_)
207            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
208    }
209
[2072]210    void ControllableEntity::setPlayer(PlayerInfo* player)
211    {
212        if (!player)
213        {
214            this->removePlayer();
215            return;
216        }
217
218        this->player_ = player;
219        this->playerID_ = player->getObjectID();
[2662]220        this->bHasLocalController_ = player->isLocalPlayer();
221        this->bHasHumanController_ = player->isHumanPlayer();
222
223        if (this->bHasLocalController_ && this->bHasHumanController_)
[2072]224        {
[2662]225            this->startLocalHumanControl();
[2072]226
[2896]227            if (!GameMode::isMaster())
[2072]228            {
229                this->client_overwrite_ = this->server_overwrite_;
[2662]230                this->setObjectMode(objectDirection::bidirectional);
[2072]231            }
232        }
[2839]233
234        this->changedPlayer();
[2072]235    }
236
237    void ControllableEntity::removePlayer()
238    {
[2662]239        if (this->bHasLocalController_ && this->bHasHumanController_)
240            this->stopLocalHumanControl();
[2072]241
242        this->player_ = 0;
[2171]243        this->playerID_ = OBJECTID_UNKNOWN;
[2662]244        this->bHasLocalController_ = false;
245        this->bHasHumanController_ = false;
246        this->setObjectMode(objectDirection::toclient);
[2072]247
[2839]248        this->changedPlayer();
249
[2072]250        if (this->bDestroyWhenPlayerLeft_)
251            delete this;
252    }
253
254    void ControllableEntity::networkcallback_changedplayerID()
255    {
256        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
[2171]257        if (this->playerID_ != OBJECTID_UNKNOWN)
[2072]258        {
[2171]259            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
[2072]260            if (this->player_ && (this->player_->getControllableEntity() != this))
261                this->player_->startControl(this);
262        }
263    }
264
[2662]265    void ControllableEntity::startLocalHumanControl()
266    {
267        if (!this->camera_)
[2072]268        {
[2662]269            this->camera_ = new Camera(this);
270            this->camera_->requestFocus();
271            if (this->cameraPositionTemplate_ != "")
272                this->addTemplate(this->cameraPositionTemplate_);
273            if (this->cameraPositions_.size() > 0)
274                this->cameraPositions_.front()->attachCamera(this->camera_);
275            else
276                this->camera_->attachToNode(this->cameraPositionRootNode_);
[2072]277        }
[2662]278
279        if (!this->hud_)
280        {
281            if (this->hudtemplate_ != "")
282            {
283                this->hud_ = new OverlayGroup(this);
284                this->hud_->addTemplate(this->hudtemplate_);
285                this->hud_->setOwner(this);
286            }
287        }
[2072]288    }
289
[2662]290    void ControllableEntity::stopLocalHumanControl()
[2072]291    {
[2662]292        if (this->camera_)
293        {
294            this->camera_->detachFromParent();
295            delete this->camera_;
296            this->camera_ = 0;
297        }
[2072]298
[2662]299        if (this->hud_)
300        {
301            delete this->hud_;
302            this->hud_ = 0;
303        }
[2072]304    }
305
[2851]306    void ControllableEntity::parentChanged()
307    {
308        WorldEntity::parentChanged();
309
310        WorldEntity* parent = this->getParent();
311        if (parent)
312        {
313            for (std::list<CameraPosition*>::iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
314                if ((*it)->getIsAbsolute())
315                    parent->attach((*it));
316        }
317    }
318
[2072]319    void ControllableEntity::tick(float dt)
320    {
[2662]321        MobileEntity::tick(dt);
322
[2072]323        if (this->isActive())
324        {
[2662]325            // Check whether Bullet doesn't do the physics for us
326            if (!this->isDynamic())
[2072]327            {
[2896]328                if (GameMode::isMaster())
[2662]329                {
330                    this->server_position_ = this->getPosition();
331                    this->server_orientation_ = this->getOrientation();
332                    this->server_linear_velocity_ = this->getVelocity();
333                    this->server_angular_velocity_ = this->getAngularVelocity();
334                }
335                else if (this->bHasLocalController_)
336                {
337                    this->client_position_ = this->getPosition();
338                    this->client_orientation_ = this->getOrientation();
339                    this->client_linear_velocity_ = this->getVelocity();
340                    this->client_angular_velocity_ = this->getAngularVelocity();
341                }
[2072]342            }
343        }
344    }
345
346    void ControllableEntity::registerVariables()
347    {
[2662]348        registerVariable(this->cameraPositionTemplate_,  variableDirection::toclient);
349        registerVariable(this->hudtemplate_,             variableDirection::toclient);
[2072]350
[2662]351        registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
352        registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
353        registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
354        registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
[2072]355
[2662]356        registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
357        registerVariable(this->client_overwrite_,        variableDirection::toserver);
[2072]358
[2662]359        registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
360        registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
361        registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
362        registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
[2072]363
[2662]364        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
[2072]365    }
366
367    void ControllableEntity::processServerPosition()
368    {
[2662]369        if (!this->bHasLocalController_)
370            MobileEntity::setPosition(this->server_position_);
[2072]371    }
372
[2662]373    void ControllableEntity::processServerLinearVelocity()
[2072]374    {
[2662]375        if (!this->bHasLocalController_)
376            MobileEntity::setVelocity(this->server_linear_velocity_);
[2072]377    }
378
379    void ControllableEntity::processServerOrientation()
380    {
[2662]381        if (!this->bHasLocalController_)
382            MobileEntity::setOrientation(this->server_orientation_);
[2072]383    }
384
[2662]385    void ControllableEntity::processServerAngularVelocity()
386    {
387        if (!this->bHasLocalController_)
388            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
389    }
390
[2072]391    void ControllableEntity::processOverwrite()
392    {
[2662]393        if (this->bHasLocalController_)
[2072]394        {
395            this->setPosition(this->server_position_);
396            this->setOrientation(this->server_orientation_);
[2662]397            this->setVelocity(this->server_linear_velocity_);
398            this->setAngularVelocity(this->server_angular_velocity_);
[2072]399
400            this->client_overwrite_ = this->server_overwrite_;
401        }
402    }
403
404    void ControllableEntity::processClientPosition()
405    {
406        if (this->server_overwrite_ == this->client_overwrite_)
407        {
[2662]408            MobileEntity::setPosition(this->client_position_);
409            this->server_position_ = this->getPosition();
[2072]410        }
411    }
412
[2662]413    void ControllableEntity::processClientLinearVelocity()
[2072]414    {
415        if (this->server_overwrite_ == this->client_overwrite_)
416        {
[2662]417            MobileEntity::setVelocity(this->client_linear_velocity_);
418            this->server_linear_velocity_ = this->getVelocity();
[2072]419        }
420    }
421
422    void ControllableEntity::processClientOrientation()
423    {
424        if (this->server_overwrite_ == this->client_overwrite_)
425        {
[2662]426            MobileEntity::setOrientation(this->client_orientation_);
427            this->server_orientation_ = this->getOrientation();
[2072]428        }
429    }
430
[2662]431    void ControllableEntity::processClientAngularVelocity()
[2072]432    {
[2662]433        if (this->server_overwrite_ == this->client_overwrite_)
[2072]434        {
[2662]435            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
436            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]437        }
438    }
439
[2662]440    void ControllableEntity::setPosition(const Vector3& position)
[2072]441    {
[2896]442        if (GameMode::isMaster())
[2072]443        {
[2662]444            MobileEntity::setPosition(position);
445            this->server_position_ = this->getPosition();
[2072]446            ++this->server_overwrite_;
447        }
[2662]448        else if (this->bHasLocalController_)
[2072]449        {
[2662]450            MobileEntity::setPosition(position);
451            this->client_position_ = this->getPosition();
[2072]452        }
453    }
454
455    void ControllableEntity::setOrientation(const Quaternion& orientation)
456    {
[2896]457        if (GameMode::isMaster())
[2072]458        {
[2662]459            MobileEntity::setOrientation(orientation);
460            this->server_orientation_ = this->getOrientation();
[2072]461            ++this->server_overwrite_;
462        }
[2662]463        else if (this->bHasLocalController_)
[2072]464        {
[2662]465            MobileEntity::setOrientation(orientation);
466            this->client_orientation_ = this->getOrientation();
[2072]467        }
468    }
469
[2662]470    void ControllableEntity::setVelocity(const Vector3& velocity)
[2072]471    {
[2896]472        if (GameMode::isMaster())
[2072]473        {
[2662]474            MobileEntity::setVelocity(velocity);
475            this->server_linear_velocity_ = this->getVelocity();
[2072]476            ++this->server_overwrite_;
477        }
[2662]478        else if (this->bHasLocalController_)
[2072]479        {
[2662]480            MobileEntity::setVelocity(velocity);
481            this->client_linear_velocity_ = this->getVelocity();
[2072]482        }
483    }
484
[2662]485    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
[2072]486    {
[2896]487        if (GameMode::isMaster())
[2072]488        {
[2662]489            MobileEntity::setAngularVelocity(velocity);
490            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]491            ++this->server_overwrite_;
492        }
[2662]493        else if (this->bHasLocalController_)
[2072]494        {
[2662]495            MobileEntity::setAngularVelocity(velocity);
496            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]497        }
498    }
499
[2662]500    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
[2072]501    {
[2662]502        MobileEntity::setWorldTransform(worldTrans);
[2896]503        if (GameMode::isMaster())
[2072]504        {
[2662]505            this->server_position_ = this->getPosition();
506            this->server_orientation_ = this->getOrientation();
507            this->server_linear_velocity_ = this->getVelocity();
508            this->server_angular_velocity_ = this->getAngularVelocity();
[2072]509        }
[2662]510        else if (this->bHasLocalController_)
[2072]511        {
[2662]512            this->client_position_ = this->getPosition();
513            this->client_orientation_ = this->getOrientation();
514            this->client_linear_velocity_ = this->getVelocity();
515            this->client_angular_velocity_ = this->getAngularVelocity();
[2072]516        }
517    }
518}
Note: See TracBrowser for help on using the repository browser.