Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Merged orxonox_cast related revisions from core4 back to trunk.

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