Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pCuts/src/orxonox/worldentities/ControllableEntity.cc @ 9083

Last change on this file since 9083 was 9083, checked in by jo, 12 years ago

Camera works.

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