Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/SpaceShip.cc @ 1553

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

moved ParticleInterface to tools, deleted particle

by the way, the last commit also changed a lot in ParticleInterface and added a new class, ParticleSpawner.

oh, and I forgot to say:
########################################

!!! UPDATE YOUR MEDIA REPOSITORY !!!

########################################

  • Property svn:eol-style set to native
File size: 20.2 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 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Benjamin Knecht
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "SpaceShip.h"
31
32#include <OgreCamera.h>
33#include <OgreRenderWindow.h>
34#include <OgreParticleSystem.h>
35#include <OgreSceneNode.h>
36
37#include "CameraHandler.h"
38#include "util/Convert.h"
39#include "util/Math.h"
40#include "core/CoreIncludes.h"
41#include "core/ConfigValueIncludes.h"
42#include "core/Debug.h"
43#include "GraphicsEngine.h"
44#include "core/input/InputManager.h"
45#include "tools/ParticleInterface.h"
46#include "RotatingProjectile.h"
47#include "ParticleProjectile.h"
48#include "core/XMLPort.h"
49#include "core/ConsoleCommand.h"
50#include "network/Client.h"
51#include "hud/HUD.h"
52
53namespace orxonox
54{
55    SetConsoleCommand(SpaceShip, setMaxSpeedTest, false).setAccessLevel(AccessLevel::Debug);
56    SetConsoleCommand(SpaceShip, whereAmI, true).setAccessLevel(AccessLevel::User);
57    SetConsoleCommand(SpaceShip, moveLongitudinal, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold);
58    SetConsoleCommand(SpaceShip, moveLateral, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold);
59    SetConsoleCommand(SpaceShip, moveYaw, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold);
60    SetConsoleCommand(SpaceShip, movePitch, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold);
61    SetConsoleCommand(SpaceShip, moveRoll, true).setAccessLevel(AccessLevel::User).setDefaultValue(0, 1.0f).setAxisParamIndex(0).setKeybindMode(KeybindMode::OnHold);
62    SetConsoleCommand(SpaceShip, fire, true).setAccessLevel(AccessLevel::User).setKeybindMode(KeybindMode::OnHold);
63    SetConsoleCommandGeneric(test1, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed"), false).setAccessLevel(AccessLevel::Debug);
64    SetConsoleCommandGeneric(test2, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber"), false).setAccessLevel(AccessLevel::Debug);
65    SetConsoleCommandGeneric(test3, SpaceShip, createConsoleCommand(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl"), false).setAccessLevel(AccessLevel::Debug);
66
67    CreateFactory(SpaceShip);
68
69    SpaceShip* SpaceShip::instance_s;
70
71
72    SpaceShip *SpaceShip::getLocalShip(){
73      Iterator<SpaceShip> it;
74      for(it = ObjectList<SpaceShip>::start(); it; ++it){
75        if( (it)->myShip_ )
76          return *it;
77      }
78      return NULL;
79    }
80
81    SpaceShip::SpaceShip() :
82      //testvector_(0,0,0),
83      //bInvertYAxis_(false),
84      setMouseEventCallback_(false),
85      bLMousePressed_(false),
86      bRMousePressed_(false),
87      camNode_(0),
88      cam_(0),
89      camName_("CamNode"),
90      tt1_(0),
91      tt2_(0),
92      redNode_(0),
93      greenNode_(0),
94      blinkTime_(0.0f),
95      chNearNode_(0),
96      chFarNode_(0),
97      timeToReload_(0.0f),
98      //reloadTime_(0.0f),
99      maxSideAndBackSpeed_(0.0f),
100      maxSpeed_(0.0f),
101      maxRotation_(0.0f),
102      translationAcceleration_(0.0f),
103      rotationAcceleration_(0.0f),
104      translationDamping_(0.0f),
105      rotationDamping_(0.0f),
106      maxRotationRadian_(0),
107      rotationAccelerationRadian_(0),
108      rotationDampingRadian_(0),
109      zeroRadian_(0),
110      mouseXRotation_(0),
111      mouseYRotation_(0),
112      mouseX_(0.0f),
113      mouseY_(0.0f),
114      myShip_(false),
115      teamNr_(0),
116      health_(100)
117    {
118        RegisterObject(SpaceShip);
119        this->registerAllVariables();
120
121        SpaceShip::instance_s = this;
122
123        this->setConfigValues();
124
125        initialDir_ = Vector3(1.0, 0.0, 0.0);
126        currentDir_ = initialDir_;
127        initialOrth_ = Vector3(0.0, 0.0, 1.0);
128        currentOrth_ = initialOrth_;
129
130        this->camName_ = this->getName() + "CamNode";
131
132        this->setRotationAxis(1, 0, 0);
133        this->setStatic(false);
134
135        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
136    }
137
138    SpaceShip::~SpaceShip()
139    {
140        if (this->tt1_)
141            delete this->tt1_;
142        if (this->tt2_)
143            delete this->tt2_;
144        if(setMouseEventCallback_)
145          InputManager::removeMouseHandler("SpaceShip");
146        if (this->cam_)
147          delete this->cam_;
148        if (!Identifier::isCreatingHierarchy() && !myShip_ && &HUD::getSingleton()!=NULL)
149          //remove the radar object
150          HUD::getSingleton().removeRadarObject(this->getNode());
151    }
152
153    bool SpaceShip::create(){
154      if(!myShip_){
155        if(network::Client::getSingleton() && objectID == network::Client::getSingleton()->getShipID())
156          myShip_=true;
157        else
158          HUD::getSingleton().addRadarObject(this->getNode(), 3);
159      }
160      if(Model::create())
161        this->init();
162      else
163        return false;
164      return true;
165    }
166
167    void SpaceShip::registerAllVariables(){
168      registerVar( &camName_, camName_.length()+1, network::STRING, 0x1 );
169      registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1);
170      registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1);
171      registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1);
172      registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1);
173      registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1);
174      registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1);
175      registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1);
176      registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1);
177      registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1);
178
179    }
180
181    void SpaceShip::init()
182    {
183        // START CREATING THRUSTER
184        this->tt1_ = new ParticleInterface("Orxonox/thruster1");
185        this->tt1_->createNewEmitter();
186        this->tt1_->getAllEmitters()->setDirection(-this->getInitialDir());
187        this->tt1_->getEmitter(0)->setPosition(Vector3(-15, 20, -1));
188        this->tt1_->getEmitter(1)->setPosition(Vector3(-15, -20, -1));
189        this->tt1_->setSpeedFactor(3.0);
190
191        Ogre::SceneNode* node2a = this->getNode()->createChildSceneNode(this->getName() + "particle2a");
192        node2a->setInheritScale(false);
193        node2a->setScale(1, 1, 1);
194        tt1_->addToSceneNode(node2a);
195
196        this->tt2_ = new ParticleInterface("Orxonox/thruster2");
197        this->tt2_->createNewEmitter();
198        this->tt2_->getAllEmitters()->setDirection(Vector3(-1, 0, 0));
199        this->tt2_->getEmitter(0)->setPosition(Vector3(-30, 40, -2));
200        this->tt2_->getEmitter(1)->setPosition(Vector3(-30, -40, -2));
201
202        Ogre::SceneNode* node2b = this->getNode()->createChildSceneNode(this->getName() + "particle2b");
203        node2b->setInheritScale(false);
204        node2b->setScale(0.5, 0.5, 0.5);
205        tt2_->addToSceneNode(node2b);
206        // END CREATING THRUSTER
207
208        // START CREATING BLINKING LIGHTS
209        this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
210        this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1);
211
212        this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3));
213        this->redNode_->setInheritScale(false);
214        this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3));
215        this->greenNode_->setInheritScale(false);
216
217        this->redNode_->attachObject(this->redBillboard_.getBillboardSet());
218        this->redNode_->setScale(0.3, 0.3, 0.3);
219
220        this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet());
221        this->greenNode_->setScale(0.3, 0.3, 0.3);
222        // END CREATING BLINKING LIGHTS
223
224        if (this->isExactlyA(Class(SpaceShip)))
225        {
226            // START of testing crosshair
227            this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1);
228            this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1);
229
230            this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0));
231            this->chNearNode_->setInheritScale(false);
232            this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0));
233            this->chFarNode_->setInheritScale(false);
234
235            this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet());
236            this->chNearNode_->setScale(0.2, 0.2, 0.2);
237
238            this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet());
239            this->chFarNode_->setScale(0.4, 0.4, 0.4);
240        }
241
242        createCamera();
243        // END of testing crosshair
244    }
245
246    void SpaceShip::setConfigValues()
247    {
248        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
249        SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds");
250        SetConfigValue(testvector_, Vector3()).description("asdfblah");
251    }
252
253    void SpaceShip::setCamera(const std::string& camera)
254    {
255      camName_=camera;
256      // change camera attributes here, if you want to ;)
257    }
258
259    void SpaceShip::getFocus(){
260      COUT(4) << "requesting focus" << std::endl;
261      if(network::Client::getSingleton()==0 || network::Client::getSingleton()->getShipID()==objectID)
262        CameraHandler::getInstance()->requestFocus(cam_);
263
264    }
265
266    Camera* SpaceShip::getCamera(){
267        return cam_;
268    }
269
270    void SpaceShip::createCamera(){
271//       COUT(4) << "begin camera creation" << std::endl;
272      this->camNode_ = this->getNode()->createChildSceneNode(camName_);
273      COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl;
274      this->camNode_->setPosition(Vector3(-25,0,5));
275//      Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0));
276//      Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1));
277//      camNode_->setOrientation(q1*q2);
278      COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl;
279      cam_ = new Camera(this->camNode_);
280
281      cam_->setTargetNode(this->getNode());
282//        cam->setPosition(Vector3(0,-350,0));
283      Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0));
284      Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(1,0,0));
285      camNode_->setOrientation(q2*q1);
286      if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){
287        this->setBacksync(true);
288        CameraHandler::getInstance()->requestFocus(cam_);
289      }
290
291    }
292
293    void SpaceShip::setMaxSpeed(float value)
294    { this->maxSpeed_ = value; }
295    void SpaceShip::setMaxSideAndBackSpeed(float value)
296    { this->maxSideAndBackSpeed_ = value; }
297    void SpaceShip::setMaxRotation(float value)
298    { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); }
299    void SpaceShip::setTransAcc(float value)
300    { this->translationAcceleration_ = value; }
301    void SpaceShip::setRotAcc(float value)
302    { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); }
303    void SpaceShip::setTransDamp(float value)
304    { this->translationDamping_ = value; }
305    void SpaceShip::setRotDamp(float value)
306    { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); }
307
308    /**
309        @brief XML loading and saving.
310        @param xmlelement The XML-element
311        @param loading Loading (true) or saving (false)
312        @return The XML-element
313    */
314    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
315    {
316        Model::XMLPort(xmlelement, mode);
317
318        XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode);
319        XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode);
320        XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode);
321        XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode);
322        XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode);
323        XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode);
324        XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode);
325        XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode);
326        myShip_=true; // TODO: this is only a hack
327
328        SpaceShip::create();
329        if (this->isExactlyA(Class(SpaceShip)))
330            getFocus();
331    }
332
333    int sgn(float x)
334    {
335        if (x >= 0)
336            return 1;
337        else
338            return -1;
339    }
340
341    std::string SpaceShip::whereAmI() {
342        return getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().x)
343        + "  " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().y)
344        + "  " + getConvertedValue<float, std::string>(SpaceShip::getLocalShip()->getPosition().z);
345    }
346
347    void SpaceShip::tick(float dt)
348    {
349        currentDir_ = getOrientation()*initialDir_;
350                currentOrth_ = getOrientation()*initialOrth_;
351
352        if (this->cam_)
353            this->cam_->tick(dt);
354
355        if (this->redNode_ && this->greenNode_)
356        {
357            this->blinkTime_ += dt;
358            float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0);
359            float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0);
360            this->redNode_->setScale(redScale, redScale, redScale);
361            this->greenNode_->setScale(greenScale, greenScale, greenScale);
362        }
363
364        if (this->timeToReload_ > 0)
365            this->timeToReload_ -= dt;
366        else
367            this->timeToReload_ = 0;
368
369        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
370        {
371
372            BillboardProjectile* projectile = new ParticleProjectile(this);
373            projectile->setColour(this->getProjectileColour());
374            projectile->create();
375            if (projectile->classID == 0)
376            {
377              COUT(3) << "generated projectile with classid 0" <<  std::endl; // TODO: remove this output
378            }
379
380            projectile->setBacksync(true);
381            this->timeToReload_ = this->reloadTime_;
382        }
383
384
385        // #####################################
386        // ############# STEERING ##############
387        // #####################################
388
389        if (this->velocity_.x > this->maxSpeed_)
390            this->velocity_.x = this->maxSpeed_;
391        if (this->velocity_.x < -this->maxSideAndBackSpeed_)
392            this->velocity_.x = -this->maxSideAndBackSpeed_;
393        if (this->velocity_.y > this->maxSideAndBackSpeed_)
394            this->velocity_.y = this->maxSideAndBackSpeed_;
395        if (this->velocity_.y < -this->maxSideAndBackSpeed_)
396            this->velocity_.y = -this->maxSideAndBackSpeed_;
397        if (this->rotationRate_ > this->maxRotationRadian_)
398            this->rotationRate_ = this->maxRotationRadian_;
399        if (this->rotationRate_ < -this->maxRotationRadian_)
400            this->rotationRate_ = -this->maxRotationRadian_;
401
402        if (this->acceleration_.x == 0)
403        {
404            if (this->velocity_.x > 0)
405            {
406                this->velocity_.x -= (this->translationDamping_ * dt);
407                if (this->velocity_.x < 0)
408                    this->velocity_.x = 0;
409            }
410            else if (this->velocity_.x < 0)
411            {
412                this->velocity_.x += (this->translationDamping_ * dt);
413                if (this->velocity_.x > 0)
414                    this->velocity_.x = 0;
415            }
416        }
417
418        if (this->acceleration_.y == 0)
419        {
420            if (this->velocity_.y > 0)
421            {
422                this->velocity_.y -= (this->translationDamping_ * dt);
423                if (this->velocity_.y < 0)
424                    this->velocity_.y = 0;
425            }
426            else if (this->velocity_.y < 0)
427            {
428                this->velocity_.y += (this->translationDamping_ * dt);
429                if (this->velocity_.y > 0)
430                    this->velocity_.y = 0;
431            }
432        }
433
434        if (this->momentum_ == this->zeroRadian_)
435        {
436            if (this->rotationRate_ > this->zeroRadian_)
437            {
438                this->rotationRate_ -= (this->rotationDampingRadian_ * dt);
439                if (this->rotationRate_ < this->zeroRadian_)
440                    this->rotationRate_ = 0;
441            }
442            else if (this->rotationRate_ < this->zeroRadian_)
443            {
444                this->rotationRate_ += (this->rotationDampingRadian_ * dt);
445                if (this->rotationRate_ > this->zeroRadian_)
446                    this->rotationRate_ = 0;
447            }
448        }
449
450
451        WorldEntity::tick(dt);
452
453        this->roll(this->mouseXRotation_ * dt);
454        if (this->bInvertYAxis_)
455            this->yaw(Radian(-this->mouseYRotation_ * dt));
456        else
457            this->yaw(Radian(this->mouseYRotation_ * dt));
458
459        if (this->acceleration_.x > 0)
460        {
461            this->tt1_->setEnabled(true);
462            this->tt2_->setEnabled(true);
463        }
464        else
465        {
466            this->tt1_->setEnabled(false);
467            this->tt2_->setEnabled(false);
468        }
469
470        if( myShip_ )
471        {
472          COUT(5) << "steering our ship: " << objectID << std::endl;
473          this->acceleration_.x = 0;
474          this->acceleration_.y = 0;
475          this->momentum_ = 0;
476          this->mouseXRotation_ = Radian(0);
477          this->mouseYRotation_ = Radian(0);
478          this->bLMousePressed_ = false;
479        }/*else
480          COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/
481    }
482
483    void SpaceShip::movePitch(float val)
484    {   getLocalShip()->setMovePitch(val);   }
485    void SpaceShip::moveYaw(float val)
486    {   getLocalShip()->setMoveYaw(val);   }
487    void SpaceShip::moveRoll(float val)
488    {   getLocalShip()->setMoveRoll(val);   }
489    void SpaceShip::moveLongitudinal(float val)
490    {   getLocalShip()->setMoveLongitudinal(val);   }
491    void SpaceShip::moveLateral(float val)
492    {   getLocalShip()->setMoveLateral(val);   }
493    void SpaceShip::fire()
494    {   getLocalShip()->doFire();   }
495
496    void SpaceShip::setMovePitch(float val)
497    {
498        val = -val * val * sgn(val) * this->rotationAcceleration_;
499        if (val > this->maxRotation_)
500            val = this->maxRotation_;
501        if (val < -this->maxRotation_)
502            val = -this->maxRotation_;
503        this->mouseYRotation_ = Radian(val);
504    }
505
506    void SpaceShip::setMoveYaw(float val)
507    {
508        val = -val * val * sgn(val) * this->rotationAcceleration_;
509        if (val > this->maxRotation_)
510            val = this->maxRotation_;
511        if (val < -this->maxRotation_)
512            val = -this->maxRotation_;
513        this->mouseXRotation_ = Radian(val);
514    }
515
516    void SpaceShip::setMoveRoll(float val)
517    {
518        this->momentum_ = Radian(-this->rotationAccelerationRadian_ * val);
519        //COUT(3) << "rotating val: " << val << " acceleration: " << this->rotationAccelerationRadian_.valueDegrees() << std::endl;
520    }
521
522    void SpaceShip::setMoveLongitudinal(float val)
523    {
524        this->acceleration_.x = this->translationAcceleration_ * val;
525    }
526
527    void SpaceShip::setMoveLateral(float val)
528    {
529        this->acceleration_.y = -this->translationAcceleration_ * val;
530    }
531
532    void SpaceShip::doFire()
533    {
534        this->bLMousePressed_ = true;
535    }
536}
Note: See TracBrowser for help on using the repository browser.