Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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