Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

many changes, most important: BaseObject takes now a pointer to it's creator which is needed to build a level hierarchy (with different scenes)

File size: 5.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 <cassert>
33#include <OgreSceneManager.h>
34
35#include "core/CoreIncludes.h"
36#include "core/XMLPort.h"
37#include "util/Convert.h"
38
39#include "GraphicsEngine.h"
40#include "objects/Scene.h"
41
42namespace orxonox
43{
44    const Vector3 WorldEntity::FRONT = Vector3::NEGATIVE_UNIT_Z;
45    const Vector3 WorldEntity::BACK  = Vector3::UNIT_Z;
46    const Vector3 WorldEntity::LEFT  = Vector3::NEGATIVE_UNIT_X;
47    const Vector3 WorldEntity::RIGHT = Vector3::UNIT_X;
48    const Vector3 WorldEntity::DOWN  = Vector3::NEGATIVE_UNIT_Y;
49    const Vector3 WorldEntity::UP    = Vector3::UNIT_Y;
50
51    WorldEntity::WorldEntity(BaseObject* creator) : BaseObject(creator)
52    {
53        RegisterObject(WorldEntity);
54
55        assert(this->getScene());
56        assert(this->getScene()->getRootSceneNode());
57
58        this->node_ = this->getScene()->getRootSceneNode()->createChildSceneNode();
59
60        this->parent_ = 0;
61        this->parentID_ = (unsigned int)-1;
62
63        this->node_->setPosition(Vector3::ZERO);
64        this->node_->setOrientation(Quaternion::IDENTITY);
65
66        this->registerVariables();
67    }
68
69    WorldEntity::~WorldEntity()
70    {
71        if (this->isInitialized())
72        {
73            this->node_->detachAllObjects();
74            if (this->getScene()->getSceneManager())
75                this->getScene()->getSceneManager()->destroySceneNode(this->node_->getName());
76        }
77    }
78
79    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
80    {
81        SUPER(WorldEntity, XMLPort, xmlelement, mode);
82
83        XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
84        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
85        XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode);
86        XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode);
87        XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode);
88        XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode);
89        XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode);
90        XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
91        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
92
93        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
94    }
95
96    void WorldEntity::registerVariables()
97    {
98        REGISTERDATA(this->bActive_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
99        REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
100
101        REGISTERDATA(this->getScale3D().x, network::direction::toclient);
102        REGISTERDATA(this->getScale3D().y, network::direction::toclient);
103        REGISTERDATA(this->getScale3D().z, network::direction::toclient);
104
105        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
106    }
107
108    void WorldEntity::updateParent()
109    {
110        WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
111        if (parent)
112            this->attachToParent(parent);
113    }
114
115    void WorldEntity::attach(WorldEntity* object)
116    {
117        if (object->getParent())
118            object->detachFromParent();
119        else
120        {
121            Ogre::Node* parent = object->node_->getParent();
122            if (parent)
123                parent->removeChild(object->node_);
124        }
125
126        this->node_->addChild(object->node_);
127        this->children_.insert(object);
128        object->parent_ = this;
129        object->parentID_ = this->getObjectID();
130    }
131
132    void WorldEntity::detach(WorldEntity* object)
133    {
134        this->node_->removeChild(object->node_);
135        this->children_.erase(object);
136        object->parent_ = 0;
137        object->parentID_ = (unsigned int)-1;
138
139        this->getScene()->getRootSceneNode()->addChild(object->node_);
140    }
141
142    WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const
143    {
144        unsigned int i = 0;
145        for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it)
146        {
147            if (i == index)
148                return (*it);
149            ++i;
150        }
151        return 0;
152    }
153}
Note: See TracBrowser for help on using the repository browser.