Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged particles2 branch to presentation2.

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