Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

several improvements:

  • AI works properly now - add enemies with 'createEnemy x' where x is the number of enemies to add, default is 1. You can remove AI ships with 'killEnemies'.
  • Added new explosion (with smoke)
  • Added new projectile (with trail)
  • Added new thruster emitter
  • AI ships are destroyable - they start with 100 hp, each hit makes 15 hp damage, this value is configurable in the config-file: [Projectile] damage_
  • Added AI ship spawn and explosion effects
  • Property svn:eol-style set to native
File size: 16.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      ...
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30
31#include <string>
32
33#include <OgreCamera.h>
34#include <OgreRenderWindow.h>
35#include <OgreSceneManager.h>
36#include <OgreSceneNode.h>
37
38#include "util/tinyxml/tinyxml.h"
39#include "util/String2Number.h"
40#include "core/CoreIncludes.h"
41#include "core/ConfigValueIncludes.h"
42#include "GraphicsEngine.h"
43#include "core/InputManager.h"
44#include "particle/ParticleInterface.h"
45#include "weapon/AmmunitionDump.h"
46#include "weapon/BarrelGun.h"
47
48#include "Fighter.h"
49
50namespace orxonox
51{
52    CreateFactory(Fighter);
53
54    Fighter::Fighter()
55    {
56        RegisterObject(Fighter);
57
58        this->setConfigValues();
59
60        this->setMouseEventCallback_ = false;
61
62        this->w = NULL;
63        this->tt = NULL;
64        this->ammoDump_ = NULL;
65        this->mainWeapon_ = NULL;
66        this->rightButtonPressed_ = false;
67        this->leftButtonPressed_ = false;
68
69        this->moveForward_ = 0;
70        this->rotateUp_ = 0;
71        this->rotateDown_ = 0;
72        this->rotateRight_ = 0;
73        this->rotateLeft_ = 0;
74        this->loopRight_ = 0;
75        this->loopLeft_ = 0;
76        this->brakeForward_ = 0;
77        this->brakeRotate_ = 0;
78        this->brakeLoop_ = 0;
79        this->speedForward_ = 0;
80        this->speedRotateUpDown_ = 0;
81        this->speedRotateRightLeft_ = 0;
82        this->speedLoopRightLeft_ = 0;
83        this->maxSpeedForward_ = 0;
84        this->maxSpeedRotateUpDown_ = 0;
85        this->maxSpeedRotateRightLeft_ = 0;
86        this->maxSpeedLoopRightLeft_ = 0;
87        this->accelerationForward_ = 0;
88        this->accelerationRotateUpDown_ = 0;
89        this->accelerationRotateRightLeft_ = 0;
90        this->accelerationLoopRightLeft_ = 0;
91
92        this->speed = 250;
93        this->loop = 100;
94        this->rotate = 10;
95        this->mouseX = 0;
96        this->mouseY = 0;
97        this->maxMouseX = 0;
98        this->minMouseX = 0;
99        this->moved = false;
100
101        this->brakeRotate(rotate*10);
102        this->brakeLoop(loop);
103
104        COUT(3) << "Info: Fighter was loaded" << std::endl;
105    }
106
107    Fighter::~Fighter()
108    {
109        if (w)
110            delete w;
111        if (tt)
112            delete tt;
113    }
114
115    void Fighter::setConfigValues()
116    {
117        SetConfigValue(bInvertMouse_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
118    }
119
120    void Fighter::setMaxSpeedValues(float maxSpeedForward, float maxSpeedRotateUpDown, float maxSpeedRotateRightLeft, float maxSpeedLoopRightLeft)
121    {
122        this->maxSpeedForward_ = maxSpeedForward;
123        this->maxSpeedRotateUpDown_ = maxSpeedRotateUpDown;
124        this->maxSpeedRotateRightLeft_ = maxSpeedRotateRightLeft;
125        this->maxSpeedLoopRightLeft_ = maxSpeedLoopRightLeft;
126    }
127
128    void Fighter::loadParams(TiXmlElement* xmlElem)
129    {
130        Model::loadParams(xmlElem);
131
132#if 0
133        w = new particle::ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"schuss" + this->getName(),"Orxonox/schuss");
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->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        GraphicsEngine::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 = GraphicsEngine::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            GraphicsEngine::getSingleton().getRenderWindow()->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 (InputManager::getSingleton()->getMouse())
258            {
259                InputManager::getSingleton()->getMouse()->setEventCallback(this);
260                this->setMouseEventCallback_ = true;
261            }
262        }
263
264        WorldEntity::tick(dt);
265
266        OIS::Keyboard* mKeyboard = InputManager::getSingleton()->getKeyboard();
267        OIS::Mouse* mMouse = InputManager::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.