Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy/src/orxonox/objects/worldentities/WorldEntity.cc @ 1948

Last change on this file since 1948 was 1948, checked in by rgrieder, 16 years ago

Fixed a bug with an index variable not being set to 0 first.

File size: 4.6 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 "WorldEntity.h"
31
32#include <OgreSceneManager.h>
33
34#include "core/CoreIncludes.h"
35#include "core/XMLPort.h"
36
37#include "GraphicsEngine.h"
38
39namespace orxonox
40{
41    WorldEntity::WorldEntity()
42    {
43        RegisterObject(WorldEntity);
44
45        this->node_ = GraphicsEngine::getInstance().getLevelSceneManager()->createSceneNode();
46        this->parent_ = 0;
47        this->parentID_ = (unsigned int)-1;
48
49        this->registerVariables();
50    }
51
52    WorldEntity::~WorldEntity()
53    {
54        if (this->isInitialized())
55        {
56            this->node_->detachAllObjects();
57            GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->node_->getName());
58        }
59    }
60
61
62    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
63    {
64        SUPER(WorldEntity, XMLPort, xmlelement, mode);
65
66        XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
67        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
68        XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode);
69        XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode);
70        XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode);
71        XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode);
72        XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode);
73        XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
74        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
75
76        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
77    }
78
79    void WorldEntity::registerVariables()
80    {
81        REGISTERDATA(this->bActive_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
82        REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
83
84        REGISTERDATA(this->getScale3D().x, network::direction::toclient);
85        REGISTERDATA(this->getScale3D().y, network::direction::toclient);
86        REGISTERDATA(this->getScale3D().z, network::direction::toclient);
87
88        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
89    }
90
91    void WorldEntity::updateParent()
92    {
93        WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
94        if (parent)
95            this->attachToParent(parent);
96    }
97
98    void WorldEntity::attach(WorldEntity* object)
99    {
100        if (object->getParent())
101            object->detachFromParent();
102
103        this->node_->addChild(object->node_);
104        this->children_.insert(object);
105        object->parent_ = this;
106        object->parentID_ = this->getObjectID();
107    }
108
109    void WorldEntity::detach(WorldEntity* object)
110    {
111        this->node_->removeChild(object->node_);
112        this->children_.erase(object);
113        object->parent_ = 0;
114        object->parentID_ = (unsigned int)-1;
115    }
116
117    WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const
118    {
119        unsigned int i = 0;
120        for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it)
121        {
122            if (i == index)
123                return (*it);
124            ++i;
125        }
126        return 0;
127    }
128}
Note: See TracBrowser for help on using the repository browser.