Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/worldentities/ControllableEntity.cc @ 6108

Last change on this file since 6108 was 6108, checked in by scheusso, 16 years ago

merged steering branch to presentation2 branch

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