Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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