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 | |
---|
41 | namespace orxonox |
---|
42 | { |
---|
43 | CreateFactory(WorldEntity); |
---|
44 | |
---|
45 | unsigned int WorldEntity::worldEntityCounter_s = 0; |
---|
46 | |
---|
47 | WorldEntity::WorldEntity() |
---|
48 | { |
---|
49 | this->bStatic_ = true; |
---|
50 | this->velocity_ = Vector3(0, 0, 0); |
---|
51 | this->acceleration_ = Vector3(0, 0, 0); |
---|
52 | this->rotationAxis_ = Vector3(0, 1, 0); |
---|
53 | this->rotationRate_ = 0; |
---|
54 | this->momentum_ = 0; |
---|
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 | else |
---|
68 | { |
---|
69 | this->node_ = 0; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | WorldEntity::~WorldEntity() |
---|
74 | { |
---|
75 | } |
---|
76 | |
---|
77 | void WorldEntity::tick(float dt) |
---|
78 | { |
---|
79 | if (!this->bStatic_) |
---|
80 | { |
---|
81 | this->velocity_ += (dt * this->acceleration_); |
---|
82 | this->translate(dt * this->velocity_, Ogre::Node::TS_LOCAL); |
---|
83 | |
---|
84 | this->rotationRate_ += (dt * this->momentum_); |
---|
85 | this->rotate(this->rotationAxis_, dt * this->rotationRate_); |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | void WorldEntity::loadParams(TiXmlElement* xmlElem) |
---|
90 | { |
---|
91 | |
---|
92 | BaseObject::loadParams(xmlElem); |
---|
93 | create(); |
---|
94 | /* |
---|
95 | if (xmlElem->Attribute("position")) |
---|
96 | { |
---|
97 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("position"),","); |
---|
98 | float x, y, z; |
---|
99 | String2Number<float>(x, pos[0]); |
---|
100 | String2Number<float>(y, pos[1]); |
---|
101 | String2Number<float>(z, pos[2]); |
---|
102 | this->setPosition(x, y, z); |
---|
103 | } |
---|
104 | |
---|
105 | if (xmlElem->Attribute("direction")) |
---|
106 | { |
---|
107 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("direction"),","); |
---|
108 | float x, y, z; |
---|
109 | String2Number<float>(x, pos[0]); |
---|
110 | String2Number<float>(y, pos[1]); |
---|
111 | String2Number<float>(z, pos[2]); |
---|
112 | this->setDirection(x, y, z); |
---|
113 | } |
---|
114 | |
---|
115 | if (xmlElem->Attribute("yaw") || xmlElem->Attribute("pitch") || xmlElem->Attribute("roll")) |
---|
116 | { |
---|
117 | float yaw = 0.0, pitch = 0.0, roll = 0.0; |
---|
118 | if (xmlElem->Attribute("yaw")) |
---|
119 | String2Number<float>(yaw,xmlElem->Attribute("yaw")); |
---|
120 | |
---|
121 | if (xmlElem->Attribute("pitch")) |
---|
122 | String2Number<float>(pitch,xmlElem->Attribute("pitch")); |
---|
123 | |
---|
124 | if (xmlElem->Attribute("roll")) |
---|
125 | String2Number<float>(roll,xmlElem->Attribute("roll")); |
---|
126 | |
---|
127 | this->yaw(Degree(yaw)); |
---|
128 | this->pitch(Degree(pitch)); |
---|
129 | this->roll(Degree(roll)); |
---|
130 | } |
---|
131 | |
---|
132 | if (xmlElem->Attribute("scale")) |
---|
133 | { |
---|
134 | std::string scaleStr = xmlElem->Attribute("scale"); |
---|
135 | float scale; |
---|
136 | String2Number<float>(scale, scaleStr); |
---|
137 | this->setScale(scale); |
---|
138 | } |
---|
139 | |
---|
140 | if (xmlElem->Attribute("rotationAxis")) |
---|
141 | { |
---|
142 | std::vector<std::string> pos = tokenize(xmlElem->Attribute("rotationAxis"),","); |
---|
143 | float x, y, z; |
---|
144 | String2Number<float>(x, pos[0]); |
---|
145 | String2Number<float>(y, pos[1]); |
---|
146 | String2Number<float>(z, pos[2]); |
---|
147 | this->setRotationAxis(x, y, z); |
---|
148 | } |
---|
149 | |
---|
150 | if (xmlElem->Attribute("rotationRate")) |
---|
151 | { |
---|
152 | float rotationRate; |
---|
153 | String2Number<float>(rotationRate, xmlElem->Attribute("rotationRate")); |
---|
154 | this->setRotationRate(Degree(rotationRate)); |
---|
155 | if (rotationRate != 0) |
---|
156 | this->setStatic(false); |
---|
157 | } |
---|
158 | |
---|
159 | create(); |
---|
160 | */ |
---|
161 | } |
---|
162 | |
---|
163 | void WorldEntity::setYawPitchRoll(const Degree& yaw, const Degree& pitch, const Degree& roll) |
---|
164 | { |
---|
165 | this->yaw(yaw); |
---|
166 | this->pitch(pitch); |
---|
167 | this->roll(roll); |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | @brief XML loading and saving. |
---|
172 | @param xmlelement The XML-element |
---|
173 | @param loading Loading (true) or saving (false) |
---|
174 | @return The XML-element |
---|
175 | */ |
---|
176 | void WorldEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
177 | { |
---|
178 | BaseObject::XMLPort(xmlelement, mode); |
---|
179 | |
---|
180 | XMLPortParam(WorldEntity, "position", setPositionLoader2, getPosition, xmlelement, mode); |
---|
181 | XMLPortParamLoadOnly(WorldEntity, "direction", setDirectionLoader, xmlelement, mode); |
---|
182 | XMLPortParamLoadOnly(WorldEntity, "yawpitchroll", setYawPitchRoll, xmlelement, mode); |
---|
183 | XMLPortParam(WorldEntity, "scale", setTotalScale, getScale, xmlelement, mode); |
---|
184 | XMLPortParam(WorldEntity, "rotationAxis", setRotationAxisLoader, getRotationAxis, xmlelement, mode); |
---|
185 | XMLPortParam(WorldEntity, "rotationRate", setRotationRate, getRotationRate, xmlelement, mode); |
---|
186 | |
---|
187 | XMLPortObject(WorldEntity, WorldEntity, "attached", attachWorldEntity, getAttachedWorldEntity, xmlelement, mode, false, true); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | void WorldEntity::registerAllVariables() |
---|
192 | { |
---|
193 | // register coordinates |
---|
194 | registerVar( (void*) &(this->getPosition().x), sizeof(this->getPosition().x), network::DATA); |
---|
195 | registerVar( (void*) &(this->getPosition().y), sizeof(this->getPosition().y), network::DATA); |
---|
196 | registerVar( (void*) &(this->getPosition().z), sizeof(this->getPosition().z), network::DATA); |
---|
197 | // register orientation |
---|
198 | registerVar( (void*) &(this->getOrientation().w), sizeof(this->getOrientation().w), network::DATA); |
---|
199 | registerVar( (void*) &(this->getOrientation().x), sizeof(this->getOrientation().x), network::DATA); |
---|
200 | registerVar( (void*) &(this->getOrientation().y), sizeof(this->getOrientation().y), network::DATA); |
---|
201 | registerVar( (void*) &(this->getOrientation().z), sizeof(this->getOrientation().z), network::DATA); |
---|
202 | // not needed at the moment, because we don't have prediction yet |
---|
203 | // register velocity_ |
---|
204 | registerVar( (void*) &(this->getVelocity().x), sizeof(this->getVelocity().x), network::DATA); |
---|
205 | registerVar( (void*) &(this->getVelocity().y), sizeof(this->getVelocity().y), network::DATA); |
---|
206 | registerVar( (void*) &(this->getVelocity().z), sizeof(this->getVelocity().z), network::DATA); |
---|
207 | // register rotationAxis/rate |
---|
208 | registerVar( (void*) &(this->getRotationRate()), sizeof(this->getRotationRate()), network::DATA); |
---|
209 | registerVar( (void*) &(this->getRotationAxis().x), sizeof(this->getRotationAxis().x), network::DATA); |
---|
210 | registerVar( (void*) &(this->getRotationAxis().y), sizeof(this->getRotationAxis().y), network::DATA); |
---|
211 | registerVar( (void*) &(this->getRotationAxis().z), sizeof(this->getRotationAxis().z), network::DATA); |
---|
212 | } |
---|
213 | |
---|
214 | void WorldEntity::attachWorldEntity(WorldEntity* entity) |
---|
215 | { |
---|
216 | this->attachedWorldEntities_.push_back(entity); |
---|
217 | } |
---|
218 | |
---|
219 | const WorldEntity* WorldEntity::getAttachedWorldEntity(unsigned int index) const |
---|
220 | { |
---|
221 | if (index < this->attachedWorldEntities_.size()) |
---|
222 | return this->attachedWorldEntities_[index]; |
---|
223 | else |
---|
224 | return 0; |
---|
225 | } |
---|
226 | } |
---|