Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/map/src/orxonox/objects/worldentities/ControllableEntity.cc @ 3050

Last change on this file since 3050 was 2913, checked in by Naaduun, 16 years ago

Added own scenemanager to map, added 2 pointer to contain mapentity and mapscenenod to radarviewable

  • Property svn:eol-style set to native
File size: 18.6 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 "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
32#include <OgreSceneManager.h>
33
34#include "core/CoreIncludes.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/Core.h"
37#include "core/XMLPort.h"
38#include "core/Template.h"
39
40#include "objects/Scene.h"
41#include "objects/infos/PlayerInfo.h"
42#include "objects/worldentities/Camera.h"
43#include "objects/worldentities/CameraPosition.h"
44#include "objects/gametypes/Gametype.h"
45#include "overlays/OverlayGroup.h"
46
47namespace orxonox
48{
49    CreateFactory(ControllableEntity);
50
51    ControllableEntity::ControllableEntity(BaseObject* creator) : MobileEntity(creator)
52    {
53        RegisterObject(ControllableEntity);
54
55        this->bHasLocalController_ = false;
56        this->bHasHumanController_ = false;
57
58        this->server_overwrite_ = 0;
59        this->client_overwrite_ = 0;
60        this->player_ = 0;
61        this->playerID_ = OBJECTID_UNKNOWN;
62        this->hud_ = 0;
63        this->camera_ = 0;
64        this->reverseCamera_ = 0;
65        this->bDestroyWhenPlayerLeft_ = false;
66        this->cameraPositionRootNode_ = this->node_->createChildSceneNode();
67        this->bMouseLook_ = false;
68        this->mouseLookSpeed_ = 200;
69
70        this->gtinfo_ = 0;
71        this->gtinfoID_ = OBJECTID_UNKNOWN;
72        this->changedGametype();
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::very_high );
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(this, false);
100
101            if (this->hud_)
102                delete this->hud_;
103
104            if (this->camera_)
105                delete this->camera_;
106
107            for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
108                delete (*it);
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    }
124
125    void ControllableEntity::setConfigValues()
126    {
127        SetConfigValue(mouseLookSpeed_, 3.0f);
128    }
129
130    void ControllableEntity::changedGametype()
131    {
132        //SUPER(ControllableEntity, changedGametype);
133        WorldEntity::changedGametype();
134
135        this->gtinfo_ = 0;
136        this->gtinfoID_ = OBJECTID_UNKNOWN;
137
138        if (this->getGametype() && this->getGametype()->getGametypeInfo())
139        {
140            this->gtinfo_ = this->getGametype()->getGametypeInfo();
141            this->gtinfoID_ = this->gtinfo_->getObjectID();
142        }
143    }
144
145    void ControllableEntity::addCameraPosition(CameraPosition* position)
146    {
147        if (position->getAllowMouseLook())
148            position->attachToNode(this->cameraPositionRootNode_);
149        else
150            this->attach(position);
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<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            }
178            else if (this->cameraPositions_.size() > 0)
179            {
180                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
181                {
182                    if ((*it) == this->camera_->getParent())
183                    {
184                        ++it;
185                        if (it != this->cameraPositions_.end())
186                            (*it)->attachCamera(this->camera_);
187                        else
188                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
189                        break;
190                    }
191                }
192            }
193            else
194            {
195                this->camera_->attachToNode(this->cameraPositionRootNode_);
196            }
197        }
198    }
199
200    void ControllableEntity::mouseLook()
201    {
202        this->bMouseLook_ = !this->bMouseLook_;
203
204        if (!this->bMouseLook_)
205            this->cameraPositionRootNode_->setOrientation(Quaternion::IDENTITY);
206    }
207
208    void ControllableEntity::rotateYaw(const Vector2& value)
209    {
210        if (this->bMouseLook_)
211            this->cameraPositionRootNode_->yaw(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
212    }
213
214    void ControllableEntity::rotatePitch(const Vector2& value)
215    {
216        if (this->bMouseLook_)
217            this->cameraPositionRootNode_->pitch(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
218    }
219
220    void ControllableEntity::rotateRoll(const Vector2& value)
221    {
222        if (this->bMouseLook_)
223            this->cameraPositionRootNode_->roll(Radian(value.y * this->mouseLookSpeed_), Ogre::Node::TS_LOCAL);
224    }
225
226    void ControllableEntity::setPlayer(PlayerInfo* player)
227    {
228        if (!player)
229        {
230            this->removePlayer();
231            return;
232        }
233
234        this->player_ = player;
235        this->playerID_ = player->getObjectID();
236        this->bHasLocalController_ = player->isLocalPlayer();
237        this->bHasHumanController_ = player->isHumanPlayer();
238
239        if (this->bHasLocalController_ && this->bHasHumanController_)
240        {
241            this->startLocalHumanControl();
242
243            if (!Core::isMaster())
244            {
245                this->client_overwrite_ = this->server_overwrite_;
246                this->setObjectMode(objectDirection::bidirectional);
247            }
248        }
249    }
250
251    void ControllableEntity::removePlayer()
252    {
253        if (this->bHasLocalController_ && this->bHasHumanController_)
254            this->stopLocalHumanControl();
255
256        this->player_ = 0;
257        this->playerID_ = OBJECTID_UNKNOWN;
258        this->bHasLocalController_ = false;
259        this->bHasHumanController_ = false;
260        this->setObjectMode(objectDirection::toclient);
261
262        if (this->bDestroyWhenPlayerLeft_)
263            delete this;
264    }
265
266    void ControllableEntity::networkcallback_changedplayerID()
267    {
268        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
269        if (this->playerID_ != OBJECTID_UNKNOWN)
270        {
271            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
272            if (this->player_ && (this->player_->getControllableEntity() != this))
273                this->player_->startControl(this);
274        }
275    }
276
277    void ControllableEntity::networkcallback_changedgtinfoID()
278    {
279        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
280        {
281            this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
282
283            if (!this->gtinfo_)
284                this->gtinfoID_ = OBJECTID_UNKNOWN;
285        }
286    }
287
288    void ControllableEntity::startLocalHumanControl()
289    {
290        if (!this->camera_)
291        {
292            this->camera_ = new Camera(this);
293            this->camera_->requestFocus();
294            if (this->cameraPositionTemplate_ != "")
295                this->addTemplate(this->cameraPositionTemplate_);
296            if (this->cameraPositions_.size() > 0)
297                this->cameraPositions_.front()->attachCamera(this->camera_);
298            else
299                this->camera_->attachToNode(this->cameraPositionRootNode_);
300        }
301
302        if (!this->hud_)
303        {
304            if (this->hudtemplate_ != "")
305            {
306                this->hud_ = new OverlayGroup(this);
307                this->hud_->addTemplate(this->hudtemplate_);
308                this->hud_->setOwner(this);
309            }
310        }
311    }
312
313    void ControllableEntity::stopLocalHumanControl()
314    {
315        if (this->camera_)
316        {
317            this->camera_->detachFromParent();
318            delete this->camera_;
319            this->camera_ = 0;
320        }
321
322        if (this->hud_)
323        {
324            delete this->hud_;
325            this->hud_ = 0;
326        }
327    }
328
329    void ControllableEntity::tick(float dt)
330    {
331        MobileEntity::tick(dt);
332
333        if (this->isActive())
334        {
335            // Check whether Bullet doesn't do the physics for us
336            if (!this->isDynamic())
337            {
338                if (Core::isMaster())
339                {
340                    this->server_position_ = this->getPosition();
341                    this->server_orientation_ = this->getOrientation();
342                    this->server_linear_velocity_ = this->getVelocity();
343                    this->server_angular_velocity_ = this->getAngularVelocity();
344                }
345                else if (this->bHasLocalController_)
346                {
347                    this->client_position_ = this->getPosition();
348                    this->client_orientation_ = this->getOrientation();
349                    this->client_linear_velocity_ = this->getVelocity();
350                    this->client_angular_velocity_ = this->getAngularVelocity();
351                }
352            }
353        }
354    }
355
356    void ControllableEntity::registerVariables()
357    {
358        registerVariable(this->cameraPositionTemplate_,  variableDirection::toclient);
359        registerVariable(this->hudtemplate_,             variableDirection::toclient);
360
361        registerVariable(this->server_position_,         variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
362        registerVariable(this->server_linear_velocity_,  variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerLinearVelocity));
363        registerVariable(this->server_orientation_,      variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
364        registerVariable(this->server_angular_velocity_, variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerAngularVelocity));
365
366        registerVariable(this->server_overwrite_,        variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
367        registerVariable(this->client_overwrite_,        variableDirection::toserver);
368
369        registerVariable(this->client_position_,         variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
370        registerVariable(this->client_linear_velocity_,  variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientLinearVelocity));
371        registerVariable(this->client_orientation_,      variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
372        registerVariable(this->client_angular_velocity_, variableDirection::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientAngularVelocity));
373
374        registerVariable(this->playerID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
375        registerVariable(this->gtinfoID_,                variableDirection::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID));
376    }
377
378    void ControllableEntity::processServerPosition()
379    {
380        if (!this->bHasLocalController_)
381            MobileEntity::setPosition(this->server_position_);
382    }
383
384    void ControllableEntity::processServerLinearVelocity()
385    {
386        if (!this->bHasLocalController_)
387            MobileEntity::setVelocity(this->server_linear_velocity_);
388    }
389
390    void ControllableEntity::processServerOrientation()
391    {
392        if (!this->bHasLocalController_)
393            MobileEntity::setOrientation(this->server_orientation_);
394    }
395
396    void ControllableEntity::processServerAngularVelocity()
397    {
398        if (!this->bHasLocalController_)
399            MobileEntity::setAngularVelocity(this->server_angular_velocity_);
400    }
401
402    void ControllableEntity::processOverwrite()
403    {
404        if (this->bHasLocalController_)
405        {
406            this->setPosition(this->server_position_);
407            this->setOrientation(this->server_orientation_);
408            this->setVelocity(this->server_linear_velocity_);
409            this->setAngularVelocity(this->server_angular_velocity_);
410
411            this->client_overwrite_ = this->server_overwrite_;
412        }
413    }
414
415    void ControllableEntity::processClientPosition()
416    {
417        if (this->server_overwrite_ == this->client_overwrite_)
418        {
419            MobileEntity::setPosition(this->client_position_);
420            this->server_position_ = this->getPosition();
421        }
422    }
423
424    void ControllableEntity::processClientLinearVelocity()
425    {
426        if (this->server_overwrite_ == this->client_overwrite_)
427        {
428            MobileEntity::setVelocity(this->client_linear_velocity_);
429            this->server_linear_velocity_ = this->getVelocity();
430        }
431    }
432
433    void ControllableEntity::processClientOrientation()
434    {
435        if (this->server_overwrite_ == this->client_overwrite_)
436        {
437            MobileEntity::setOrientation(this->client_orientation_);
438            this->server_orientation_ = this->getOrientation();
439        }
440    }
441
442    void ControllableEntity::processClientAngularVelocity()
443    {
444        if (this->server_overwrite_ == this->client_overwrite_)
445        {
446            MobileEntity::setAngularVelocity(this->client_angular_velocity_);
447            this->server_angular_velocity_ = this->getAngularVelocity();
448        }
449    }
450
451    void ControllableEntity::setPosition(const Vector3& position)
452    {
453        if (Core::isMaster())
454        {
455            MobileEntity::setPosition(position);
456            this->server_position_ = this->getPosition();
457            ++this->server_overwrite_;
458        }
459        else if (this->bHasLocalController_)
460        {
461            MobileEntity::setPosition(position);
462            this->client_position_ = this->getPosition();
463        }
464    }
465
466    void ControllableEntity::setOrientation(const Quaternion& orientation)
467    {
468        if (Core::isMaster())
469        {
470            MobileEntity::setOrientation(orientation);
471            this->server_orientation_ = this->getOrientation();
472            ++this->server_overwrite_;
473        }
474        else if (this->bHasLocalController_)
475        {
476            MobileEntity::setOrientation(orientation);
477            this->client_orientation_ = this->getOrientation();
478        }
479    }
480
481    void ControllableEntity::setVelocity(const Vector3& velocity)
482    {
483        if (Core::isMaster())
484        {
485            MobileEntity::setVelocity(velocity);
486            this->server_linear_velocity_ = this->getVelocity();
487            ++this->server_overwrite_;
488        }
489        else if (this->bHasLocalController_)
490        {
491            MobileEntity::setVelocity(velocity);
492            this->client_linear_velocity_ = this->getVelocity();
493        }
494    }
495
496    void ControllableEntity::setAngularVelocity(const Vector3& velocity)
497    {
498        if (Core::isMaster())
499        {
500            MobileEntity::setAngularVelocity(velocity);
501            this->server_angular_velocity_ = this->getAngularVelocity();
502            ++this->server_overwrite_;
503        }
504        else if (this->bHasLocalController_)
505        {
506            MobileEntity::setAngularVelocity(velocity);
507            this->client_angular_velocity_ = this->getAngularVelocity();
508        }
509    }
510
511    void ControllableEntity::setWorldTransform(const btTransform& worldTrans)
512    {
513        MobileEntity::setWorldTransform(worldTrans);
514        if (Core::isMaster())
515        {
516            this->server_position_ = this->getPosition();
517            this->server_orientation_ = this->getOrientation();
518            this->server_linear_velocity_ = this->getVelocity();
519            this->server_angular_velocity_ = this->getAngularVelocity();
520        }
521        else if (this->bHasLocalController_)
522        {
523            this->client_position_ = this->getPosition();
524            this->client_orientation_ = this->getOrientation();
525            this->client_linear_velocity_ = this->getVelocity();
526            this->client_angular_velocity_ = this->getAngularVelocity();
527        }
528    }
529}
Note: See TracBrowser for help on using the repository browser.