Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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