Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7860 was 7860, checked in by landauf, 13 years ago

fixed a few issues related to the free mouse look mode (default CTRL key):

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