Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/asylum/orxonox/objects/Fighter.cc @ 1052

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

merged core2 back to trunk
there might be some errors, wasn't able to test it yet due to some strange g++ and linker behaviour.

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