Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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