Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/worldentities/ControllableEntity.cc @ 8680

Last change on this file since 8680 was 8680, checked in by dafrick, 13 years ago

Make the camera position stay the same between differnet stones in Tetris.

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