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: |
---|
22 | * ... |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #include "SpaceShip.h" |
---|
29 | |
---|
30 | #include "../../tinyxml/tinyxml.h" |
---|
31 | #include "../../misc/String2Number.h" |
---|
32 | #include "../core/CoreIncludes.h" |
---|
33 | #include "../Orxonox.h" |
---|
34 | |
---|
35 | #include "OgreCamera.h" |
---|
36 | #include <OgreRenderWindow.h> |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | CreateFactory(SpaceShip); |
---|
41 | |
---|
42 | SpaceShip::SpaceShip() |
---|
43 | { |
---|
44 | RegisterObject(SpaceShip); |
---|
45 | |
---|
46 | SetConfigValue(bInvertMouse_, true); |
---|
47 | |
---|
48 | this->setMouseEventCallback_ = false; |
---|
49 | |
---|
50 | this->moveForward_ = 0; |
---|
51 | this->rotateUp_ = 0; |
---|
52 | this->rotateDown_ = 0; |
---|
53 | this->rotateRight_ = 0; |
---|
54 | this->rotateLeft_ = 0; |
---|
55 | this->loopRight_ = 0; |
---|
56 | this->loopLeft_ = 0; |
---|
57 | this->brakeForward_ = 0; |
---|
58 | this->brakeRotate_ = 0; |
---|
59 | this->brakeLoop_ = 0; |
---|
60 | this->speedForward_ = 0; |
---|
61 | this->speedRotateUpDown_ = 0; |
---|
62 | this->speedRotateRightLeft_ = 0; |
---|
63 | this->speedLoopRightLeft_ = 0; |
---|
64 | this->maxSpeedForward_ = 0; |
---|
65 | this->maxSpeedRotateUpDown_ = 0; |
---|
66 | this->maxSpeedRotateRightLeft_ = 0; |
---|
67 | this->maxSpeedLoopRightLeft_ = 0; |
---|
68 | this->accelerationForward_ = 0; |
---|
69 | this->accelerationRotateUpDown_ = 0; |
---|
70 | this->accelerationRotateRightLeft_ = 0; |
---|
71 | this->accelerationLoopRightLeft_ = 0; |
---|
72 | |
---|
73 | this->speed = 250; |
---|
74 | this->loop = 100; |
---|
75 | this->rotate = 10; |
---|
76 | this->mouseX = 0; |
---|
77 | this->mouseY = 0; |
---|
78 | this->maxMouseX = 0; |
---|
79 | this->minMouseX = 0; |
---|
80 | this->moved = false; |
---|
81 | |
---|
82 | this->brakeRotate(rotate*10); |
---|
83 | this->brakeLoop(loop); |
---|
84 | |
---|
85 | COUT(3) << "Info: SpaceShip was loaded" << std::endl; |
---|
86 | } |
---|
87 | |
---|
88 | SpaceShip::~SpaceShip() |
---|
89 | { |
---|
90 | if (w) |
---|
91 | delete w; |
---|
92 | if (tt) |
---|
93 | delete tt; |
---|
94 | } |
---|
95 | |
---|
96 | void SpaceShip::setMaxSpeedValues(float maxSpeedForward, float maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float maxSpeedLoopRightLeft) |
---|
97 | { |
---|
98 | this->maxSpeedForward_ = maxSpeedForward; |
---|
99 | this->maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
100 | this->maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
101 | this->maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
102 | } |
---|
103 | |
---|
104 | void SpaceShip::loadParams(TiXmlElement* xmlElem) |
---|
105 | { |
---|
106 | Model::loadParams(xmlElem); |
---|
107 | |
---|
108 | w = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"schuss" + this->getName(),"Orxonox/schuss"); |
---|
109 | w->particleSystem_->setParameter("local_space","true"); |
---|
110 | w->newEmitter(); |
---|
111 | /* |
---|
112 | w->setDirection(Vector3(0,0,1)); |
---|
113 | w->setPositionOfEmitter(0, Vector3(10,10,0)); |
---|
114 | w->setPositionOfEmitter(1, Vector3(-10,10,0)); |
---|
115 | */ |
---|
116 | w->setDirection(Vector3(1,0,0)); |
---|
117 | w->setPositionOfEmitter(0, Vector3(0,10,10)); |
---|
118 | w->setPositionOfEmitter(1, Vector3(0,-10,10)); |
---|
119 | |
---|
120 | Ogre::SceneNode* node1 = this->getNode()->createChildSceneNode(this->getName() + "particle1"); |
---|
121 | node1->setInheritScale(false); |
---|
122 | w->addToSceneNode(node1); |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | tt = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow"); |
---|
127 | tt->particleSystem_->setParameter("local_space","true"); |
---|
128 | tt->newEmitter(); |
---|
129 | /* |
---|
130 | tt->setDirection(Vector3(0,0,1)); |
---|
131 | tt->setPositionOfEmitter(0, Vector3(20,-1,-15)); |
---|
132 | tt->setPositionOfEmitter(1, Vector3(-20,-1,-15)); |
---|
133 | */ |
---|
134 | tt->setDirection(Vector3(-1,0,0)); |
---|
135 | tt->setPositionOfEmitter(0, Vector3(-15,20,-1)); |
---|
136 | tt->setPositionOfEmitter(1, Vector3(-15,-20,-1)); |
---|
137 | |
---|
138 | Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2"); |
---|
139 | node2->setInheritScale(false); |
---|
140 | tt->addToSceneNode(node2); |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft")) |
---|
145 | { |
---|
146 | std::string forwardStr = xmlElem->Attribute("forward"); |
---|
147 | std::string rotateupdownStr = xmlElem->Attribute("rotateupdown"); |
---|
148 | std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft"); |
---|
149 | std::string looprightleftStr = xmlElem->Attribute("looprightleft"); |
---|
150 | |
---|
151 | String2Number<float>(this->maxSpeedForward_, forwardStr); |
---|
152 | String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr); |
---|
153 | String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr); |
---|
154 | String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr); |
---|
155 | |
---|
156 | COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl; |
---|
157 | } |
---|
158 | |
---|
159 | if (xmlElem->Attribute("camera")) |
---|
160 | { |
---|
161 | Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam"); |
---|
162 | Ogre::SceneNode *node = this->getNode()->createChildSceneNode("CamNode"); |
---|
163 | /* |
---|
164 | // node->setInheritOrientation(false); |
---|
165 | cam->setPosition(Vector3(0,50,-150)); |
---|
166 | cam->lookAt(Vector3(0,20,0)); |
---|
167 | cam->roll(Degree(0)); |
---|
168 | */ |
---|
169 | |
---|
170 | cam->setPosition(Vector3(-150,0,50)); |
---|
171 | // cam->setPosition(Vector3(0,-350,0)); |
---|
172 | cam->lookAt(Vector3(0,0,20)); |
---|
173 | cam->roll(Degree(-90)); |
---|
174 | |
---|
175 | node->attachObject(cam); |
---|
176 | Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam); |
---|
177 | } |
---|
178 | } |
---|
179 | |
---|
180 | bool SpaceShip::mouseMoved(const OIS::MouseEvent &e) |
---|
181 | { |
---|
182 | this->mouseX += e.state.X.rel; |
---|
183 | if (this->bInvertMouse_) |
---|
184 | this->mouseY += e.state.Y.rel; |
---|
185 | else |
---|
186 | this->mouseY -= e.state.Y.rel; |
---|
187 | |
---|
188 | // if(mouseX>maxMouseX) maxMouseX = mouseX; |
---|
189 | // if(mouseX<minMouseX) minMouseX = mouseX; |
---|
190 | // cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl; |
---|
191 | |
---|
192 | this->moved = true; |
---|
193 | |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | void SpaceShip::tick(float dt) |
---|
198 | { |
---|
199 | if (!this->setMouseEventCallback_) |
---|
200 | { |
---|
201 | if (Orxonox::getSingleton()->getMouse()) |
---|
202 | { |
---|
203 | Orxonox::getSingleton()->getMouse()->setEventCallback(this); |
---|
204 | this->setMouseEventCallback_ = true; |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | WorldEntity::tick(dt); |
---|
209 | |
---|
210 | OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard(); |
---|
211 | OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse(); |
---|
212 | |
---|
213 | mKeyboard->capture(); |
---|
214 | mMouse->capture(); |
---|
215 | |
---|
216 | if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W)) |
---|
217 | this->moveForward(speed); |
---|
218 | else |
---|
219 | this->moveForward(0); |
---|
220 | |
---|
221 | if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S)) |
---|
222 | this->brakeForward(speed); |
---|
223 | else |
---|
224 | this->brakeForward(speed/10); |
---|
225 | |
---|
226 | if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D)) |
---|
227 | this->loopRight(loop); |
---|
228 | else |
---|
229 | this->loopRight(0); |
---|
230 | |
---|
231 | if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A)) |
---|
232 | this->loopLeft(loop); |
---|
233 | else |
---|
234 | this->loopLeft(0); |
---|
235 | |
---|
236 | if(moved) |
---|
237 | { |
---|
238 | if (mouseY<=0) |
---|
239 | this->rotateUp(-mouseY*rotate); |
---|
240 | if (mouseY>0) |
---|
241 | this->rotateDown(mouseY*rotate); |
---|
242 | if (mouseX>0) |
---|
243 | this->rotateRight(mouseX*rotate); |
---|
244 | if (mouseX<=0) |
---|
245 | this->rotateLeft(-mouseX*rotate); |
---|
246 | |
---|
247 | mouseY = 0; |
---|
248 | mouseX = 0; |
---|
249 | moved = false; |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | this->rotateUp(0); |
---|
254 | this->rotateDown(0); |
---|
255 | this->rotateRight(0); |
---|
256 | this->rotateLeft(0); |
---|
257 | } |
---|
258 | |
---|
259 | if(moveForward_ > 0) |
---|
260 | { |
---|
261 | accelerationForward_ = moveForward_; |
---|
262 | if(speedForward_ < maxSpeedForward_) |
---|
263 | speedForward_ += accelerationForward_*dt; |
---|
264 | if(speedForward_ > maxSpeedForward_) |
---|
265 | speedForward_ = maxSpeedForward_; |
---|
266 | } |
---|
267 | |
---|
268 | if(moveForward_ <= 0) |
---|
269 | { |
---|
270 | accelerationForward_ = brakeForward_; |
---|
271 | if(speedForward_ > 0) |
---|
272 | speedForward_ -= accelerationForward_*dt; |
---|
273 | if(speedForward_ < 0) |
---|
274 | speedForward_ = 0; |
---|
275 | } |
---|
276 | |
---|
277 | if(rotateUp_ > 0) |
---|
278 | { |
---|
279 | accelerationRotateUpDown_ = rotateUp_; |
---|
280 | if(speedRotateUpDown_ < maxSpeedRotateUpDown_) |
---|
281 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
282 | if(speedRotateUpDown_ > maxSpeedRotateUpDown_) |
---|
283 | speedRotateUpDown_ = maxSpeedRotateUpDown_; |
---|
284 | } |
---|
285 | |
---|
286 | if(rotateDown_ > 0) |
---|
287 | { |
---|
288 | accelerationRotateUpDown_ = rotateDown_; |
---|
289 | if(speedRotateUpDown_ > -maxSpeedRotateUpDown_) |
---|
290 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
291 | if(speedRotateUpDown_ < -maxSpeedRotateUpDown_) |
---|
292 | speedRotateUpDown_ = -maxSpeedRotateUpDown_; |
---|
293 | } |
---|
294 | |
---|
295 | if(rotateUp_ == 0 && rotateDown_ == 0) |
---|
296 | { |
---|
297 | accelerationRotateUpDown_ = brakeRotate_; |
---|
298 | if(speedRotateUpDown_ > 0) |
---|
299 | speedRotateUpDown_ -= accelerationRotateUpDown_*dt; |
---|
300 | if(speedRotateUpDown_ < 0) |
---|
301 | speedRotateUpDown_ += accelerationRotateUpDown_*dt; |
---|
302 | if(fabs(speedRotateUpDown_) < accelerationRotateUpDown_*dt) |
---|
303 | speedRotateUpDown_ = 0; |
---|
304 | } |
---|
305 | |
---|
306 | if(rotateRight_ > 0) |
---|
307 | { |
---|
308 | accelerationRotateRightLeft_ = rotateRight_; |
---|
309 | if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_) |
---|
310 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
311 | if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_) |
---|
312 | speedRotateRightLeft_ = -maxSpeedRotateRightLeft_; |
---|
313 | } |
---|
314 | |
---|
315 | if(rotateLeft_ > 0) |
---|
316 | { |
---|
317 | accelerationRotateRightLeft_ = rotateLeft_; |
---|
318 | if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_) |
---|
319 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
320 | if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_) |
---|
321 | speedRotateRightLeft_ = maxSpeedRotateRightLeft_; |
---|
322 | } |
---|
323 | |
---|
324 | if(rotateRight_ == 0 && rotateLeft_ == 0) |
---|
325 | { |
---|
326 | accelerationRotateRightLeft_ = brakeRotate_; |
---|
327 | if(speedRotateRightLeft_ > 0) |
---|
328 | speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt; |
---|
329 | if(speedRotateRightLeft_ < 0) |
---|
330 | speedRotateRightLeft_ += accelerationRotateRightLeft_*dt; |
---|
331 | if(fabs(speedRotateRightLeft_) < accelerationRotateRightLeft_*dt) |
---|
332 | speedRotateRightLeft_ = 0; |
---|
333 | } |
---|
334 | |
---|
335 | if(loopRight_ > 0) |
---|
336 | { |
---|
337 | accelerationLoopRightLeft_ = loopRight_; |
---|
338 | if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_) |
---|
339 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
340 | if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_) |
---|
341 | speedLoopRightLeft_ = maxSpeedLoopRightLeft_; |
---|
342 | } |
---|
343 | |
---|
344 | if(loopLeft_ > 0) |
---|
345 | { |
---|
346 | accelerationLoopRightLeft_ = loopLeft_; |
---|
347 | if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_) |
---|
348 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
349 | if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_) |
---|
350 | speedLoopRightLeft_ = -maxSpeedLoopRightLeft_; |
---|
351 | } |
---|
352 | |
---|
353 | if(loopLeft_ == 0 && loopRight_ == 0) |
---|
354 | { |
---|
355 | accelerationLoopRightLeft_ = brakeLoop_; |
---|
356 | if(speedLoopRightLeft_ > 0) |
---|
357 | speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt; |
---|
358 | if(speedLoopRightLeft_ < 0) |
---|
359 | speedLoopRightLeft_ += accelerationLoopRightLeft_*dt; |
---|
360 | if(fabs(speedLoopRightLeft_) < accelerationLoopRightLeft_*dt) |
---|
361 | speedLoopRightLeft_ = 0; |
---|
362 | } |
---|
363 | |
---|
364 | Vector3 transVector = Vector3::ZERO; |
---|
365 | /* |
---|
366 | transVector.z = 1; |
---|
367 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
368 | this->pitch(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
369 | this->yaw(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
370 | this->roll(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
371 | */ |
---|
372 | |
---|
373 | transVector.x = 1; |
---|
374 | this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL); |
---|
375 | this->yaw(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL); |
---|
376 | this->roll(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
377 | this->pitch(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL); |
---|
378 | |
---|
379 | } |
---|
380 | |
---|
381 | void SpaceShip::moveForward(float moveForward) { |
---|
382 | moveForward_ = moveForward; |
---|
383 | } |
---|
384 | |
---|
385 | void SpaceShip::rotateUp(float rotateUp) { |
---|
386 | rotateUp_ = rotateUp; |
---|
387 | } |
---|
388 | |
---|
389 | void SpaceShip::rotateDown(float rotateDown) { |
---|
390 | rotateDown_ = rotateDown; |
---|
391 | } |
---|
392 | |
---|
393 | void SpaceShip::rotateLeft(float rotateLeft) { |
---|
394 | rotateLeft_ = rotateLeft; |
---|
395 | } |
---|
396 | |
---|
397 | void SpaceShip::rotateRight(float rotateRight) { |
---|
398 | rotateRight_ = rotateRight; |
---|
399 | } |
---|
400 | |
---|
401 | void SpaceShip::loopLeft(float loopLeft) { |
---|
402 | loopLeft_ = loopLeft; |
---|
403 | } |
---|
404 | |
---|
405 | void SpaceShip::loopRight(float loopRight) { |
---|
406 | loopRight_ = loopRight; |
---|
407 | } |
---|
408 | |
---|
409 | void SpaceShip::brakeForward(float brakeForward) { |
---|
410 | brakeForward_ = brakeForward; |
---|
411 | } |
---|
412 | |
---|
413 | void SpaceShip::brakeRotate(float brakeRotate) { |
---|
414 | brakeRotate_ = brakeRotate; |
---|
415 | } |
---|
416 | |
---|
417 | void SpaceShip::brakeLoop(float brakeLoop) { |
---|
418 | brakeLoop_ = brakeLoop; |
---|
419 | } |
---|
420 | |
---|
421 | void SpaceShip::maxSpeedForward(float maxSpeedForward) { |
---|
422 | maxSpeedForward_ = maxSpeedForward; |
---|
423 | } |
---|
424 | |
---|
425 | void SpaceShip::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) { |
---|
426 | maxSpeedRotateUpDown_ = maxSpeedRotateUpDown; |
---|
427 | } |
---|
428 | |
---|
429 | void SpaceShip::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) { |
---|
430 | maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft; |
---|
431 | } |
---|
432 | |
---|
433 | void SpaceShip::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) { |
---|
434 | maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft; |
---|
435 | } |
---|
436 | } |
---|