| [513] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
| [1056] | 3 |  *                    > www.orxonox.net < | 
|---|
| [513] | 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: | 
|---|
| [670] | 23 |  *      Fabian 'x3n' Landau | 
|---|
| [513] | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
| [786] | 29 | #include "OrxonoxStableHeaders.h" | 
|---|
| [1039] | 30 | #include "WorldEntity.h" | 
|---|
| [784] | 31 |  | 
|---|
| [715] | 32 | #include <string> | 
|---|
| [497] | 33 | #include <sstream> | 
|---|
 | 34 |  | 
|---|
| [1209] | 35 | #include "tinyxml/tinyxml.h" | 
|---|
| [1064] | 36 | #include "util/SubString.h" | 
|---|
| [1032] | 37 | #include "core/CoreIncludes.h" | 
|---|
 | 38 | #include "GraphicsEngine.h" | 
|---|
| [871] | 39 | #include "core/XMLPort.h" | 
|---|
| [497] | 40 |  | 
|---|
 | 41 | namespace orxonox | 
|---|
 | 42 | { | 
|---|
 | 43 |     CreateFactory(WorldEntity); | 
|---|
 | 44 |  | 
|---|
 | 45 |     unsigned int WorldEntity::worldEntityCounter_s = 0; | 
|---|
 | 46 |  | 
|---|
 | 47 |     WorldEntity::WorldEntity() | 
|---|
 | 48 |     { | 
|---|
 | 49 |         RegisterObject(WorldEntity); | 
|---|
 | 50 |  | 
|---|
| [1021] | 51 |         //create(); | 
|---|
| [497] | 52 |  | 
|---|
 | 53 |         this->bStatic_ = true; | 
|---|
 | 54 |         this->velocity_ = Vector3(0, 0, 0); | 
|---|
 | 55 |         this->acceleration_ = Vector3(0, 0, 0); | 
|---|
 | 56 |         this->rotationAxis_ = Vector3(0, 1, 0); | 
|---|
 | 57 |         this->rotationRate_ = 0; | 
|---|
 | 58 |         this->momentum_ = 0; | 
|---|
| [1056] | 59 |  | 
|---|
| [1032] | 60 |         if (GraphicsEngine::getSingleton().getSceneManager()) | 
|---|
| [1021] | 61 |         { | 
|---|
 | 62 |           std::ostringstream name; | 
|---|
 | 63 |           name << (WorldEntity::worldEntityCounter_s++); | 
|---|
 | 64 |           this->setName("WorldEntity" + name.str()); | 
|---|
| [1032] | 65 |           this->node_ = GraphicsEngine::getSingleton().getSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName()); | 
|---|
| [1056] | 66 |  | 
|---|
| [1021] | 67 |           registerAllVariables(); | 
|---|
 | 68 |         } | 
|---|
 | 69 |         else | 
|---|
 | 70 |         { | 
|---|
 | 71 |           this->node_ = 0; | 
|---|
 | 72 |         } | 
|---|
| [497] | 73 |     } | 
|---|
 | 74 |  | 
|---|
 | 75 |     WorldEntity::~WorldEntity() | 
|---|
 | 76 |     { | 
|---|
 | 77 |     } | 
|---|
 | 78 |  | 
|---|
 | 79 |     void WorldEntity::tick(float dt) | 
|---|
 | 80 |     { | 
|---|
 | 81 |         if (!this->bStatic_) | 
|---|
 | 82 |         { | 
|---|
 | 83 |             this->velocity_ += (dt * this->acceleration_); | 
|---|
| [643] | 84 |             this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL); | 
|---|
| [497] | 85 |  | 
|---|
 | 86 |             this->rotationRate_ += (dt * this->momentum_); | 
|---|
 | 87 |             this->rotate(this->rotationAxis_, dt * this->rotationRate_); | 
|---|
 | 88 |         } | 
|---|
 | 89 |     } | 
|---|
| [583] | 90 |  | 
|---|
 | 91 |     void WorldEntity::loadParams(TiXmlElement* xmlElem) | 
|---|
 | 92 |     { | 
|---|
| [871] | 93 |  | 
|---|
| [583] | 94 |         BaseObject::loadParams(xmlElem); | 
|---|
| [1021] | 95 |         create(); | 
|---|
| [871] | 96 | /* | 
|---|
| [622] | 97 |         if (xmlElem->Attribute("position")) | 
|---|
 | 98 |         { | 
|---|
| [715] | 99 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("position"),","); | 
|---|
| [622] | 100 |             float x, y, z; | 
|---|
 | 101 |             String2Number<float>(x, pos[0]); | 
|---|
 | 102 |             String2Number<float>(y, pos[1]); | 
|---|
 | 103 |             String2Number<float>(z, pos[2]); | 
|---|
 | 104 |             this->setPosition(x, y, z); | 
|---|
 | 105 |         } | 
|---|
| [589] | 106 |  | 
|---|
| [622] | 107 |         if (xmlElem->Attribute("direction")) | 
|---|
 | 108 |         { | 
|---|
| [715] | 109 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("direction"),","); | 
|---|
| [622] | 110 |             float x, y, z; | 
|---|
 | 111 |             String2Number<float>(x, pos[0]); | 
|---|
 | 112 |             String2Number<float>(y, pos[1]); | 
|---|
 | 113 |             String2Number<float>(z, pos[2]); | 
|---|
 | 114 |             this->setDirection(x, y, z); | 
|---|
 | 115 |         } | 
|---|
| [589] | 116 |  | 
|---|
| [595] | 117 |         if (xmlElem->Attribute("yaw") || xmlElem->Attribute("pitch") || xmlElem->Attribute("roll")) | 
|---|
 | 118 |         { | 
|---|
 | 119 |             float yaw = 0.0, pitch = 0.0, roll = 0.0; | 
|---|
 | 120 |             if (xmlElem->Attribute("yaw")) | 
|---|
 | 121 |                 String2Number<float>(yaw,xmlElem->Attribute("yaw")); | 
|---|
 | 122 |  | 
|---|
 | 123 |             if (xmlElem->Attribute("pitch")) | 
|---|
 | 124 |                 String2Number<float>(pitch,xmlElem->Attribute("pitch")); | 
|---|
 | 125 |  | 
|---|
 | 126 |             if (xmlElem->Attribute("roll")) | 
|---|
 | 127 |                 String2Number<float>(roll,xmlElem->Attribute("roll")); | 
|---|
 | 128 |  | 
|---|
 | 129 |             this->yaw(Degree(yaw)); | 
|---|
 | 130 |             this->pitch(Degree(pitch)); | 
|---|
 | 131 |             this->roll(Degree(roll)); | 
|---|
 | 132 |         } | 
|---|
 | 133 |  | 
|---|
| [622] | 134 |         if (xmlElem->Attribute("scale")) | 
|---|
 | 135 |         { | 
|---|
| [715] | 136 |             std::string scaleStr = xmlElem->Attribute("scale"); | 
|---|
| [622] | 137 |             float scale; | 
|---|
 | 138 |             String2Number<float>(scale, scaleStr); | 
|---|
 | 139 |             this->setScale(scale); | 
|---|
 | 140 |         } | 
|---|
 | 141 |  | 
|---|
 | 142 |         if (xmlElem->Attribute("rotationAxis")) | 
|---|
 | 143 |         { | 
|---|
| [715] | 144 |             std::vector<std::string> pos = tokenize(xmlElem->Attribute("rotationAxis"),","); | 
|---|
| [622] | 145 |             float x, y, z; | 
|---|
 | 146 |             String2Number<float>(x, pos[0]); | 
|---|
 | 147 |             String2Number<float>(y, pos[1]); | 
|---|
 | 148 |             String2Number<float>(z, pos[2]); | 
|---|
 | 149 |             this->setRotationAxis(x, y, z); | 
|---|
 | 150 |         } | 
|---|
 | 151 |  | 
|---|
 | 152 |         if (xmlElem->Attribute("rotationRate")) | 
|---|
 | 153 |         { | 
|---|
 | 154 |             float rotationRate; | 
|---|
 | 155 |             String2Number<float>(rotationRate, xmlElem->Attribute("rotationRate")); | 
|---|
 | 156 |             this->setRotationRate(Degree(rotationRate)); | 
|---|
 | 157 |             if (rotationRate != 0) | 
|---|
 | 158 |                 this->setStatic(false); | 
|---|
 | 159 |         } | 
|---|
 | 160 |  | 
|---|
| [630] | 161 |         create(); | 
|---|
| [871] | 162 | */ | 
|---|
 | 163 |     } | 
|---|
| [643] | 164 |  | 
|---|
| [871] | 165 |     void WorldEntity::setYawPitchRoll(const Degree& yaw, const Degree& pitch, const Degree& roll) | 
|---|
 | 166 |     { | 
|---|
 | 167 |         this->yaw(yaw); | 
|---|
 | 168 |         this->pitch(pitch); | 
|---|
 | 169 |         this->roll(roll); | 
|---|
| [583] | 170 |     } | 
|---|
 | 171 |  | 
|---|
| [871] | 172 |     /** | 
|---|
 | 173 |         @brief XML loading and saving. | 
|---|
 | 174 |         @param xmlelement The XML-element | 
|---|
 | 175 |         @param loading Loading (true) or saving (false) | 
|---|
 | 176 |         @return The XML-element | 
|---|
 | 177 |     */ | 
|---|
| [1052] | 178 |     void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| [871] | 179 |     { | 
|---|
| [1052] | 180 |         BaseObject::XMLPort(xmlelement, mode); | 
|---|
| [871] | 181 |  | 
|---|
| [1052] | 182 |         XMLPortParam(WorldEntity, "position", setPositionLoader2, getPosition, xmlelement, mode); | 
|---|
 | 183 |         XMLPortParamLoadOnly(WorldEntity, "direction", setDirectionLoader, xmlelement, mode); | 
|---|
 | 184 |         XMLPortParamLoadOnly(WorldEntity, "yawpitchroll", setYawPitchRoll, xmlelement, mode); | 
|---|
 | 185 |         XMLPortParam(WorldEntity, "scale", setTotalScale, getScale, xmlelement, mode); | 
|---|
 | 186 |         XMLPortParam(WorldEntity, "rotationAxis", setRotationAxisLoader, getRotationAxis, xmlelement, mode); | 
|---|
 | 187 |         XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, mode); | 
|---|
| [871] | 188 |  | 
|---|
| [1052] | 189 |         XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, mode, false, true); | 
|---|
| [871] | 190 |     } | 
|---|
 | 191 |  | 
|---|
| [643] | 192 |  | 
|---|
| [583] | 193 |     void WorldEntity::registerAllVariables() | 
|---|
 | 194 |     { | 
|---|
| [1021] | 195 |       // register coordinates | 
|---|
| [630] | 196 |       registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA); | 
|---|
 | 197 |       registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA); | 
|---|
 | 198 |       registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA); | 
|---|
 | 199 |       // register orientation | 
|---|
 | 200 |       registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA); | 
|---|
 | 201 |       registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA); | 
|---|
 | 202 |       registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA); | 
|---|
| [1021] | 203 |       registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA); | 
|---|
| [630] | 204 |       // not needed at the moment, because we don't have prediction yet | 
|---|
| [1021] | 205 |       // register velocity_ | 
|---|
| [630] | 206 |       registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA); | 
|---|
 | 207 |       registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA); | 
|---|
 | 208 |       registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA); | 
|---|
 | 209 |       // register rotationAxis/rate | 
|---|
 | 210 |       registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA); | 
|---|
 | 211 |       registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA); | 
|---|
 | 212 |       registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA); | 
|---|
| [1021] | 213 |       registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA); | 
|---|
| [565] | 214 |     } | 
|---|
| [871] | 215 |  | 
|---|
 | 216 |     void WorldEntity::attachWorldEntity(WorldEntity* entity) | 
|---|
 | 217 |     { | 
|---|
 | 218 |         this->attachedWorldEntities_.push_back(entity); | 
|---|
 | 219 |     } | 
|---|
 | 220 |  | 
|---|
| [1052] | 221 |     const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index) const | 
|---|
| [871] | 222 |     { | 
|---|
 | 223 |         if (index < this->attachedWorldEntities_.size()) | 
|---|
 | 224 |             return this->attachedWorldEntities_[index]; | 
|---|
 | 225 |         else | 
|---|
 | 226 |             return 0; | 
|---|
 | 227 |     } | 
|---|
| [497] | 228 | } | 
|---|