Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

put all SetConfigValue macro calls into the setConfigValues function (this is needed to allow changes of the config values at runtime)

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