Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/orxonox/objects/worldentities/ControllableEntity.cc @ 2461

Last change on this file since 2461 was 2461, checked in by rgrieder, 15 years ago
  • Updated msvc files
  • temporary solution for a super function caller that doesn't work with msvc.
  • Property svn:eol-style set to native
File size: 17.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 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "ControllableEntity.h"
31
32#include "core/CoreIncludes.h"
33#include "core/Core.h"
34#include "core/XMLPort.h"
35#include "core/Template.h"
36
37#include "objects/infos/PlayerInfo.h"
38#include "objects/worldentities/Camera.h"
39#include "objects/worldentities/CameraPosition.h"
40#include "objects/gametypes/Gametype.h"
41#include "overlays/OverlayGroup.h"
42
43namespace orxonox
44{
45    CreateFactory(ControllableEntity);
46
47    ControllableEntity::ControllableEntity(BaseObject* creator) : WorldEntity(creator)
48    {
49        RegisterObject(ControllableEntity);
50
51        this->bHasLocalController_ = false;
52        this->bHasHumanController_ = false;
53
54        this->server_overwrite_ = 0;
55        this->client_overwrite_ = 0;
56        this->player_ = 0;
57        this->playerID_ = OBJECTID_UNKNOWN;
58        this->hud_ = 0;
59        this->camera_ = 0;
60        this->bDestroyWhenPlayerLeft_ = false;
61
62        this->gtinfo_ = 0;
63        this->gtinfoID_ = OBJECTID_UNKNOWN;
64        this->changedGametype();
65
66        this->velocity_ = Vector3::ZERO;
67        this->acceleration_ = Vector3::ZERO;
68
69        this->server_position_ = Vector3::ZERO;
70        this->client_position_ = Vector3::ZERO;
71        this->server_velocity_ = Vector3::ZERO;
72        this->client_velocity_ = Vector3::ZERO;
73        this->server_orientation_ = Quaternion::IDENTITY;
74        this->client_orientation_ = Quaternion::IDENTITY;
75
76        this->registerVariables();
77    }
78
79    ControllableEntity::~ControllableEntity()
80    {
81        if (this->isInitialized())
82        {
83            this->bDestroyWhenPlayerLeft_ = false;
84
85            if (this->bHasLocalController_ && this->bHasHumanController_)
86                this->stopLocalHumanControl();
87
88            if (this->getPlayer() && this->getPlayer()->getControllableEntity() == this)
89                this->getPlayer()->stopControl(this, false);
90
91            if (this->hud_)
92                delete this->hud_;
93
94            if (this->camera_)
95                delete this->camera_;
96        }
97    }
98
99    void ControllableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
100    {
101        SUPER(ControllableEntity, XMLPort, xmlelement, mode);
102
103        XMLPortParam(ControllableEntity, "hudtemplate", setHudTemplate, getHudTemplate, xmlelement, mode);
104        XMLPortParam(ControllableEntity, "camerapositiontemplate", setCameraPositionTemplate, getCameraPositionTemkplate, xmlelement, mode);
105
106        XMLPortObject(ControllableEntity, CameraPosition, "camerapositions", addCameraPosition, getCameraPosition, xmlelement, mode);
107    }
108
109    void ControllableEntity::changedGametype()
110    {
111        //SUPER(ControllableEntity, changedGametype);
112        WorldEntity::changedGametype();
113
114        this->gtinfo_ = 0;
115        this->gtinfoID_ = OBJECTID_UNKNOWN;
116
117        if (this->getGametype() && this->getGametype()->getGametypeInfo())
118        {
119            this->gtinfo_ = this->getGametype()->getGametypeInfo();
120            this->gtinfoID_ = this->gtinfo_->getObjectID();
121        }
122    }
123
124    void ControllableEntity::addCameraPosition(CameraPosition* position)
125    {
126        this->attach(position);
127        this->cameraPositions_.push_back(position);
128    }
129
130    CameraPosition* ControllableEntity::getCameraPosition(unsigned int index) const
131    {
132        unsigned int i = 0;
133        for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
134        {
135            if (i == index)
136                return (*it);
137            ++i;
138        }
139        return 0;
140    }
141
142    void ControllableEntity::switchCamera()
143    {
144        if (this->camera_)
145        {
146            if (this->camera_->getParent() == this && this->cameraPositions_.size() > 0)
147            {
148                this->cameraPositions_.front()->attachCamera(this->camera_);
149            }
150            else if (this->cameraPositions_.size() > 0)
151            {
152                for (std::list<CameraPosition*>::const_iterator it = this->cameraPositions_.begin(); it != this->cameraPositions_.end(); ++it)
153                {
154                    if ((*it) == this->camera_->getParent())
155                    {
156                        ++it;
157                        if (it != this->cameraPositions_.end())
158                            (*it)->attachCamera(this->camera_);
159                        else
160                            (*this->cameraPositions_.begin())->attachCamera(this->camera_);
161                        break;
162                    }
163                }
164            }
165            else
166            {
167                this->attach(this->camera_);
168            }
169        }
170    }
171
172    void ControllableEntity::setPlayer(PlayerInfo* player)
173    {
174        if (!player)
175        {
176            this->removePlayer();
177            return;
178        }
179
180        this->player_ = player;
181        this->playerID_ = player->getObjectID();
182        this->bHasLocalController_ = player->isLocalPlayer();
183        this->bHasHumanController_ = player->isHumanPlayer();
184
185        if (this->bHasLocalController_ && this->bHasHumanController_)
186        {
187            this->startLocalHumanControl();
188
189            if (!Core::isMaster())
190            {
191                this->client_overwrite_ = this->server_overwrite_;
192                this->setObjectMode(direction::bidirectional);
193            }
194        }
195    }
196
197    void ControllableEntity::removePlayer()
198    {
199        if (this->bHasLocalController_ && this->bHasHumanController_)
200            this->stopLocalHumanControl();
201
202        this->player_ = 0;
203        this->playerID_ = OBJECTID_UNKNOWN;
204        this->bHasLocalController_ = false;
205        this->bHasHumanController_ = false;
206        this->setObjectMode(direction::toclient);
207
208        if (this->bDestroyWhenPlayerLeft_)
209            delete this;
210    }
211
212    void ControllableEntity::networkcallback_changedplayerID()
213    {
214        // just do this in case the entity wasn't yet synchronized when the corresponding PlayerInfo got our objectID
215        if (this->playerID_ != OBJECTID_UNKNOWN)
216        {
217            this->player_ = dynamic_cast<PlayerInfo*>(Synchronisable::getSynchronisable(this->playerID_));
218            if (this->player_ && (this->player_->getControllableEntity() != this))
219                this->player_->startControl(this);
220        }
221    }
222
223    void ControllableEntity::networkcallback_changedgtinfoID()
224    {
225        if (this->gtinfoID_ != OBJECTID_UNKNOWN)
226        {
227            this->gtinfo_ = dynamic_cast<GametypeInfo*>(Synchronisable::getSynchronisable(this->gtinfoID_));
228
229            if (!this->gtinfo_)
230                this->gtinfoID_ = OBJECTID_UNKNOWN;
231        }
232    }
233
234    void ControllableEntity::startLocalHumanControl()
235    {
236        if (!this->camera_)
237        {
238            this->camera_ = new Camera(this);
239            this->camera_->requestFocus();
240            if (this->cameraPositionTemplate_ != "")
241                this->addTemplate(this->cameraPositionTemplate_);
242            if (this->cameraPositions_.size() > 0)
243                this->cameraPositions_.front()->attachCamera(this->camera_);
244            else
245                this->attach(this->camera_);
246        }
247
248        if (!this->hud_)
249        {
250            if (this->hudtemplate_ != "")
251            {
252                this->hud_ = new OverlayGroup(this);
253                this->hud_->addTemplate(this->hudtemplate_);
254                this->hud_->setOwner(this);
255            }
256        }
257    }
258
259    void ControllableEntity::stopLocalHumanControl()
260    {
261        if (this->camera_)
262        {
263            this->camera_->detachFromParent();
264            delete this->camera_;
265            this->camera_ = 0;
266        }
267
268        if (this->hud_)
269        {
270            delete this->hud_;
271            this->hud_ = 0;
272        }
273    }
274
275    void ControllableEntity::tick(float dt)
276    {
277        if (this->isActive())
278        {
279            SUPER(ControllableEntity, tick, dt);
280
281            this->velocity_ += (dt * this->acceleration_);
282            this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
283
284            if (Core::isMaster())
285            {
286                this->server_velocity_ = this->velocity_;
287                this->server_position_ = this->node_->getPosition();
288            }
289            else if (this->bHasLocalController_)
290            {
291                this->client_velocity_ = this->velocity_;
292                this->client_position_ = this->node_->getPosition();
293            }
294        }
295    }
296
297    void ControllableEntity::registerVariables()
298    {
299        REGISTERSTRING(this->cameraPositionTemplate_, direction::toclient);
300        REGISTERSTRING(this->hudtemplate_, direction::toclient);
301
302        REGISTERDATA(this->client_overwrite_,   direction::toserver);
303
304        REGISTERDATA(this->server_position_,    direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
305        REGISTERDATA(this->server_velocity_,    direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity));
306        REGISTERDATA(this->server_orientation_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
307        REGISTERDATA(this->server_overwrite_,   direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
308
309        REGISTERDATA(this->client_position_,    direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
310        REGISTERDATA(this->client_velocity_,    direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity));
311        REGISTERDATA(this->client_orientation_, direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
312
313
314        REGISTERDATA(this->playerID_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
315        REGISTERDATA(this->gtinfoID_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID));
316    }
317
318    void ControllableEntity::processServerPosition()
319    {
320        if (!this->bHasLocalController_)
321            this->node_->setPosition(this->server_position_);
322    }
323
324    void ControllableEntity::processServerVelocity()
325    {
326        if (!this->bHasLocalController_)
327            this->velocity_ = this->server_velocity_;
328    }
329
330    void ControllableEntity::processServerOrientation()
331    {
332        if (!this->bHasLocalController_)
333            this->node_->setOrientation(this->server_orientation_);
334    }
335
336    void ControllableEntity::processOverwrite()
337    {
338        if (this->bHasLocalController_)
339        {
340            this->setPosition(this->server_position_);
341            this->setVelocity(this->server_velocity_);
342            this->setOrientation(this->server_orientation_);
343
344            this->client_overwrite_ = this->server_overwrite_;
345        }
346    }
347
348    void ControllableEntity::processClientPosition()
349    {
350        if (this->server_overwrite_ == this->client_overwrite_)
351        {
352            this->node_->setPosition(this->client_position_);
353            this->server_position_ = this->client_position_;
354        }
355    }
356
357    void ControllableEntity::processClientVelocity()
358    {
359        if (this->server_overwrite_ == this->client_overwrite_)
360        {
361            this->velocity_ = this->client_velocity_;
362            this->server_velocity_ = this->client_velocity_;
363        }
364    }
365
366    void ControllableEntity::processClientOrientation()
367    {
368        if (this->server_overwrite_ == this->client_overwrite_)
369        {
370            this->node_->setOrientation(this->client_orientation_);
371            this->server_orientation_ = this->client_orientation_;
372        }
373    }
374
375
376    void ControllableEntity::setPosition(const Vector3& position)
377    {
378        if (Core::isMaster())
379        {
380            this->node_->setPosition(position);
381            this->server_position_ = position;
382            ++this->server_overwrite_;
383        }
384        else if (this->bHasLocalController_)
385        {
386            this->node_->setPosition(position);
387            this->client_position_ = position;
388        }
389    }
390
391    void ControllableEntity::setVelocity(const Vector3& velocity)
392    {
393        if (Core::isMaster())
394        {
395            this->velocity_ = velocity;
396            this->server_velocity_ = velocity;
397            ++this->server_overwrite_;
398        }
399        else if (this->bHasLocalController_)
400        {
401            this->velocity_ = velocity;
402            this->client_velocity_ = velocity;
403        }
404    }
405
406    void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
407    {
408        if (Core::isMaster())
409        {
410            this->node_->translate(distance, relativeTo);
411            this->server_position_ = this->node_->getPosition();
412            ++this->server_overwrite_;
413        }
414        else if (this->bHasLocalController_)
415        {
416            this->node_->translate(distance, relativeTo);
417            this->client_position_ = this->node_->getPosition();
418        }
419    }
420
421    void ControllableEntity::setOrientation(const Quaternion& orientation)
422    {
423        if (Core::isMaster())
424        {
425            this->node_->setOrientation(orientation);
426            this->server_orientation_ = orientation;
427            ++this->server_overwrite_;
428        }
429        else if (this->bHasLocalController_)
430        {
431            this->node_->setOrientation(orientation);
432            this->client_orientation_ = orientation;
433        }
434    }
435
436    void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
437    {
438        if (Core::isMaster())
439        {
440            this->node_->rotate(rotation, relativeTo);
441            this->server_orientation_ = this->node_->getOrientation();
442            ++this->server_overwrite_;
443        }
444        else if (this->bHasLocalController_)
445        {
446            this->node_->rotate(rotation, relativeTo);
447            this->client_orientation_ = this->node_->getOrientation();
448        }
449    }
450
451    void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
452    {
453        if (Core::isMaster())
454        {
455            this->node_->yaw(angle, relativeTo);
456            this->server_orientation_ = this->node_->getOrientation();
457            ++this->server_overwrite_;
458        }
459        else if (this->bHasLocalController_)
460        {
461            this->node_->yaw(angle, relativeTo);
462            this->client_orientation_ = this->node_->getOrientation();
463        }
464    }
465
466    void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
467    {
468        if (Core::isMaster())
469        {
470            this->node_->pitch(angle, relativeTo);
471            this->server_orientation_ = this->node_->getOrientation();
472            ++this->server_overwrite_;
473        }
474        else if (this->bHasLocalController_)
475        {
476            this->node_->pitch(angle, relativeTo);
477            this->client_orientation_ = this->node_->getOrientation();
478        }
479    }
480
481    void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
482    {
483        if (Core::isMaster())
484        {
485            this->node_->roll(angle, relativeTo);
486            this->server_orientation_ = this->node_->getOrientation();
487            ++this->server_overwrite_;
488        }
489        else if (this->bHasLocalController_)
490        {
491            this->node_->roll(angle, relativeTo);
492            this->client_orientation_ = this->node_->getOrientation();
493        }
494    }
495
496    void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
497    {
498        if (Core::isMaster())
499        {
500            this->node_->lookAt(target, relativeTo, localDirectionVector);
501            this->server_orientation_ = this->node_->getOrientation();
502            ++this->server_overwrite_;
503        }
504        else if (this->bHasLocalController_)
505        {
506            this->node_->lookAt(target, relativeTo, localDirectionVector);
507            this->client_orientation_ = this->node_->getOrientation();
508        }
509    }
510
511    void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
512    {
513        if (Core::isMaster())
514        {
515            this->node_->setDirection(direction, relativeTo, localDirectionVector);
516            this->server_orientation_ = this->node_->getOrientation();
517            ++this->server_overwrite_;
518        }
519        else if (this->bHasLocalController_)
520        {
521            this->node_->setDirection(direction, relativeTo, localDirectionVector);
522            this->client_orientation_ = this->node_->getOrientation();
523        }
524    }
525}
Note: See TracBrowser for help on using the repository browser.