Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2447 was 2447, checked in by landauf, 15 years ago
  • Removed directional-light-hack from Scene
  • Many changes in Light, works in all game-modes (standalone, dedicated, server and client)
  • Fixed a bug which caused clients to not having shadows

There's still a big problem, bug I can't explain it.

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