Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/fps/src/orxonox/worldentities/ControllableEntity.cc @ 7073

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

Merged presentation2 branch back to trunk.
Major new features:

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