Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6783 was 6417, checked in by rgrieder, 15 years ago

Merged presentation2 branch back to trunk.
Major new features:

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