Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/orxonox/objects/SpaceShip.cc @ 1391

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