Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2437 was 2428, checked in by landauf, 16 years ago
  • Moved some variables from Gametype to GametypeInfo (Gametype exists only on the Server while GametypeInfo is synchronized)
  • Created a new HUD-element, GametypeStatus, based on GametypeInfo (the same effect was formerly achieved by using a hack in Spectator and a TextOverlay)
  • HumanController creates a HUD (additionally to the SpaceShips HUD) which includes GametypeStatus and ChatOverlay and even more in the future
  • Property svn:eol-style set to native
File size: 17.1 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_)
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        this->camera_->detachFromParent();
253        delete this->camera_;
254        this->camera_ = 0;
255
256        delete this->hud_;
257        this->hud_ = 0;
258    }
259
260    void ControllableEntity::tick(float dt)
261    {
262        if (this->isActive())
263        {
264            SUPER(ControllableEntity, tick, dt);
265
266            this->velocity_ += (dt * this->acceleration_);
267            this->node_->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
268
269            if (Core::isMaster())
270            {
271                this->server_velocity_ = this->velocity_;
272                this->server_position_ = this->node_->getPosition();
273            }
274            else if (this->bHasLocalController_)
275            {
276                this->client_velocity_ = this->velocity_;
277                this->client_position_ = this->node_->getPosition();
278            }
279        }
280    }
281
282    void ControllableEntity::registerVariables()
283    {
284        REGISTERSTRING(this->cameraPositionTemplate_, direction::toclient);
285        REGISTERSTRING(this->hudtemplate_, direction::toclient);
286
287        REGISTERDATA(this->client_overwrite_,   direction::toserver);
288
289        REGISTERDATA(this->server_position_,    direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerPosition));
290        REGISTERDATA(this->server_velocity_,    direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerVelocity));
291        REGISTERDATA(this->server_orientation_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processServerOrientation));
292        REGISTERDATA(this->server_overwrite_,   direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processOverwrite));
293
294        REGISTERDATA(this->client_position_,    direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientPosition));
295        REGISTERDATA(this->client_velocity_,    direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientVelocity));
296        REGISTERDATA(this->client_orientation_, direction::toserver, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::processClientOrientation));
297
298
299        REGISTERDATA(this->playerID_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedplayerID));
300        REGISTERDATA(this->gtinfoID_, direction::toclient, new NetworkCallback<ControllableEntity>(this, &ControllableEntity::networkcallback_changedgtinfoID));
301    }
302
303    void ControllableEntity::processServerPosition()
304    {
305        if (!this->bHasLocalController_)
306            this->node_->setPosition(this->server_position_);
307    }
308
309    void ControllableEntity::processServerVelocity()
310    {
311        if (!this->bHasLocalController_)
312            this->velocity_ = this->server_velocity_;
313    }
314
315    void ControllableEntity::processServerOrientation()
316    {
317        if (!this->bHasLocalController_)
318            this->node_->setOrientation(this->server_orientation_);
319    }
320
321    void ControllableEntity::processOverwrite()
322    {
323        if (this->bHasLocalController_)
324        {
325            this->setPosition(this->server_position_);
326            this->setVelocity(this->server_velocity_);
327            this->setOrientation(this->server_orientation_);
328
329            this->client_overwrite_ = this->server_overwrite_;
330        }
331    }
332
333    void ControllableEntity::processClientPosition()
334    {
335        if (this->server_overwrite_ == this->client_overwrite_)
336        {
337            this->node_->setPosition(this->client_position_);
338            this->server_position_ = this->client_position_;
339        }
340    }
341
342    void ControllableEntity::processClientVelocity()
343    {
344        if (this->server_overwrite_ == this->client_overwrite_)
345        {
346            this->velocity_ = this->client_velocity_;
347            this->server_velocity_ = this->client_velocity_;
348        }
349    }
350
351    void ControllableEntity::processClientOrientation()
352    {
353        if (this->server_overwrite_ == this->client_overwrite_)
354        {
355            this->node_->setOrientation(this->client_orientation_);
356            this->server_orientation_ = this->client_orientation_;
357        }
358    }
359
360
361    void ControllableEntity::setPosition(const Vector3& position)
362    {
363        if (Core::isMaster())
364        {
365            this->node_->setPosition(position);
366            this->server_position_ = position;
367            ++this->server_overwrite_;
368        }
369        else if (this->bHasLocalController_)
370        {
371            this->node_->setPosition(position);
372            this->client_position_ = position;
373        }
374    }
375
376    void ControllableEntity::setVelocity(const Vector3& velocity)
377    {
378        if (Core::isMaster())
379        {
380            this->velocity_ = velocity;
381            this->server_velocity_ = velocity;
382            ++this->server_overwrite_;
383        }
384        else if (this->bHasLocalController_)
385        {
386            this->velocity_ = velocity;
387            this->client_velocity_ = velocity;
388        }
389    }
390
391    void ControllableEntity::translate(const Vector3& distance, Ogre::Node::TransformSpace relativeTo)
392    {
393        if (Core::isMaster())
394        {
395            this->node_->translate(distance, relativeTo);
396            this->server_position_ = this->node_->getPosition();
397            ++this->server_overwrite_;
398        }
399        else if (this->bHasLocalController_)
400        {
401            this->node_->translate(distance, relativeTo);
402            this->client_position_ = this->node_->getPosition();
403        }
404    }
405
406    void ControllableEntity::setOrientation(const Quaternion& orientation)
407    {
408        if (Core::isMaster())
409        {
410            this->node_->setOrientation(orientation);
411            this->server_orientation_ = orientation;
412            ++this->server_overwrite_;
413        }
414        else if (this->bHasLocalController_)
415        {
416            this->node_->setOrientation(orientation);
417            this->client_orientation_ = orientation;
418        }
419    }
420
421    void ControllableEntity::rotate(const Quaternion& rotation, Ogre::Node::TransformSpace relativeTo)
422    {
423        if (Core::isMaster())
424        {
425            this->node_->rotate(rotation, relativeTo);
426            this->server_orientation_ = this->node_->getOrientation();
427            ++this->server_overwrite_;
428        }
429        else if (this->bHasLocalController_)
430        {
431            this->node_->rotate(rotation, relativeTo);
432            this->client_orientation_ = this->node_->getOrientation();
433        }
434    }
435
436    void ControllableEntity::yaw(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
437    {
438        if (Core::isMaster())
439        {
440            this->node_->yaw(angle, relativeTo);
441            this->server_orientation_ = this->node_->getOrientation();
442            ++this->server_overwrite_;
443        }
444        else if (this->bHasLocalController_)
445        {
446            this->node_->yaw(angle, relativeTo);
447            this->client_orientation_ = this->node_->getOrientation();
448        }
449    }
450
451    void ControllableEntity::pitch(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
452    {
453        if (Core::isMaster())
454        {
455            this->node_->pitch(angle, relativeTo);
456            this->server_orientation_ = this->node_->getOrientation();
457            ++this->server_overwrite_;
458        }
459        else if (this->bHasLocalController_)
460        {
461            this->node_->pitch(angle, relativeTo);
462            this->client_orientation_ = this->node_->getOrientation();
463        }
464    }
465
466    void ControllableEntity::roll(const Degree& angle, Ogre::Node::TransformSpace relativeTo)
467    {
468        if (Core::isMaster())
469        {
470            this->node_->roll(angle, relativeTo);
471            this->server_orientation_ = this->node_->getOrientation();
472            ++this->server_overwrite_;
473        }
474        else if (this->bHasLocalController_)
475        {
476            this->node_->roll(angle, relativeTo);
477            this->client_orientation_ = this->node_->getOrientation();
478        }
479    }
480
481    void ControllableEntity::lookAt(const Vector3& target, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
482    {
483        if (Core::isMaster())
484        {
485            this->node_->lookAt(target, relativeTo, localDirectionVector);
486            this->server_orientation_ = this->node_->getOrientation();
487            ++this->server_overwrite_;
488        }
489        else if (this->bHasLocalController_)
490        {
491            this->node_->lookAt(target, relativeTo, localDirectionVector);
492            this->client_orientation_ = this->node_->getOrientation();
493        }
494    }
495
496    void ControllableEntity::setDirection(const Vector3& direction, Ogre::Node::TransformSpace relativeTo, const Vector3& localDirectionVector)
497    {
498        if (Core::isMaster())
499        {
500            this->node_->setDirection(direction, relativeTo, localDirectionVector);
501            this->server_orientation_ = this->node_->getOrientation();
502            ++this->server_overwrite_;
503        }
504        else if (this->bHasLocalController_)
505        {
506            this->node_->setDirection(direction, relativeTo, localDirectionVector);
507            this->client_orientation_ = this->node_->getOrientation();
508        }
509    }
510}
Note: See TracBrowser for help on using the repository browser.