Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/FICN/src/orxonox/objects/Fighter.cc @ 715

Last change on this file since 715 was 715, checked in by rgrieder, 16 years ago
  • the master has spoken…
  • misc/String.h is not anymore..
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 <string>
29
30#include <OgreCamera.h>
31#include <OgreRenderWindow.h>
32#include <OgreSceneManager.h>
33#include <OgreSceneNode.h>
34
35#include "tinyxml/tinyxml.h"
36#include "misc/String2Number.h"
37#include "../core/CoreIncludes.h"
38#include "../Orxonox.h"
39#include "../particle/ParticleInterface.h"
40#include "weapon_system/AmmunitionDump.h"
41#include "weapon_system/BarrelGun.h"
42
43#include "Fighter.h"
44
45namespace orxonox
46{
47    CreateFactory(Fighter);
48
49    Fighter::Fighter()
50    {
51        RegisterObject(Fighter);
52
53        this->setConfigValues();
54
55        this->setMouseEventCallback_ = false;
56
57        this->w = NULL;
58        this->tt = NULL;
59        this->ammoDump_ = NULL;
60        this->mainWeapon_ = NULL;
61        this->rightButtonPressed_ = false;
62        this->leftButtonPressed_ = false;
63
64        this->moveForward_ = 0;
65        this->rotateUp_ = 0;
66        this->rotateDown_ = 0;
67        this->rotateRight_ = 0;
68        this->rotateLeft_ = 0;
69        this->loopRight_ = 0;
70        this->loopLeft_ = 0;
71        this->brakeForward_ = 0;
72        this->brakeRotate_ = 0;
73        this->brakeLoop_ = 0;
74        this->speedForward_ = 0;
75        this->speedRotateUpDown_ = 0;
76        this->speedRotateRightLeft_ = 0;
77        this->speedLoopRightLeft_ = 0;
78        this->maxSpeedForward_ = 0;
79        this->maxSpeedRotateUpDown_ = 0;
80        this->maxSpeedRotateRightLeft_ = 0;
81        this->maxSpeedLoopRightLeft_ = 0;
82        this->accelerationForward_ = 0;
83        this->accelerationRotateUpDown_ = 0;
84        this->accelerationRotateRightLeft_ = 0;
85        this->accelerationLoopRightLeft_ = 0;
86
87        this->speed = 250;
88        this->loop = 100;
89        this->rotate = 10;
90        this->mouseX = 0;
91        this->mouseY = 0;
92        this->maxMouseX = 0;
93        this->minMouseX = 0;
94        this->moved = false;
95
96        this->brakeRotate(rotate*10);
97        this->brakeLoop(loop);
98
99        COUT(3) << "Info: Fighter was loaded" << std::endl;
100    }
101
102    Fighter::~Fighter()
103    {
104        if (w)
105            delete w;
106        if (tt)
107            delete tt;
108    }
109
110    void Fighter::setConfigValues()
111    {
112        SetConfigValue(bInvertMouse_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
113    }
114
115    void Fighter::setMaxSpeedValues(float maxSpeedForward, float maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float maxSpeedLoopRightLeft)
116    {
117        this->maxSpeedForward_ = maxSpeedForward;
118        this->maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
119        this->maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
120        this->maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
121    }
122
123    void Fighter::loadParams(TiXmlElement* xmlElem)
124    {
125        Model::loadParams(xmlElem);
126
127#if 0
128        w = new particle::ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"schuss" + this->getName(),"Orxonox/schuss");
129        w->getParticleSystem()->setParameter("local_space","true");
130        w->newEmitter();
131/*
132        w->setDirection(Vector3(0,0,1));
133        w->setPositionOfEmitter(0, Vector3(10,10,0));
134        w->setPositionOfEmitter(1, Vector3(-10,10,0));
135*/
136        w->setDirection(Vector3(1,0,0));
137        w->setPositionOfEmitter(0, Vector3(0,10,10));
138        w->setPositionOfEmitter(1, Vector3(0,-10,10));
139
140        emitterRate_ = w->getRate();
141
142        Ogre::SceneNode* node1 = this->getNode()->createChildSceneNode(this->getName() + "particle1");
143        node1->setInheritScale(false);
144        w->addToSceneNode(node1);
145#endif
146
147        tt = new ParticleInterface(Orxonox::getSingleton()->getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
148        tt->getParticleSystem()->setParameter("local_space","true");
149        tt->newEmitter();
150/*
151        tt->setDirection(Vector3(0,0,1));
152        tt->setPositionOfEmitter(0, Vector3(20,-1,-15));
153        tt->setPositionOfEmitter(1, Vector3(-20,-1,-15));
154*/
155        tt->setDirection(Vector3(-1,0,0));
156        tt->setPositionOfEmitter(0, Vector3(-15,20,-1));
157        tt->setPositionOfEmitter(1, Vector3(-15,-20,-1));
158        tt->setVelocity(50);
159
160        Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2");
161        node2->setInheritScale(false);
162        tt->addToSceneNode(node2);
163
164        // add weapon
165
166        ammoDump_ = new AmmunitionDump();
167        ammoDump_->setDumpSize("Barrel", 1000);
168        ammoDump_->store("Barrel", 420);
169
170        mainWeapon_ = new BarrelGun();
171        mainWeapon_->setAmmoDump(ammoDump_);
172        Orxonox::getSingleton()->getSceneManager()->getRootSceneNode()->removeChild(mainWeapon_->getNode());
173        getNode()->addChild(mainWeapon_->getNode());
174
175        if (xmlElem->Attribute("forward") && xmlElem->Attribute("rotateupdown") && xmlElem->Attribute("rotaterightleft") && xmlElem->Attribute("looprightleft"))
176        {
177            std::string forwardStr = xmlElem->Attribute("forward");
178            std::string rotateupdownStr = xmlElem->Attribute("rotateupdown");
179            std::string rotaterightleftStr = xmlElem->Attribute("rotaterightleft");
180            std::string looprightleftStr = xmlElem->Attribute("looprightleft");
181
182            String2Number<float>(this->maxSpeedForward_, forwardStr);
183            String2Number<float>(this->maxSpeedRotateUpDown_, rotateupdownStr);
184            String2Number<float>(this->maxSpeedRotateRightLeft_, rotaterightleftStr);
185            String2Number<float>(this->maxSpeedLoopRightLeft_, looprightleftStr);
186
187            COUT(4) << "Loader: Initialized spaceship steering with values " << maxSpeedForward_ << " " << maxSpeedRotateUpDown_ << " " << maxSpeedRotateRightLeft_ << " " << maxSpeedLoopRightLeft_ << " " << std::endl;
188        }
189
190        if (xmlElem->Attribute("camera"))
191        {
192            Ogre::Camera *cam = Orxonox::getSingleton()->getSceneManager()->createCamera("ShipCam");
193            Ogre::SceneNode *node = this->getNode()->createChildSceneNode("CamNode");
194/*
195//            node->setInheritOrientation(false);
196            cam->setPosition(Vector3(0,50,-150));
197            cam->lookAt(Vector3(0,20,0));
198            cam->roll(Degree(0));
199*/
200
201            cam->setPosition(Vector3(-150,0,50));
202//            cam->setPosition(Vector3(0,-350,0));
203            cam->lookAt(Vector3(0,0,20));
204            cam->roll(Degree(-90));
205
206            node->attachObject(cam);
207            Orxonox::getSingleton()->getOgrePointer()->getRoot()->getAutoCreatedWindow()->addViewport(cam);
208        }
209    }
210
211    bool Fighter::mouseMoved(const OIS::MouseEvent &e)
212    {
213        this->mouseX += e.state.X.rel;
214        //if (this->bInvertMouse_)
215            //this->mouseY += e.state.Y.rel;
216        //else
217            this->mouseY -= e.state.Y.rel;
218
219//        if(mouseX>maxMouseX) maxMouseX = mouseX;
220//        if(mouseX<minMouseX) minMouseX = mouseX;
221//        cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
222
223        this->moved = true;
224
225        return true;
226    }
227
228    bool Fighter::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id )
229    {
230      if (id == OIS::MB_Left)
231      {
232        this->leftButtonPressed_ = true;
233      }
234      else if (id == OIS::MB_Right)
235        this->rightButtonPressed_ = true;
236      return true;
237    }
238
239    bool Fighter::mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id )
240    {
241      if (id == OIS::MB_Left)
242      {
243        this->leftButtonPressed_ = false;
244      }
245      else if (id == OIS::MB_Right)
246        this->rightButtonPressed_ = false;
247      return true;
248    }
249
250    void Fighter::tick(float dt)
251    {
252        if (!this->setMouseEventCallback_)
253        {
254            if (Orxonox::getSingleton()->getMouse())
255            {
256                Orxonox::getSingleton()->getMouse()->setEventCallback(this);
257                this->setMouseEventCallback_ = true;
258            }
259        }
260
261        WorldEntity::tick(dt);
262
263        OIS::Keyboard* mKeyboard = Orxonox::getSingleton()->getKeyboard();
264        OIS::Mouse* mMouse = Orxonox::getSingleton()->getMouse();
265
266        mKeyboard->capture();
267        mMouse->capture();
268
269        if (leftButtonPressed_)
270            mainWeapon_->primaryFireRequest();
271        if (rightButtonPressed_)
272            mainWeapon_->secondaryFireRequest();
273
274        if (mKeyboard->isKeyDown(OIS::KC_UP) || mKeyboard->isKeyDown(OIS::KC_W))
275            this->moveForward(speed);
276        else
277            this->moveForward(0);
278
279        if(mKeyboard->isKeyDown(OIS::KC_DOWN) || mKeyboard->isKeyDown(OIS::KC_S))
280            this->brakeForward(speed);
281        else
282            this->brakeForward(speed/10);
283
284        if (mKeyboard->isKeyDown(OIS::KC_RIGHT) || mKeyboard->isKeyDown(OIS::KC_D))
285            this->loopRight(loop);
286        else
287            this->loopRight(0);
288
289        if (mKeyboard->isKeyDown(OIS::KC_LEFT) || mKeyboard->isKeyDown(OIS::KC_A))
290            this->loopLeft(loop);
291        else
292            this->loopLeft(0);
293
294        if (mKeyboard->isKeyDown(OIS::KC_G))
295            this->mainWeapon_->addAction(BaseWeapon::RELOAD);
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 Fighter::moveForward(float moveForward) {
454        moveForward_ = moveForward;
455    }
456
457    void Fighter::rotateUp(float rotateUp) {
458        rotateUp_ = rotateUp;
459    }
460
461    void Fighter::rotateDown(float rotateDown) {
462        rotateDown_ = rotateDown;
463    }
464
465    void Fighter::rotateLeft(float rotateLeft) {
466        rotateLeft_ = rotateLeft;
467    }
468
469    void Fighter::rotateRight(float rotateRight) {
470        rotateRight_ = rotateRight;
471    }
472
473    void Fighter::loopLeft(float loopLeft) {
474        loopLeft_ = loopLeft;
475    }
476
477    void Fighter::loopRight(float loopRight) {
478        loopRight_ = loopRight;
479    }
480
481    void Fighter::brakeForward(float brakeForward) {
482        brakeForward_ = brakeForward;
483    }
484
485    void Fighter::brakeRotate(float brakeRotate) {
486        brakeRotate_ = brakeRotate;
487    }
488
489    void Fighter::brakeLoop(float brakeLoop) {
490        brakeLoop_ = brakeLoop;
491    }
492
493    void Fighter::maxSpeedForward(float maxSpeedForward) {
494        maxSpeedForward_ = maxSpeedForward;
495    }
496
497    void Fighter::maxSpeedRotateUpDown(float maxSpeedRotateUpDown) {
498        maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
499    }
500
501    void Fighter::maxSpeedRotateRightLeft(float maxSpeedRotateRightLeft) {
502        maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
503    }
504
505    void Fighter::maxSpeedLoopRightLeft(float maxSpeedLoopRightLeft) {
506        maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
507    }
508}
Note: See TracBrowser for help on using the repository browser.