Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/steering/src/orxonox/worldentities/ControllableEntity.cc @ 6333

Last change on this file since 6333 was 6045, checked in by wirthmi, 15 years ago

First implementation of direction shooting.

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