Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1473 was 1454, checked in by landauf, 17 years ago

fixed tcl initialisation bug

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