Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/SpaceShip.cc @ 646

Last change on this file since 646 was 646, checked in by landauf, 16 years ago
  • added very bad collision detection (presentation hack :D)
  • added explosions
  • fixed bug in ParticleInterface (it tried to delete SceneManager)

AND:

  • fixed one of the most amazing bugs ever! (the game crashed when I deleted an object through a timer-function. because the timer-functions is called by an iterator, the iterator indirectly delted its object. by overloading the (it++) operator, i was able to solve this problem)
File size: 17.2 KB
Line 
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#include "Projectile.h"
30
31#include "../../tinyxml/tinyxml.h"
32#include "../../misc/String2Number.h"
33#include "../core/CoreIncludes.h"
34#include "../Orxonox.h"
35
36#include "OgreCamera.h"
37#include <OgreRenderWindow.h>
38
39namespace orxonox
40{
41    CreateFactory(SpaceShip);
42
43    SpaceShip::SpaceShip()
44    {
45        RegisterObject(SpaceShip);
46
47        SetConfigValue(bInvertMouse_, true);
48        SetConfigValue(reloadTime_, 0.125);
49
50        this->setMouseEventCallback_ = false;
51        this->bLMousePressed_ = false;
52        this->bRMousePressed_ = false;
53
54        this->camNode_ = 0;
55
56        this->tt_ = 0;
57        this->redNode_ = 0;
58        this->greenNode_ = 0;
59        this->blinkTime_ = 0;
60
61        this->timeToReload_ = 0;
62
63        this->moveForward_ = 0;
64        this->rotateUp_ = 0;
65        this->rotateDown_ = 0;
66        this->rotateRight_ = 0;
67        this->rotateLeft_ = 0;
68        this->loopRight_ = 0;
69        this->loopLeft_ = 0;
70        this->brakeForward_ = 0;
71        this->brakeRotate_ = 0;
72        this->brakeLoop_ = 0;
73        this->speedForward_ = 0;
74        this->speedRotateUpDown_ = 0;
75        this->speedRotateRightLeft_ = 0;
76        this->speedLoopRightLeft_ = 0;
77        this->maxSpeedForward_ = 0;
78        this->maxSpeedRotateUpDown_ = 0;
79        this->maxSpeedRotateRightLeft_ = 0;
80        this->maxSpeedLoopRightLeft_ = 0;
81        this->accelerationForward_ = 0;
82        this->accelerationRotateUpDown_ = 0;
83        this->accelerationRotateRightLeft_ = 0;
84        this->accelerationLoopRightLeft_ = 0;
85
86        this->speed = 250;
87        this->loop = 100;
88        this->rotate = 10;
89        this->mouseX = 0;
90        this->mouseY = 0;
91        this->maxMouseX = 0;
92        this->minMouseX = 0;
93        this->moved = false;
94
95        this->brakeRotate(rotate*10);
96        this->brakeLoop(loop);
97
98        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
99    }
100
101    SpaceShip::~SpaceShip()
102    {
103        if (tt_)
104            delete tt_;
105    }
106
107    void SpaceShip::setMaxSpeedValues(float maxSpeedForward, float maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float maxSpeedLoopRightLeft)
108    {
109        this->maxSpeedForward_ = maxSpeedForward;
110        this->maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
111        this->maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
112        this->maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
113    }
114
115    void SpaceShip::loadParams(TiXmlElement* xmlElem)
116    {
117        Model::loadParams(xmlElem);
118
119
120        // START CREATING THRUSTER
121        tt_ = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
122        tt_->getParticleSystem()->setParameter("local_space","true");
123        tt_->newEmitter();
124/*
125        tt_->setDirection(Vector3(0,0,1));
126        tt_->setPositionOfEmitter(0, Vector3(20,-1,-15));
127        tt_->setPositionOfEmitter(1, Vector3(-20,-1,-15));
128*/
129        tt_->setDirection(Vector3(-1,0,0));
130        tt_->setPositionOfEmitter(0, Vector3(-15,20,-1));
131        tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1));
132        tt_->setVelocity(50);
133
134        emitterRate_ = tt_->getRate();
135
136        Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2");
137        node2->setInheritScale(false);
138        tt_->addToSceneNode(node2);
139        // END CREATING THRUSTER
140
141        // START CREATING BLINKING LIGHTS
142        this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
143        this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1);
144
145        this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.7, -0.3));
146        this->redNode_->setInheritScale(false);
147        this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.7, -0.3));
148        this->greenNode_->setInheritScale(false);
149
150        this->redNode_->attachObject(this->redBillboard_.getBillboardSet());
151        this->redNode_->setScale(0.3, 0.3, 0.3);
152
153        this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet());
154        this->greenNode_->setScale(0.3, 0.3, 0.3);
155        // END CREATING BLINKING LIGHTS
156
157
158        if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft"))
159        {
160            std::string forwardStr = xmlElem->Attribute("forward");
161            std::string rotateupdownStr = xmlElem->Attribute("rotateupdown");
162            std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft");
163            std::string looprightleftStr = xmlElem->Attribute("looprightleft");
164
165            String2Number<float>(this->maxSpeedForward_, forwardStr);
166            String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr);
167            String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr);
168            String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr);
169
170            COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl;
171        }
172
173        if (xmlElem->Attribute("camera"))
174        {
175            Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam");
176            this->camNode_ = this->getNode()->createChildSceneNode("CamNode");
177/*
178//            node->setInheritOrientation(false);
179            cam->setPosition(Vector3(0,50,-150));
180            cam->lookAt(Vector3(0,20,0));
181            cam->roll(Degree(0));
182*/
183
184            cam->setPosition(Vector3(-150,0,50));
185//            cam->setPosition(Vector3(0,-350,0));
186            cam->lookAt(Vector3(0,0,20));
187            cam->roll(Degree(-90));
188
189            this->camNode_->attachObject(cam);
190            Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam);
191        }
192    }
193
194    bool SpaceShip::mouseMoved(const OIS::MouseEvent &e)
195    {
196        this->mouseX += e.state.X.rel;
197        if (this->bInvertMouse_)
198            this->mouseY += e.state.Y.rel;
199        else
200            this->mouseY -= e.state.Y.rel;
201
202//        if(mouseX>maxMouseX) maxMouseX = mouseX;
203//        if(mouseX<minMouseX) minMouseX = mouseX;
204//        cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
205
206        this->moved = true;
207
208        if (this->bRMousePressed_)
209        {
210            this->camNode_->roll(Degree(-e.state.X.rel * 0.10));
211            this->camNode_->yaw(Degree(e.state.Y.rel * 0.10));
212        }
213
214        return true;
215    }
216
217    bool SpaceShip::mousePressed(const OIS::MouseEvent &e, OIS::MouseButtonID id)
218    {
219        if (id == OIS::MB_Left)
220            this->bLMousePressed_ = true;
221        else if (id == OIS::MB_Right)
222            this->bRMousePressed_ = true;
223
224        return true;
225    }
226
227    bool SpaceShip::mouseReleased(const OIS::MouseEvent &e, OIS::MouseButtonID id)
228    {
229        if (id == OIS::MB_Left)
230            this->bLMousePressed_ = false;
231        else if (id == OIS::MB_Right)
232        {
233            this->bRMousePressed_ = false;
234            this->camNode_->resetOrientation();
235        }
236
237        return true;
238    }
239
240    void SpaceShip::tick(float dt)
241    {
242        if (!this->setMouseEventCallback_)
243        {
244            if (Orxonox::getSingleton()->getMouse())
245            {
246                Orxonox::getSingleton()->getMouse()->setEventCallback(this);
247                this->setMouseEventCallback_ = true;
248            }
249        }
250
251        WorldEntity::tick(dt);
252
253        if (this->redNode_ && this->greenNode_)
254        {
255            this->blinkTime_ += dt;
256            float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0);
257            float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0);
258            this->redNode_->setScale(redScale, redScale, redScale);
259            this->greenNode_->setScale(greenScale, greenScale, greenScale);
260        }
261
262        if (this->timeToReload_ > 0)
263            this->timeToReload_ -= dt;
264        else
265            this->timeToReload_ = 0;
266
267        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
268        {
269            Projectile* proj = new Projectile(this);
270            this->timeToReload_ = this->reloadTime_;
271        }
272
273        OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard();
274        OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse();
275
276        mKeyboard->capture();
277        mMouse->capture();
278
279        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
280            this->moveForward(speed);
281        else
282            this->moveForward(0);
283
284        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
285            this->brakeForward(speed);
286        else
287            this->brakeForward(speed/10);
288
289        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
290            this->loopRight(loop);
291        else
292            this->loopRight(0);
293
294        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
295            this->loopLeft(loop);
296        else
297            this->loopLeft(0);
298
299        if(moved)
300        {
301            if (mouseY<=0)
302                this->rotateUp(-mouseY*rotate);
303            if (mouseY>0)
304                this->rotateDown(mouseY*rotate);
305            if (mouseX>0)
306                this->rotateRight(mouseX*rotate);
307            if (mouseX<=0)
308                this->rotateLeft(-mouseX*rotate);
309
310            mouseY = 0;
311            mouseX = 0;
312            moved = false;
313        }
314        else
315        {
316            this->rotateUp(0);
317            this->rotateDown(0);
318            this->rotateRight(0);
319            this->rotateLeft(0);
320        }
321
322        if(moveForward_ > 0)
323        {
324            accelerationForward_ = moveForward_;
325            if(speedForward_ < maxSpeedForward_)
326                speedForward_ += accelerationForward_*dt;
327            if(speedForward_ > maxSpeedForward_)
328                speedForward_ = maxSpeedForward_;
329        }
330
331        if(moveForward_ <= 0)
332        {
333            accelerationForward_ = -brakeForward_;
334            if(speedForward_ > 0)
335                speedForward_ += accelerationForward_*dt;
336            if(speedForward_ < 0)
337                speedForward_ = 0;
338        }
339
340        if(rotateUp_ > 0)
341        {
342            accelerationRotateUpDown_ = rotateUp_;
343            if(speedRotateUpDown_ < maxSpeedRotateUpDown_)
344                speedRotateUpDown_ += accelerationRotateUpDown_*dt;
345            if(speedRotateUpDown_ > maxSpeedRotateUpDown_)
346            speedRotateUpDown_ = maxSpeedRotateUpDown_;
347        }
348
349        if(rotateDown_ > 0)
350        {
351            accelerationRotateUpDown_ = rotateDown_;
352            if(speedRotateUpDown_ > -maxSpeedRotateUpDown_)
353                speedRotateUpDown_ -= accelerationRotateUpDown_*dt;
354            if(speedRotateUpDown_ < -maxSpeedRotateUpDown_)
355                speedRotateUpDown_ = -maxSpeedRotateUpDown_;
356        }
357
358        if(rotateUp_ == 0 && rotateDown_ == 0)
359        {
360            accelerationRotateUpDown_ = brakeRotate_;
361            if(speedRotateUpDown_ > 0)
362                speedRotateUpDown_ -= accelerationRotateUpDown_*dt;
363            if(speedRotateUpDown_ < 0)
364                speedRotateUpDown_ += accelerationRotateUpDown_*dt;
365            if(fabs(speedRotateUpDown_) < accelerationRotateUpDown_*dt)
366                speedRotateUpDown_ = 0;
367        }
368
369        if(rotateRight_ > 0)
370        {
371            accelerationRotateRightLeft_ = rotateRight_;
372            if(speedRotateRightLeft_ > -maxSpeedRotateRightLeft_)
373                speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt;
374            if(speedRotateRightLeft_ < -maxSpeedRotateRightLeft_)
375                speedRotateRightLeft_ = -maxSpeedRotateRightLeft_;
376        }
377
378        if(rotateLeft_ > 0)
379        {
380            accelerationRotateRightLeft_ = rotateLeft_;
381            if(speedRotateRightLeft_ < maxSpeedRotateRightLeft_)
382                speedRotateRightLeft_ += accelerationRotateRightLeft_*dt;
383            if(speedRotateRightLeft_ > maxSpeedRotateRightLeft_)
384                speedRotateRightLeft_ = maxSpeedRotateRightLeft_;
385        }
386
387        if(rotateRight_ == 0 && rotateLeft_ == 0)
388        {
389            accelerationRotateRightLeft_ = brakeRotate_;
390            if(speedRotateRightLeft_ > 0)
391                speedRotateRightLeft_ -= accelerationRotateRightLeft_*dt;
392            if(speedRotateRightLeft_ < 0)
393                speedRotateRightLeft_ += accelerationRotateRightLeft_*dt;
394            if(fabs(speedRotateRightLeft_) < accelerationRotateRightLeft_*dt)
395                speedRotateRightLeft_ = 0;
396        }
397
398        if(loopRight_ > 0)
399        {
400            accelerationLoopRightLeft_ = loopRight_;
401            if(speedLoopRightLeft_ < maxSpeedLoopRightLeft_)
402                speedLoopRightLeft_ += accelerationLoopRightLeft_*dt;
403            if(speedLoopRightLeft_ > maxSpeedLoopRightLeft_)
404                speedLoopRightLeft_ = maxSpeedLoopRightLeft_;
405        }
406
407        if(loopLeft_ > 0)
408        {
409            accelerationLoopRightLeft_ = loopLeft_;
410            if(speedLoopRightLeft_ > -maxSpeedLoopRightLeft_)
411                speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt;
412            if(speedLoopRightLeft_ < -maxSpeedLoopRightLeft_)
413                speedLoopRightLeft_ = -maxSpeedLoopRightLeft_;
414        }
415
416        if(loopLeft_ == 0 && loopRight_ == 0)
417        {
418            accelerationLoopRightLeft_ = brakeLoop_;
419            if(speedLoopRightLeft_ > 0)
420                speedLoopRightLeft_ -= accelerationLoopRightLeft_*dt;
421            if(speedLoopRightLeft_ < 0)
422                speedLoopRightLeft_ += accelerationLoopRightLeft_*dt;
423            if(fabs(speedLoopRightLeft_) < accelerationLoopRightLeft_*dt)
424                speedLoopRightLeft_ = 0;
425        }
426
427        Vector3 transVector = Vector3::ZERO;
428/*
429        transVector.z = 1;
430        this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL);
431        this->pitch(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL);
432        this->yaw(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL);
433        this->roll(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL);
434*/
435
436        transVector.x = 1;
437        this->translate(transVector*speedForward_*dt, Ogre::Node::TS_LOCAL);
438        this->yaw(Degree(speedRotateUpDown_*dt), Ogre::Node::TS_LOCAL);
439        this->roll(Degree(speedRotateRightLeft_*dt), Ogre::Node::TS_LOCAL);
440        this->pitch(Degree(speedLoopRightLeft_*dt), Ogre::Node::TS_LOCAL);
441
442        if (accelerationForward_ > 25.0)
443        {
444            this->tt_->setRate(emitterRate_);
445        }
446        else
447        {
448            this->tt_->setRate(0);
449        }
450
451    }
452
453    void SpaceShip::moveForward(float moveForward) {
454        moveForward_ = moveForward;
455    }
456
457    void SpaceShip::rotateUp(float rotateUp) {
458        rotateUp_ = rotateUp;
459    }
460
461    void SpaceShip::rotateDown(float rotateDown) {
462        rotateDown_ = rotateDown;
463    }
464
465    void SpaceShip::rotateLeft(float rotateLeft) {
466        rotateLeft_ = rotateLeft;
467    }
468
469    void SpaceShip::rotateRight(float rotateRight) {
470        rotateRight_ = rotateRight;
471    }
472
473    void SpaceShip::loopLeft(float loopLeft) {
474        loopLeft_ = loopLeft;
475    }
476
477    void SpaceShip::loopRight(float loopRight) {
478        loopRight_ = loopRight;
479    }
480
481    void SpaceShip::brakeForward(float brakeForward) {
482        brakeForward_ = brakeForward;
483    }
484
485    void SpaceShip::brakeRotate(float brakeRotate) {
486        brakeRotate_ = brakeRotate;
487    }
488
489    void SpaceShip::brakeLoop(float brakeLoop) {
490        brakeLoop_ = brakeLoop;
491    }
492
493    void SpaceShip::maxSpeedForward(float maxSpeedForward) {
494        maxSpeedForward_ = maxSpeedForward;
495    }
496
497    void SpaceShip::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) {
498        maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
499    }
500
501    void SpaceShip::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) {
502        maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
503    }
504
505    void SpaceShip::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) {
506        maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
507    }
508}
Note: See TracBrowser for help on using the repository browser.