Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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