Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1989 was 1989, checked in by landauf, 15 years ago
  • added ControllableEntity, the baseclass of all players, vehicles and ships.
  • moved Template to core
  • some changes in Camera
  • added 6 constants to WorldEntity to identify relative directions
  • changed vom Radian to Degree as default angle unit
File size: 5.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 "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    const Vector3 WorldEntity::FRONT = Vector3::NEGATIVE_UNIT_Z;
42    const Vector3 WorldEntity::BACK  = Vector3::UNIT_Z;
43    const Vector3 WorldEntity::LEFT  = Vector3::NEGATIVE_UNIT_X;
44    const Vector3 WorldEntity::RIGHT = Vector3::UNIT_X;
45    const Vector3 WorldEntity::DOWN  = Vector3::NEGATIVE_UNIT_Y;
46    const Vector3 WorldEntity::UP    = Vector3::UNIT_Y;
47
48    WorldEntity::WorldEntity()
49    {
50        RegisterObject(WorldEntity);
51
52        this->node_ = GraphicsEngine::getInstance().getLevelSceneManager()->createSceneNode();
53        this->parent_ = 0;
54        this->parentID_ = (unsigned int)-1;
55
56        this->node_->setPosition(Vector3::ZERO);
57        this->node_->setOrientation(Quaternion::IDENTITY);
58
59        this->registerVariables();
60    }
61
62    WorldEntity::~WorldEntity()
63    {
64        if (this->isInitialized())
65        {
66            this->node_->detachAllObjects();
67            GraphicsEngine::getInstance().getLevelSceneManager()->destroySceneNode(this->node_->getName());
68        }
69    }
70
71
72    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(WorldEntity, XMLPort, xmlelement, mode);
75
76        XMLPortParamTemplate(WorldEntity, "position", setPosition, getPosition, xmlelement, mode, const Vector3&);
77        XMLPortParamTemplate(WorldEntity, "orientation", setOrientation, getOrientation, xmlelement, mode, const Quaternion&);
78        XMLPortParamLoadOnly(WorldEntity, "lookat", lookAt_xmlport, xmlelement, mode);
79        XMLPortParamLoadOnly(WorldEntity, "direction", setDirection_xmlport, xmlelement, mode);
80        XMLPortParamLoadOnly(WorldEntity, "yaw", yaw_xmlport, xmlelement, mode);
81        XMLPortParamLoadOnly(WorldEntity, "pitch", pitch_xmlport, xmlelement, mode);
82        XMLPortParamLoadOnly(WorldEntity, "roll", roll_xmlport, xmlelement, mode);
83        XMLPortParamTemplate(WorldEntity, "scale3D", setScale3D, getScale3D, xmlelement, mode, const Vector3&);
84        XMLPortParam(WorldEntity, "scale", setScale, getScale, xmlelement, mode);
85
86        XMLPortObject(WorldEntity, WorldEntity, "attached", attach, getAttachedObject, xmlelement, mode);
87    }
88
89    void WorldEntity::registerVariables()
90    {
91        REGISTERDATA(this->bActive_,  network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedActivity));
92        REGISTERDATA(this->bVisible_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::changedVisibility));
93
94        REGISTERDATA(this->getScale3D().x, network::direction::toclient);
95        REGISTERDATA(this->getScale3D().y, network::direction::toclient);
96        REGISTERDATA(this->getScale3D().z, network::direction::toclient);
97
98        REGISTERDATA(this->parentID_, network::direction::toclient, new network::NetworkCallback<WorldEntity>(this, &WorldEntity::updateParent));
99    }
100
101    void WorldEntity::updateParent()
102    {
103        WorldEntity* parent = dynamic_cast<WorldEntity*>(Synchronisable::getSynchronisable(this->parentID_));
104        if (parent)
105            this->attachToParent(parent);
106    }
107
108    void WorldEntity::attach(WorldEntity* object)
109    {
110        if (object->getParent())
111            object->detachFromParent();
112
113        this->node_->addChild(object->node_);
114        this->children_.insert(object);
115        object->parent_ = this;
116        object->parentID_ = this->getObjectID();
117    }
118
119    void WorldEntity::detach(WorldEntity* object)
120    {
121        this->node_->removeChild(object->node_);
122        this->children_.erase(object);
123        object->parent_ = 0;
124        object->parentID_ = (unsigned int)-1;
125    }
126
127    WorldEntity* WorldEntity::getAttachedObject(unsigned int index) const
128    {
129        unsigned int i = 0;
130        for (std::set<WorldEntity*>::const_iterator it = this->children_.begin(); it != this->children_.end(); ++it)
131        {
132            if (i == index)
133                return (*it);
134            ++i;
135        }
136        return 0;
137    }
138}
Note: See TracBrowser for help on using the repository browser.