Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/WorldEntity.cc @ 1558

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

added support for isVisible() and isActive() to all objects.
those functions are provided by BaseObject, combined with virtual functions changedVisibility and changedActivity respectively.
use them, to make orxonox scriptable, say: to change the state of objects with 2 simple functions instead of changing all meshes and emitters and billboards of an object. don't forget to pass calls to changedVisibility and changedActivity to the parent class.

additionally there is a new function, isInitialized(), provided by BaseObject too. this returns true if the constructor of BaseObject passed the object registration. this allows you, to check whether an object was properly initialized or if the constructor returned (as this is the case while creating the class hierarchy). use isInitialized() in your destructor before deleting something.

  • Property svn:eol-style set to native
File size: 7.4 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 <string>
33#include <sstream>
34
35#include "tinyxml/tinyxml.h"
36#include "util/SubString.h"
37#include "core/CoreIncludes.h"
38#include "GraphicsEngine.h"
39#include "core/XMLPort.h"
40
41namespace orxonox
42{
43    CreateFactory(WorldEntity);
44
45    unsigned int WorldEntity::worldEntityCounter_s = 0;
46
47    WorldEntity::WorldEntity() :
48      velocity_    (0, 0, 0),
49      acceleration_(0, 0, 0),
50      rotationAxis_(0, 1, 0),
51      rotationRate_(0),
52      momentum_    (0),
53      node_        (0),
54      bStatic_     (true)
55    {
56        RegisterObject(WorldEntity);
57
58        if (GraphicsEngine::getSingleton().getSceneManager())
59        {
60          std::ostringstream name;
61          name << (WorldEntity::worldEntityCounter_s++);
62          this->setName("WorldEntity" + name.str());
63          this->node_ = GraphicsEngine::getSingleton().getSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName());
64
65          registerAllVariables();
66        }
67    }
68
69
70    WorldEntity::~WorldEntity()
71    {
72        // just to make sure we clean out all scene nodes
73        if(this->getNode())
74        {
75            this->getNode()->removeAndDestroyAllChildren();
76            GraphicsEngine::getSingleton().getSceneManager()->destroySceneNode(this->getName());
77        }
78    }
79
80    void WorldEntity::tick(float dt)
81    {
82        if (!this->bStatic_ && this->isActive())
83        {
84//             COUT(4) << "acceleration: " << this->acceleration_ << " velocity: " << this->velocity_ << std::endl;
85            this->velocity_ += (dt * this->acceleration_);
86            this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
87
88            this->rotationRate_ += (dt * this->momentum_);
89            this->rotate(this->rotationAxis_, dt * this->rotationRate_);
90            //COUT(3) << "rotationrate: " << this->rotationRate_.valueDegrees() << " momentum: " << this->momentum_.valueDegrees() << std::endl;
91        }
92    }
93
94    void WorldEntity::loadParams(TiXmlElement* xmlElem)
95    {
96
97        BaseObject::loadParams(xmlElem);
98        create();
99    }
100
101
102    void WorldEntity::setYawPitchRoll(const Degree& yaw, const Degree& pitch, const Degree& roll)
103    {
104        this->yaw(yaw);
105        this->pitch(pitch);
106        this->roll(roll);
107    }
108
109    /**
110        @brief XML loading and saving.
111        @param xmlelement The XML-element
112        @param loading Loading (true) or saving (false)
113        @return The XML-element
114    */
115    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
116    {
117        BaseObject::XMLPort(xmlelement, mode);
118
119        XMLPortParam(WorldEntity, "position", setPositionLoader2, getPosition, xmlelement, mode);
120        XMLPortParamLoadOnly(WorldEntity, "direction", setDirectionLoader, xmlelement, mode);
121        XMLPortParamLoadOnly(WorldEntity, "yawpitchroll", setYawPitchRoll, xmlelement, mode);
122        XMLPortParam(WorldEntity, "scale", setTotalScale, getScale, xmlelement, mode);
123        XMLPortParam(WorldEntity, "rotationAxis", setRotationAxisLoader, getRotationAxis, xmlelement, mode);
124        XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, mode);
125
126        XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, mode, false, true);
127
128        WorldEntity::create();
129    }
130
131
132    void WorldEntity::registerAllVariables()
133    {
134      // register inheritec variables from BaseObject
135      registerVar( (void*) &(this->bActive_), sizeof(this->bActive_), network::DATA, 0x3);
136      registerVar( (void*) &(this->bVisible_), sizeof(this->bVisible_), network::DATA, 0x3);
137      // register coordinates
138      registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA, 0x3);
139      registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA, 0x3);
140      registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA, 0x3);
141      // register orientation
142      registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA, 0x3);
143      registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA, 0x3);
144      registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA, 0x3);
145      registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA, 0x3);
146      // register velocity_
147      registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA, 0x3);
148      registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA, 0x3);
149      registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA, 0x3);
150      // register rotationAxis/rate
151      registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA, 0x3);
152      registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA, 0x3);
153      registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA, 0x3);
154      registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA, 0x3);
155      // register scale of node
156      registerVar( (void*) &(this->getScale().x), sizeof(this->getScale().x), network::DATA, 0x3);
157      registerVar( (void*) &(this->getScale().y), sizeof(this->getScale().y), network::DATA, 0x3);
158      registerVar( (void*) &(this->getScale().z), sizeof(this->getScale().z), network::DATA, 0x3);
159      //register staticity
160      registerVar( (void*) &(this->bStatic_), sizeof(this->bStatic_), network::DATA, 0x3);
161      //register acceleration & momentum
162//       registerVar( (void*) &(this->getAcceleration().x), sizeof(this->getAcceleration().x), network::DATA, 0x2);
163//       registerVar( (void*) &(this->getAcceleration().y), sizeof(this->getAcceleration().y), network::DATA, 0x2);
164//       registerVar( (void*) &(this->getAcceleration().z), sizeof(this->getAcceleration().z), network::DATA, 0x2);
165//       registerVar( (void*) &(this->getMomentum()), sizeof(this->getMomentum()), network::DATA, 0x2); // only backsync
166    }
167
168    void WorldEntity::attachWorldEntity(WorldEntity* entity)
169    {
170        this->attachedWorldEntities_.push_back(entity);
171    }
172
173    const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index) const
174    {
175        if (index < this->attachedWorldEntities_.size())
176            return this->attachedWorldEntities_[index];
177        else
178            return 0;
179    }
180}
Note: See TracBrowser for help on using the repository browser.