Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3108 was 3089, checked in by landauf, 16 years ago

merged map branch back to trunk

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