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
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 <string>
33
34#include <OgreCamera.h>
35#include <OgreRenderWindow.h>
36#include <OgreParticleSystem.h>
37#include <OgreSceneNode.h>
38
39#include "CameraHandler.h"
40//#include "util/Convert.h"
41#include "util/Math.h"
42#include "core/CoreIncludes.h"
43#include "core/ConfigValueIncludes.h"
44#include "core/Debug.h"
45#include "GraphicsEngine.h"
46#include "core/InputManager.h"
47#include "particle/ParticleInterface.h"
48#include "Projectile.h"
49#include "core/XMLPort.h"
50#include "core/ConsoleCommand.h"
51#include "network/Client.h"
52
53namespace orxonox
54{
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
60    CreateFactory(SpaceShip);
61
62    SpaceShip* SpaceShip::instance_s;
63
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   
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)
107    {
108        RegisterObject(SpaceShip);
109        this->registerAllVariables();
110
111        SpaceShip::instance_s = this;
112
113        this->setConfigValues();
114
115
116        this->setRotationAxis(1, 0, 0);
117        this->setStatic(false);
118
119        COUT(3) << "Info: SpaceShip was loaded" << std::endl;
120    }
121
122    SpaceShip::~SpaceShip()
123    {
124        if (this->tt_)
125            delete this->tt_;
126        if(setMouseEventCallback_)
127          InputManager::removeMouseHandler("SpaceShip");
128        if (this->cam_)
129          delete this->cam_;
130    }
131
132    bool SpaceShip::create(){
133      if(Model::create())
134        this->init();
135      else
136        return false;
137      return true;
138    }
139
140    void SpaceShip::registerAllVariables(){
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);
151
152    }
153
154    void SpaceShip::init()
155    {
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        }
165
166        // START CREATING THRUSTER
167        this->tt_ = new ParticleInterface(GraphicsEngine::getSingleton().getSceneManager(),"twinthruster" + this->getName(),"Orxonox/engineglow");
168        this->tt_->getParticleSystem()->setParameter("local_space","true");
169        this->tt_->newEmitter();
170/*
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));
174*/
175        this->tt_->setDirection(Vector3(-1,0,0));
176        this->tt_->setPositionOfEmitter(0, Vector3(-15,20,-1));
177        this->tt_->setPositionOfEmitter(1, Vector3(-15,-20,-1));
178        this->tt_->setVelocity(50);
179
180        emitterRate_ = tt_->getRate();
181
182        Ogre::SceneNode* node2 = this->getNode()->createChildSceneNode(this->getName() + "particle2");
183        node2->setInheritScale(false);
184        tt_->addToSceneNode(node2);
185        // END CREATING THRUSTER
186
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);
190
191        this->redNode_ = this->getNode()->createChildSceneNode(this->getName() + "red", Vector3(0.3, 4.0, -0.3));
192        this->redNode_->setInheritScale(false);
193        this->greenNode_ = this->getNode()->createChildSceneNode(this->getName() + "green", Vector3(0.3, -4.0, -0.3));
194        this->greenNode_->setInheritScale(false);
195
196        this->redNode_->attachObject(this->redBillboard_.getBillboardSet());
197        this->redNode_->setScale(0.3, 0.3, 0.3);
198
199        this->greenNode_->attachObject(this->greenBillboard_.getBillboardSet());
200        this->greenNode_->setScale(0.3, 0.3, 0.3);
201        // END CREATING BLINKING LIGHTS
202
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);
206
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
218        createCamera();
219        // END of testing crosshair
220    }
221
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    {
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_);
252
253      cam_->setTargetNode(this->getNode());
254//        cam->setPosition(Vector3(0,-350,0));
255      if(network::Client::getSingleton()!=0 && network::Client::getSingleton()->getShipID()==objectID){
256        this->setBacksync(true);
257        CameraHandler::getInstance()->requestFocus(cam_);
258      }
259
260    }
261
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    */
283    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
284    {
285        Model::XMLPort(xmlelement, mode);
286
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);
295        server_=true; // TODO: this is only a hack
296        SpaceShip::create();
297        getFocus();
298    }
299
300    int sgn(float x)
301    {
302        if (x >= 0)
303            return 1;
304        else
305            return -1;
306    }
307
308    void SpaceShip::mouseMoved(IntVector2 abs, IntVector2 rel, IntVector2 clippingSize)
309    {
310/*
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;
322*/
323        if (this->bRMousePressed_)
324        {
325            this->camNode_->roll(Degree(-rel.x * 0.10));
326            this->camNode_->yaw(Degree(rel.y * 0.10));
327        }
328        else
329        {
330            float minDimension = clippingSize.y;
331            if (clippingSize.x < minDimension)
332                minDimension = clippingSize.x;
333
334            this->mouseX_ += rel.x;
335            if (this->mouseX_ < -minDimension)
336                this->mouseX_ = -minDimension;
337            if (this->mouseX_ > minDimension)
338                this->mouseX_ = minDimension;
339
340            this->mouseY_ += rel.y;
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        }
364    }
365
366    void SpaceShip::mouseButtonPressed(MouseButton::Enum id)
367    {
368        if (id == MouseButton::Left)
369            this->bLMousePressed_ = true;
370        else if (id == MouseButton::Right)
371            this->bRMousePressed_ = true;
372    }
373
374    void SpaceShip::mouseButtonReleased(MouseButton::Enum id)
375    {
376        if (id == MouseButton::Left)
377            this->bLMousePressed_ = false;
378        else if (id == MouseButton::Right)
379        {
380            this->bRMousePressed_ = false;
381            this->camNode_->resetOrientation();
382        }
383    }
384
385    void SpaceShip::tick(float dt)
386    {
387        if (this->cam_)
388            this->cam_->tick(dt);
389
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
399        if (this->timeToReload_ > 0)
400            this->timeToReload_ -= dt;
401        else
402            this->timeToReload_ = 0;
403
404        if (this->bLMousePressed_ && this->timeToReload_ <= 0)
405        {
406         
407            Projectile *p = new Projectile(this);
408           
409            p->setBacksync(true);
410            this->timeToReload_ = this->reloadTime_;
411        }
412
413
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
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))
483            this->acceleration_.x = this->translationAcceleration_;
484          else if(InputManager::isKeyDown(KeyCode::Down) || InputManager::isKeyDown(KeyCode::S))
485            this->acceleration_.x = -this->translationAcceleration_;
486          else
487            this->acceleration_.x = 0;
488
489          if (InputManager::isKeyDown(KeyCode::Right) || InputManager::isKeyDown(KeyCode::D))
490            this->acceleration_.y = -this->translationAcceleration_;
491          else if (InputManager::isKeyDown(KeyCode::Left) || InputManager::isKeyDown(KeyCode::A))
492            this->acceleration_.y = this->translationAcceleration_;
493          else
494            this->acceleration_.y = 0;
495
496          if (InputManager::isKeyDown(KeyCode::Delete) || InputManager::isKeyDown(KeyCode::Q))
497            this->momentum_ = Radian(-this->rotationAccelerationRadian_);
498          else if (InputManager::isKeyDown(KeyCode::PageDown) || InputManager::isKeyDown(KeyCode::E))
499            this->momentum_ = Radian(this->rotationAccelerationRadian_);
500          else
501            this->momentum_ = 0;
502        }/*else
503          COUT(4) << "not steering ship: " << objectID << " our ship: " << network::Client::getSingleton()->getShipID() << std::endl;*/
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);
517    }
518
519}
Note: See TracBrowser for help on using the repository browser.