Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/worldentities/ControllableEntity.cc @ 6111

Last change on this file since 6111 was 6108, checked in by scheusso, 16 years ago

merged steering branch to presentation2 branch

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