Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/objects/Fighter.cc @ 1049

Last change on this file since 1049 was 1030, checked in by landauf, 17 years ago

extracted all config-value related macros from CoreIncludes.h and moved them to ConfigValueIncludes.h.

ConfigValueContainer can now handle std::vector<x> where 'x' is is any type supported by MultiTypeMath (all primitives, pointer, string, vector2, vector3, quaternion, colourvalue, radian, degree).

the vectors size is currently limited to 256 elements. this is just a practical limit, it can be raised if it's necessary. the reason for the limit is: you can add new elements to a vector by simply typing 'set classname varname index value' into the console or adding a new entry in the config-file. if 'index' is bigger than the vectors size, all elements up to 'index' are inserted. if the user accidentally enters a big number, he could end up with >4*109 elements in his config-file, resulting in 10-100gb on the hdd and a completely filled memory. and that's not exactly what i want ;)

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