Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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