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