Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1453 was 1453, checked in by scheusso, 16 years ago

the spaceships of the other players now also have a radarpoint and targeting mode ;)

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