[513] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
[670] | 22 | * Fabian 'x3n' Landau |
---|
[513] | 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[786] | 28 | #include "OrxonoxStableHeaders.h" |
---|
[784] | 29 | |
---|
[715] | 30 | #include <string> |
---|
[497] | 31 | #include <sstream> |
---|
| 32 | |
---|
[742] | 33 | #include "util/tinyxml/tinyxml.h" |
---|
| 34 | #include "util/Tokenizer.h" |
---|
| 35 | #include "util/String2Number.h" |
---|
[497] | 36 | #include "../core/CoreIncludes.h" |
---|
[614] | 37 | #include "../Orxonox.h" |
---|
[708] | 38 | #include "WorldEntity.h" |
---|
[497] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | CreateFactory(WorldEntity); |
---|
| 43 | |
---|
| 44 | unsigned int WorldEntity::worldEntityCounter_s = 0; |
---|
| 45 | |
---|
| 46 | WorldEntity::WorldEntity() |
---|
| 47 | { |
---|
| 48 | RegisterObject(WorldEntity); |
---|
| 49 | |
---|
[554] | 50 | if (Orxonox::getSingleton()->getSceneManager()) |
---|
[497] | 51 | { |
---|
| 52 | std::ostringstream name; |
---|
| 53 | name << (WorldEntity::worldEntityCounter_s++); |
---|
| 54 | this->setName("WorldEntity" + name.str()); |
---|
[633] | 55 | this->node_ = Orxonox::getSingleton()->getSceneManager()->getRootSceneNode()->createChildSceneNode(this->getName()); |
---|
[497] | 56 | } |
---|
[633] | 57 | else |
---|
| 58 | { |
---|
| 59 | this->node_ = 0; |
---|
| 60 | } |
---|
[497] | 61 | |
---|
| 62 | this->bStatic_ = true; |
---|
| 63 | this->velocity_ = Vector3(0, 0, 0); |
---|
| 64 | this->acceleration_ = Vector3(0, 0, 0); |
---|
| 65 | this->rotationAxis_ = Vector3(0, 1, 0); |
---|
| 66 | this->rotationRate_ = 0; |
---|
| 67 | this->momentum_ = 0; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | WorldEntity::~WorldEntity() |
---|
| 71 | { |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void WorldEntity::tick(float dt) |
---|
| 75 | { |
---|
| 76 | if (!this->bStatic_) |
---|
| 77 | { |
---|
| 78 | this->velocity_ += (dt * this->acceleration_); |
---|
[643] | 79 | this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL); |
---|
[497] | 80 | |
---|
| 81 | this->rotationRate_ += (dt * this->momentum_); |
---|
| 82 | this->rotate(this->rotationAxis_, dt * this->rotationRate_); |
---|
| 83 | } |
---|
| 84 | } |
---|
[583] | 85 | |
---|
| 86 | void WorldEntity::loadParams(TiXmlElement* xmlElem) |
---|
| 87 | { |
---|
| 88 | BaseObject::loadParams(xmlElem); |
---|
| 89 | |
---|
[622] | 90 | if (xmlElem->Attribute("position")) |
---|
| 91 | { |
---|
[715] | 92 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("position"),","); |
---|
[622] | 93 | float x, y, z; |
---|
| 94 | String2Number<float>(x, pos[0]); |
---|
| 95 | String2Number<float>(y, pos[1]); |
---|
| 96 | String2Number<float>(z, pos[2]); |
---|
| 97 | this->setPosition(x, y, z); |
---|
| 98 | } |
---|
[589] | 99 | |
---|
[622] | 100 | if (xmlElem->Attribute("direction")) |
---|
| 101 | { |
---|
[715] | 102 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("direction"),","); |
---|
[622] | 103 | float x, y, z; |
---|
| 104 | String2Number<float>(x, pos[0]); |
---|
| 105 | String2Number<float>(y, pos[1]); |
---|
| 106 | String2Number<float>(z, pos[2]); |
---|
| 107 | this->setDirection(x, y, z); |
---|
| 108 | } |
---|
[589] | 109 | |
---|
[595] | 110 | if (xmlElem->Attribute("yaw") || xmlElem->Attribute("pitch") || xmlElem->Attribute("roll")) |
---|
| 111 | { |
---|
| 112 | float yaw = 0.0, pitch = 0.0, roll = 0.0; |
---|
| 113 | if (xmlElem->Attribute("yaw")) |
---|
| 114 | String2Number<float>(yaw,xmlElem->Attribute("yaw")); |
---|
| 115 | |
---|
| 116 | if (xmlElem->Attribute("pitch")) |
---|
| 117 | String2Number<float>(pitch,xmlElem->Attribute("pitch")); |
---|
| 118 | |
---|
| 119 | if (xmlElem->Attribute("roll")) |
---|
| 120 | String2Number<float>(roll,xmlElem->Attribute("roll")); |
---|
| 121 | |
---|
| 122 | this->yaw(Degree(yaw)); |
---|
| 123 | this->pitch(Degree(pitch)); |
---|
| 124 | this->roll(Degree(roll)); |
---|
| 125 | } |
---|
| 126 | |
---|
[622] | 127 | if (xmlElem->Attribute("scale")) |
---|
| 128 | { |
---|
[715] | 129 | std::string scaleStr = xmlElem->Attribute("scale"); |
---|
[622] | 130 | float scale; |
---|
| 131 | String2Number<float>(scale, scaleStr); |
---|
| 132 | this->setScale(scale); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | if (xmlElem->Attribute("rotationAxis")) |
---|
| 136 | { |
---|
[715] | 137 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("rotationAxis"),","); |
---|
[622] | 138 | float x, y, z; |
---|
| 139 | String2Number<float>(x, pos[0]); |
---|
| 140 | String2Number<float>(y, pos[1]); |
---|
| 141 | String2Number<float>(z, pos[2]); |
---|
| 142 | this->setRotationAxis(x, y, z); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if (xmlElem->Attribute("rotationRate")) |
---|
| 146 | { |
---|
| 147 | float rotationRate; |
---|
| 148 | String2Number<float>(rotationRate, xmlElem->Attribute("rotationRate")); |
---|
| 149 | this->setRotationRate(Degree(rotationRate)); |
---|
| 150 | if (rotationRate != 0) |
---|
| 151 | this->setStatic(false); |
---|
| 152 | } |
---|
| 153 | |
---|
[630] | 154 | create(); |
---|
[643] | 155 | |
---|
[583] | 156 | } |
---|
| 157 | |
---|
[630] | 158 | bool WorldEntity::create(){ |
---|
[631] | 159 | registerAllVariables(); |
---|
[630] | 160 | return true; |
---|
| 161 | } |
---|
[643] | 162 | |
---|
[583] | 163 | void WorldEntity::registerAllVariables() |
---|
| 164 | { |
---|
[630] | 165 | // register coordinates |
---|
| 166 | registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA); |
---|
| 167 | registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA); |
---|
| 168 | registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA); |
---|
| 169 | // register orientation |
---|
| 170 | registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA); |
---|
| 171 | registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA); |
---|
| 172 | registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA); |
---|
| 173 | registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA); |
---|
| 174 | // not needed at the moment, because we don't have prediction yet |
---|
| 175 | /*// register velocity_ |
---|
| 176 | registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA); |
---|
| 177 | registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA); |
---|
| 178 | registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA); |
---|
| 179 | // register rotationAxis/rate |
---|
| 180 | registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA); |
---|
| 181 | registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA); |
---|
| 182 | registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA); |
---|
| 183 | registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA);*/ |
---|
[565] | 184 | } |
---|
[497] | 185 | } |
---|