Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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