Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/tutorial/src/orxonox/objects/Fighter.cc @ 859

Last change on this file since 859 was 859, checked in by landauf, 16 years ago

more or less a copy of the trunk

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