Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2350 was 2256, checked in by landauf, 16 years ago

The SpaceShips HUD is working again (Speedbar and Radar)

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