Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1360 was 1360, checked in by rgrieder, 16 years ago

merged merge branch back to trunk

File size: 19.8 KB
RevLine 
[608]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1056]3 *                    > www.orxonox.net <
[608]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:
[670]23 *      Fabian 'x3n' Landau
[608]24 *   Co-authors:
[1293]25 *      Benjamin Knecht
[608]26 *
27 */
28
[786]29#include "OrxonoxStableHeaders.h"
[1039]30#include "SpaceShip.h"
[784]31
[715]32#include <string>
33
[708]34#include <OgreCamera.h>
35#include <OgreRenderWindow.h>
36#include <OgreParticleSystem.h>
37#include <OgreSceneNode.h>
[608]38
[1293]39#include "CameraHandler.h"
[1349]40//#include "util/Convert.h"
[742]41#include "util/Math.h"
[1021]42#include "core/CoreIncludes.h"
[1052]43#include "core/ConfigValueIncludes.h"
[1021]44#include "core/Debug.h"
[1032]45#include "GraphicsEngine.h"
[1024]46#include "core/InputManager.h"
[1021]47#include "particle/ParticleInterface.h"
[708]48#include "Projectile.h"
[871]49#include "core/XMLPort.h"
[1052]50#include "core/ConsoleCommand.h"
[1293]51#include "network/Client.h"
[608]52
53namespace orxonox
54{
[1052]55    ConsoleCommand(SpaceShip, setMaxSpeedTest, AccessLevel::Debug, false);
56    ConsoleCommandGeneric(test1, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxSpeed", AccessLevel::Debug), false);
57    ConsoleCommandGeneric(test2, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setMaxBlubber", AccessLevel::Debug), false);
58    ConsoleCommandGeneric(test3, SpaceShip, createExecutor(createFunctor(&SpaceShip::setMaxSpeedTest), "setRofl", AccessLevel::Debug), false);
59
[608]60    CreateFactory(SpaceShip);
61
[1052]62    SpaceShip* SpaceShip::instance_s;
63
[1360]64    SpaceShip *SpaceShip::getLocalShip(){
65      Iterator<SpaceShip> it;
66      for(it = ObjectList<SpaceShip>::start(); it; ++it){
67        if((it)->server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==it->objectID ) )
68          return *it;
69      }
70      return NULL;
71    }
72   
[1293]73    SpaceShip::SpaceShip() :
74      //testvector_(0,0,0),
75      //bInvertYAxis_(false),
76      setMouseEventCallback_(false),
77      bLMousePressed_(false),
78      bRMousePressed_(false),
79      camNode_(0),
80      cam_(0),
81      camName_("CamNode"),
82      tt_(0),
83      redNode_(0),
84      greenNode_(0),
85      blinkTime_(0.0f),
86      chNearNode_(0),
87      chFarNode_(0),
88      timeToReload_(0.0f),
89      //reloadTime_(0.0f),
90      maxSideAndBackSpeed_(0.0f),
91      maxSpeed_(0.0f),
92      maxRotation_(0.0f),
93      translationAcceleration_(0.0f),
94      rotationAcceleration_(0.0f),
95      translationDamping_(0.0f),
96      rotationDamping_(0.0f),
97      maxRotationRadian_(0),
98      rotationAccelerationRadian_(0),
99      rotationDampingRadian_(0),
100      zeroRadian_(0),
101      mouseXRotation_(0),
102      mouseYRotation_(0),
103      mouseX_(0.0f),
104      mouseY_(0.0f),
105      emitterRate_(0.0f),
106      server_(false)
[608]107    {
108        RegisterObject(SpaceShip);
[1021]109        this->registerAllVariables();
[608]110
[1052]111        SpaceShip::instance_s = this;
112
[697]113        this->setConfigValues();
[608]114
115
[647]116        this->setRotationAxis(1, 0, 0);
117        this->setStatic(false);
[608]118
119        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
120    }
121
122    SpaceShip::~SpaceShip()
123    {
[647]124        if (this->tt_)
125            delete this->tt_;
[1293]126        if(setMouseEventCallback_)
127          InputManager::removeMouseHandler("SpaceShip");
128        if (this->cam_)
129          delete this->cam_;
[608]130    }
131
[1021]132    bool SpaceShip::create(){
133      if(Model::create())
134        this->init();
135      else
136        return false;
137      return true;
138    }
[1056]139
[1021]140    void SpaceShip::registerAllVariables(){
[1293]141      registerVar( &camName_, camName_.length()+1, network::STRING, 0x1);
142      registerVar( &maxSpeed_, sizeof(maxSpeed_), network::DATA, 0x1);
143      registerVar( &maxSideAndBackSpeed_, sizeof(maxSideAndBackSpeed_), network::DATA, 0x1);
144      registerVar( &maxRotation_, sizeof(maxRotation_), network::DATA, 0x1);
145      registerVar( &translationAcceleration_, sizeof(translationAcceleration_), network::DATA, 0x1);
146      registerVar( &rotationAcceleration_, sizeof(rotationAcceleration_), network::DATA, 0x1);
147      registerVar( &rotationAccelerationRadian_, sizeof(rotationAccelerationRadian_), network::DATA, 0x1);
148      registerVar( &translationDamping_, sizeof(translationDamping_), network::DATA, 0x1);
149      registerVar( &rotationDamping_, sizeof(rotationDamping_), network::DATA, 0x1);
150      registerVar( &rotationDampingRadian_, sizeof(rotationDampingRadian_), network::DATA, 0x1);
[1056]151
[1021]152    }
[1056]153
[871]154    void SpaceShip::init()
[697]155    {
[1360]156        if ((server_ || ( network::Client::getSingleton() && network::Client::getSingleton()->getShipID()==objectID ) ))
157        {
158              if (!setMouseEventCallback_)
159              {
160                  InputManager::addMouseHandler(this, "SpaceShip");
161                  //InputManager::enableMouseHandler("SpaceShip");
162                  setMouseEventCallback_ = true;
163              }
164        }
[1293]165
[633]166        // START CREATING THRUSTER
[1032]167        this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
[647]168        this->tt_->getParticleSystem()->setParameter("local_space","true");
[978]169        this->tt_->newEmitter();
[608]170/*
[647]171        this->tt_->setDirection(Vector3(0,0,1));
172        this->tt_->setPositionOfEmitter(0, Vector3(20,-1,-15));
173        this->tt_->setPositionOfEmitter(1, Vector3(-20,-1,-15));
[608]174*/
[647]175        this->tt_->setDirection(Vector3(-1,0,0));
[978]176        this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1));
177        this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1));
[647]178        this->tt_->setVelocity(50);
[608]179
[633]180        emitterRate_ = tt_->getRate();
[626]181
[633]182        Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2");
183        node2->setInheritScale(false);
184        tt_->addToSceneNode(node2);
185        // END CREATING THRUSTER
[608]186
[633]187        // START CREATING BLINKING LIGHTS
188        this->redBillboard_.setBillboardSet("Examples/Flare", ColourValue(1.0, 0.0, 0.0), 1);
189        this->greenBillboard_.setBillboardSet("Examples/Flare", ColourValue(0.0, 1.0, 0.0), 1);
[608]190
[1293]191        this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3));
[633]192        this->redNode_->setInheritScale(false);
[1293]193        this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3));
[633]194        this->greenNode_->setInheritScale(false);
[608]195
[633]196        this->redNode_->attachObject(this->redBillboard_.getBillboardSet());
197        this->redNode_->setScale(0.3, 0.3, 0.3);
[608]198
[633]199        this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet());
200        this->greenNode_->setScale(0.3, 0.3, 0.3);
201        // END CREATING BLINKING LIGHTS
[608]202
[661]203        // START of testing crosshair
204        this->crosshairNear_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1);
205        this->crosshairFar_.setBillboardSet("Orxonox/Crosshair", ColourValue(1.0, 1.0, 0.0), 1);
[608]206
[661]207        this->chNearNode_ = this->getNode()->createChildSceneNode(this->getName() + "near", Vector3(50.0, 0.0, 0.0));
208        this->chNearNode_->setInheritScale(false);
209        this->chFarNode_ = this->getNode()->createChildSceneNode(this->getName() + "far", Vector3(200.0, 0.0, 0.0));
210        this->chFarNode_->setInheritScale(false);
211
212        this->chNearNode_->attachObject(this->crosshairNear_.getBillboardSet());
213        this->chNearNode_->setScale(0.2, 0.2, 0.2);
214
215        this->chFarNode_->attachObject(this->crosshairFar_.getBillboardSet());
216        this->chFarNode_->setScale(0.4, 0.4, 0.4);
217
[1293]218        createCamera();
[661]219        // END of testing crosshair
[871]220    }
[661]221
[871]222    void SpaceShip::setConfigValues()
223    {
224        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
225        SetConfigValue(reloadTime_, 0.125).description("The reload time of the weapon in seconds");
226        SetConfigValue(testvector_, Vector3()).description("asdfblah");
227    }
228
229    void SpaceShip::setCamera(const std::string& camera)
230    {
[1293]231      camName_=camera;
232      // change camera attributes here, if you want to ;)
233    }
234   
235    void SpaceShip::getFocus(){
236      COUT(4) << "requesting focus" << std::endl;
237      if(network::Client::getSingleton()==0 || network::Client::getSingleton()->getShipID()==objectID)
238        CameraHandler::getInstance()->requestFocus(cam_);
239     
240    }
241   
242    void SpaceShip::createCamera(){
243//       COUT(4) << "begin camera creation" << std::endl;
244      this->camNode_ = this->getNode()->createChildSceneNode(camName_);
245      COUT(4) << "position: (this)" << this->getNode()->getPosition() << std::endl;
246      this->camNode_->setPosition(Vector3(-50,0,10));
247      Quaternion q1 = Quaternion(Radian(Degree(90)),Vector3(0,-1,0));
248      Quaternion q2 = Quaternion(Radian(Degree(90)),Vector3(0,0,-1));
249      camNode_->setOrientation(q1*q2);
250      COUT(4) << "position: (cam)" << this->camNode_->getPosition() << std::endl;
251      cam_ = new Camera(this->camNode_);
[608]252
[1293]253      cam_->setTargetNode(this->getNode());
[871]254//        cam->setPosition(Vector3(0,-350,0));
[1293]255      if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){
256        this->setBacksync(true);
257        CameraHandler::getInstance()->requestFocus(cam_);
258      }
[608]259
260    }
261
[871]262    void SpaceShip::setMaxSpeed(float value)
263    { this->maxSpeed_ = value; }
264    void SpaceShip::setMaxSideAndBackSpeed(float value)
265    { this->maxSideAndBackSpeed_ = value; }
266    void SpaceShip::setMaxRotation(float value)
267    { this->maxRotation_ = value; this->maxRotationRadian_ = Radian(value); }
268    void SpaceShip::setTransAcc(float value)
269    { this->translationAcceleration_ = value; }
270    void SpaceShip::setRotAcc(float value)
271    { this->rotationAcceleration_ = value; this->rotationAccelerationRadian_ = Radian(value); }
272    void SpaceShip::setTransDamp(float value)
273    { this->translationDamping_ = value; }
274    void SpaceShip::setRotDamp(float value)
275    { this->rotationDamping_ = value; this->rotationDampingRadian_ = Radian(value); }
276
277    /**
278        @brief XML loading and saving.
279        @param xmlelement The XML-element
280        @param loading Loading (true) or saving (false)
281        @return The XML-element
282    */
[1052]283    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[871]284    {
[1052]285        Model::XMLPort(xmlelement, mode);
[871]286
[1052]287        XMLPortParamLoadOnly(SpaceShip, "camera", setCamera, xmlelement, mode);
288        XMLPortParamLoadOnly(SpaceShip, "maxSpeed", setMaxSpeed, xmlelement, mode);
289        XMLPortParamLoadOnly(SpaceShip, "maxSideAndBackSpeed", setMaxSideAndBackSpeed, xmlelement, mode);
290        XMLPortParamLoadOnly(SpaceShip, "maxRotation", setMaxRotation, xmlelement, mode);
291        XMLPortParamLoadOnly(SpaceShip, "transAcc", setTransAcc, xmlelement, mode);
292        XMLPortParamLoadOnly(SpaceShip, "rotAcc", setRotAcc, xmlelement, mode);
293        XMLPortParamLoadOnly(SpaceShip, "transDamp", setTransDamp, xmlelement, mode);
294        XMLPortParamLoadOnly(SpaceShip, "rotDamp", setRotDamp, xmlelement, mode);
[1293]295        server_=true; // TODO: this is only a hack
296        SpaceShip::create();
297        getFocus();
[871]298    }
299
[647]300    int sgn(float x)
301    {
302        if (x >= 0)
303            return 1;
304        else
305            return -1;
306    }
307
[1349]308    void SpaceShip::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
[608]309    {
[647]310/*
[608]311        this->mouseX += e.state.X.rel;
312        if (this->bInvertMouse_)
313            this->mouseY += e.state.Y.rel;
314        else
315            this->mouseY -= e.state.Y.rel;
316
317//        if(mouseX>maxMouseX) maxMouseX = mouseX;
318//        if(mouseX<minMouseX) minMouseX = mouseX;
319//        cout << "mouseX: " << mouseX << "\tmouseY: " << mouseY << endl;
320
321        this->moved = true;
[647]322*/
[644]323        if (this->bRMousePressed_)
324        {
[1349]325            this->camNode_->roll(Degree(-rel.x * 0.10));
326            this->camNode_->yaw(Degree(rel.y * 0.10));
[644]327        }
[647]328        else
329        {
[1349]330            float minDimension = clippingSize.y;
331            if (clippingSize.x < minDimension)
332                minDimension = clippingSize.x;
[644]333
[1349]334            this->mouseX_ += rel.x;
[647]335            if (this->mouseX_ < -minDimension)
336                this->mouseX_ = -minDimension;
337            if (this->mouseX_ > minDimension)
338                this->mouseX_ = minDimension;
339
[1349]340            this->mouseY_ += rel.y;
[647]341            if (this->mouseY_ < -minDimension)
342                this->mouseY_ = -minDimension;
343            if (this->mouseY_ > minDimension)
344                this->mouseY_ = minDimension;
345
346            float xRotation = this->mouseX_ / minDimension;
347            xRotation = xRotation*xRotation * sgn(xRotation);
348            xRotation *= -this->rotationAcceleration_;
349            if (xRotation > this->maxRotation_)
350                xRotation = this->maxRotation_;
351            if (xRotation < -this->maxRotation_)
352                xRotation = -this->maxRotation_;
353            this->mouseXRotation_ = Radian(xRotation);
354
355            float yRotation = this->mouseY_ / minDimension;
356            yRotation = yRotation*yRotation * sgn(yRotation);
357            yRotation *= this->rotationAcceleration_;
358            if (yRotation > this->maxRotation_)
359                yRotation = this->maxRotation_;
360            if (yRotation < -this->maxRotation_)
361                yRotation = -this->maxRotation_;
362            this->mouseYRotation_ = Radian(yRotation);
363        }
[608]364    }
365
[1349]366    void SpaceShip::mouseButtonPressed(MouseButton::Enum id)
[643]367    {
[1293]368        if (id == MouseButton::Left)
[644]369            this->bLMousePressed_ = true;
[1293]370        else if (id == MouseButton::Right)
[644]371            this->bRMousePressed_ = true;
[643]372    }
373
[1349]374    void SpaceShip::mouseButtonReleased(MouseButton::Enum id)
[643]375    {
[1293]376        if (id == MouseButton::Left)
[644]377            this->bLMousePressed_ = false;
[1293]378        else if (id == MouseButton::Right)
[644]379        {
380            this->bRMousePressed_ = false;
381            this->camNode_->resetOrientation();
382        }
[643]383    }
384
[608]385    void SpaceShip::tick(float dt)
386    {
[1293]387        if (this->cam_)
388            this->cam_->tick(dt);
[608]389
[633]390        if (this->redNode_ && this->greenNode_)
391        {
392            this->blinkTime_ += dt;
393            float redScale = 0.15 + 0.15 * sin(this->blinkTime_ * 10.0);
394            float greenScale = 0.15 - 0.15 * sin(this->blinkTime_ * 10.0);
395            this->redNode_->setScale(redScale, redScale, redScale);
396            this->greenNode_->setScale(greenScale, greenScale, greenScale);
397        }
398
[643]399        if (this->timeToReload_ > 0)
400            this->timeToReload_ -= dt;
401        else
402            this->timeToReload_ = 0;
403
[644]404        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
[643]405        {
[1360]406         
[1293]407            Projectile *p = new Projectile(this);
[1360]408           
[1293]409            p->setBacksync(true);
[643]410            this->timeToReload_ = this->reloadTime_;
411        }
412
[608]413
[647]414        // #####################################
415        // ############# STEERING ##############
416        // #####################################
417
418        if (this->velocity_.x > this->maxSpeed_)
419            this->velocity_.x = this->maxSpeed_;
420        if (this->velocity_.x < -this->maxSideAndBackSpeed_)
421            this->velocity_.x = -this->maxSideAndBackSpeed_;
422        if (this->velocity_.y > this->maxSideAndBackSpeed_)
423            this->velocity_.y = this->maxSideAndBackSpeed_;
424        if (this->velocity_.y < -this->maxSideAndBackSpeed_)
425            this->velocity_.y = -this->maxSideAndBackSpeed_;
426        if (this->rotationRate_ > this->maxRotationRadian_)
427            this->rotationRate_ = this->maxRotationRadian_;
428        if (this->rotationRate_ < -this->maxRotationRadian_)
429            this->rotationRate_ = -this->maxRotationRadian_;
430
431        if (this->acceleration_.x == 0)
432        {
433            if (this->velocity_.x > 0)
434            {
435                this->velocity_.x -= (this->translationDamping_ * dt);
436                if (this->velocity_.x < 0)
437                    this->velocity_.x = 0;
438            }
439            else if (this->velocity_.x < 0)
440            {
441                this->velocity_.x += (this->translationDamping_ * dt);
442                if (this->velocity_.x > 0)
443                    this->velocity_.x = 0;
444            }
445        }
446
447        if (this->acceleration_.y == 0)
448        {
449            if (this->velocity_.y > 0)
450            {
451                this->velocity_.y -= (this->translationDamping_ * dt);
452                if (this->velocity_.y < 0)
453                    this->velocity_.y = 0;
454            }
455            else if (this->velocity_.y < 0)
456            {
457                this->velocity_.y += (this->translationDamping_ * dt);
458                if (this->velocity_.y > 0)
459                    this->velocity_.y = 0;
460            }
461        }
462
463        if (this->momentum_ == this->zeroRadian_)
464        {
465            if (this->rotationRate_ > this->zeroRadian_)
466            {
467                this->rotationRate_ -= (this->rotationDampingRadian_ * dt);
468                if (this->rotationRate_ < this->zeroRadian_)
469                    this->rotationRate_ = 0;
470            }
471            else if (this->rotationRate_ < this->zeroRadian_)
472            {
473                this->rotationRate_ += (this->rotationDampingRadian_ * dt);
474                if (this->rotationRate_ > this->zeroRadian_)
475                    this->rotationRate_ = 0;
476            }
477        }
478
[1293]479        if( (network::Client::getSingleton() &&  network::Client::getSingleton()->getShipID() == objectID) || server_ )
480        {
481          COUT(4) << "steering our ship: " << objectID << std::endl;
482          if (InputManager::isKeyDown(KeyCode::Up) || InputManager::isKeyDown(KeyCode::W))
[647]483            this->acceleration_.x = this->translationAcceleration_;
[1293]484          else if(InputManager::isKeyDown(KeyCode::Down) || InputManager::isKeyDown(KeyCode::S))
[647]485            this->acceleration_.x = -this->translationAcceleration_;
[1293]486          else
[647]487            this->acceleration_.x = 0;
488
[1293]489          if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D))
[647]490            this->acceleration_.y = -this->translationAcceleration_;
[1293]491          else if (InputManager::isKeyDown(KeyCode::Left) || InputManager::isKeyDown(KeyCode::A))
[647]492            this->acceleration_.y = this->translationAcceleration_;
[1293]493          else
[647]494            this->acceleration_.y = 0;
495
[1293]496          if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q))
[647]497            this->momentum_ = Radian(-this->rotationAccelerationRadian_);
[1293]498          else if (InputManager::isKeyDown(KeyCode::PageDown) || InputManager::isKeyDown(KeyCode::E))
[647]499            this->momentum_ = Radian(this->rotationAccelerationRadian_);
[1293]500          else
[647]501            this->momentum_ = 0;
[1293]502        }/*else
503          COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/
[647]504
505        WorldEntity::tick(dt);
506
507        this->roll(this->mouseXRotation_ * dt);
508        if (this->bInvertYAxis_)
509            this->yaw(Radian(-this->mouseYRotation_ * dt));
510        else
511            this->yaw(Radian(this->mouseYRotation_ * dt));
512
513        if (this->acceleration_.x > 0)
514            this->tt_->setRate(emitterRate_);
515        else
516            this->tt_->setRate(0);
[608]517    }
518
519}
Note: See TracBrowser for help on using the repository browser.