Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/orxonox/objects/WorldEntity.cc @ 1263

Last change on this file since 1263 was 1263, checked in by rgrieder, 16 years ago
  • merged ogre branch into merge branch
File size: 5.8 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    WorldEntity::~WorldEntity()
70    {
71    }
72
73    void WorldEntity::tick(float dt)
74    {
75        if (!this->bStatic_)
76        {
77            this->velocity_ += (dt * this->acceleration_);
78            this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL);
79
80            this->rotationRate_ += (dt * this->momentum_);
81            this->rotate(this->rotationAxis_, dt * this->rotationRate_);
82        }
83    }
84
85    void WorldEntity::loadParams(TiXmlElement* xmlElem)
86    {
87
88        BaseObject::loadParams(xmlElem);
89        create();
90    }
91
92    void WorldEntity::setYawPitchRoll(const Degree& yaw, const Degree& pitch, const Degree& roll)
93    {
94        this->yaw(yaw);
95        this->pitch(pitch);
96        this->roll(roll);
97    }
98
99    /**
100        @brief XML loading and saving.
101        @param xmlelement The XML-element
102        @param loading Loading (true) or saving (false)
103        @return The XML-element
104    */
105    void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
106    {
107        BaseObject::XMLPort(xmlelement, mode);
108
109        XMLPortParam(WorldEntity, "position", setPositionLoader2, getPosition, xmlelement, mode);
110        XMLPortParamLoadOnly(WorldEntity, "direction", setDirectionLoader, xmlelement, mode);
111        XMLPortParamLoadOnly(WorldEntity, "yawpitchroll", setYawPitchRoll, xmlelement, mode);
112        XMLPortParam(WorldEntity, "scale", setTotalScale, getScale, xmlelement, mode);
113        XMLPortParam(WorldEntity, "rotationAxis", setRotationAxisLoader, getRotationAxis, xmlelement, mode);
114        XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, mode);
115
116        XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, mode, false, true);
117    }
118
119
120    void WorldEntity::registerAllVariables()
121    {
122      // register coordinates
123      registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA);
124      registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA);
125      registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA);
126      // register orientation
127      registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA);
128      registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA);
129      registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA);
130      registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA);
131      // not needed at the moment, because we don't have prediction yet
132      // register velocity_
133      registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA);
134      registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA);
135      registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA);
136      // register rotationAxis/rate
137      registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA);
138      registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA);
139      registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA);
140      registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA);
141    }
142
143    void WorldEntity::attachWorldEntity(WorldEntity* entity)
144    {
145        this->attachedWorldEntities_.push_back(entity);
146    }
147
148    const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index) const
149    {
150        if (index < this->attachedWorldEntities_.size())
151            return this->attachedWorldEntities_[index];
152        else
153            return 0;
154    }
155}
Note: See TracBrowser for help on using the repository browser.