Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/space_ships/space_ship.cc @ 10032

Last change on this file since 10032 was 10032, checked in by muellmic, 17 years ago

changed roll- handling at left/right translation

File size: 28.6 KB
RevLine 
[6868]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Knecht
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
[7193]22#include "util/loading/resource_manager.h"
[6868]23
24#include "weapons/test_gun.h"
[9970]25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
[10023]28#include "weapons/swarm_launcher.h"
[9970]29#include "weapons/boomerang_gun.h"
[6868]30#include "weapons/turret.h"
31#include "weapons/cannon.h"
32
[9869]33#include "particles/dot_emitter.h"
34#include "particles/sprite_particles.h"
[6868]35
[7193]36#include "util/loading/factory.h"
[6868]37#include "key_mapper.h"
38
39#include "network_game_manager.h"
[8708]40#include "shared_network_data.h"
[6868]41
42#include "power_ups/weapon_power_up.h"
43#include "power_ups/param_power_up.h"
44
45#include "graphics_engine.h"
46
47#include "plane.h"
48
49#include "state.h"
50#include "player.h"
51
[7193]52#include "util/loading/load_param.h"
[10020]53#include "time.h"
[6868]54
[7056]55
[6868]56// #include "lib/gui/gl_gui/glgui_bar.h"
57// #include "lib/gui/gl_gui/glgui_pushbutton.h"
58
59
[9869]60#include "class_id_DEPRECATED.h"
61ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP);
62CREATE_FACTORY(SpaceShip);
[9406]63
[9061]64#include "script_class.h"
[9869]65CREATE_SCRIPTABLE_CLASS(SpaceShip,
66                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
67                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
68                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
69                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
70                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
71                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
72                        //Coordinates
73                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
74                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
75                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
76                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
[10017]77                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
[9061]78                       );
[6868]79
80/**
81 *  destructs the spaceship, deletes alocated memory
82 */
83SpaceShip::~SpaceShip ()
84{
[6986]85  this->setPlayer(NULL);
[6868]86}
87
88/**
89 * loads a Spaceships information from a specified file.
90 * @param fileName the name of the File to load the spaceship from (absolute path)
91 */
[7221]92SpaceShip::SpaceShip(const std::string& fileName)
[10019]93    : secWeaponMan(this) //,
94    //supportedPlaymodes(Playable::Vertical) ,
95    //playmode(Playable::Vertical)
[6868]96{
97  this->init();
98  TiXmlDocument doc(fileName);
99
100  if(!doc.LoadFile())
101  {
[7221]102    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
[6868]103    return;
104  }
105
106  this->loadParams(doc.RootElement());
107}
108
109/**
110 *  creates a new Spaceship from Xml Data
111 * @param root the xml element containing spaceship data
112
113   @todo add more parameters to load
114*/
115SpaceShip::SpaceShip(const TiXmlElement* root)
[10019]116    : secWeaponMan(this) //,
117    //supportedPlaymodes(Playable::Vertical) ,
118    //playmode(Playable::Vertical)
[6868]119{
120  this->init();
121  if (root != NULL)
122    this->loadParams(root);
123
124}
125
126
127/**
128 * initializes a Spaceship
129 */
130void SpaceShip::init()
131{
[9998]132
[10022]133  srand(time(0));   //initialize Random Nomber Generator
134
[9869]135  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
136  this->registerObject(this, SpaceShip::_objectList);
[6868]137
138  PRINTF(4)("SPACESHIP INIT\n");
139
[9998]140  secWeaponMan.showCrosshair();
141
[6868]142  //weapons:
[9998]143
[9996]144  Weapon* wpRight1 = new LightBlaster ();
[10030]145  wpRight1->setName("LightBlaster");
[9996]146  Weapon* wpLeft1 = new LightBlaster ();
[10030]147  wpLeft1->setName("LightBlaster");
[6868]148
[9996]149  Weapon* wpRight2 = new MediumBlaster ();
[10030]150  wpRight2->setName("MediumBlaster");
[9996]151  Weapon* wpLeft2 = new MediumBlaster ();
[10030]152  wpLeft2->setName("MediumBlaster");
[9996]153
[10030]154  Weapon* wpRight3 = new HeavyBlaster ();
155  wpRight3->setName("HeavyBlaster");
[9996]156  Weapon* wpLeft3 = new HeavyBlaster ();
[10030]157  wpLeft3->setName("HeavyBlaster");
[9996]158
[10023]159  Weapon* cannon = new SwarmLauncher();
160  cannon->setName("SwarmLauncher");
[10004]161 
[6868]162
[9961]163
[10016]164  this->weaponMan.addWeapon( wpLeft1, 0, 0);
165  this->weaponMan.addWeapon( wpRight1, 0, 1);
166  this->weaponMan.addWeapon( wpLeft2, 0, 2);
167  this->weaponMan.addWeapon( wpRight2, 0, 3);
[10030]168  this->weaponMan.addWeapon( wpLeft3, 0, 4);
169  this->weaponMan.addWeapon( wpRight3, 0, 5);
[9998]170
[10023]171  this->secWeaponMan.addWeapon( cannon, 1, 0);
[9961]172
[9965]173  this->weaponMan.changeWeaponConfig(0);
[10023]174  this->secWeaponMan.changeWeaponConfig(1);
[9961]175
[9996]176  wpRight1->requestAction(WA_ACTIVATE);
177  wpLeft1->requestAction(WA_ACTIVATE);
178  wpRight2->requestAction(WA_ACTIVATE);
179  wpLeft2->requestAction(WA_ACTIVATE);
[10030]180  wpRight3->requestAction(WA_ACTIVATE);
181  wpLeft3->requestAction(WA_ACTIVATE);
[9998]182
[9961]183  cannon->requestAction(WA_ACTIVATE);
[9965]184
[9970]185  curWeaponPrimary    = 0;
186  curWeaponSecondary  = 0;
[9965]187
188  Playable::weaponConfigChanged();
189
[9970]190  reactorOutput     = 10;
[9965]191
[9970]192  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
193  engineSpeedBase   = 5;
194  shieldRegen       = 2;
195
196  shieldEnergyShare = 0.3;
197  weaponEnergyShare = 0.3;
198  engineEnergyShare = 0.4;
199
200  shieldCur         = 20;
201  shieldMax         = 100;
[10020]202  shieldTH          = .2* shieldMax;   // shield power must be 20% before shield kicks in again
[9970]203
[10024]204  electronicCur = 50;
[10022]205  electronicMax = 50;
[10020]206  electronicRegen   = 3;
207  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
208
[9961]209  /*
[6868]210  this->addWeapon(wpLeft, 1, 0);
211  this->addWeapon(wpRight,1 ,1);
[7087]212  //this->addWeapon(cannon, 0, 6);
[6868]213
[7337]214  this->getWeaponManager().changeWeaponConfig(1);
[9961]215  */
[6868]216
[9970]217  this->loadModel("models/ships/mantawing.obj");
[10017]218  //this->setVisibiliy(false);
[6868]219
[10017]220  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
221 
222  /*
[6868]223  xMouse = yMouse = 0;
224  yInvert = 1;
225  mouseSensitivity = 0.001;
[7122]226  airViscosity = 0.9;
[6868]227  controlVelocityX = 25;
228  controlVelocityY = 150;
229  shipInertia = 1.5;
[10017]230  */
[9869]231  //  cycle = 0.0;
[6868]232
[9970]233  this->setHealthMax(shieldMax);
234  this->setHealth(shieldCur);
[6868]235
[10017]236  this->travelNode = new PNode();
237
238  // camera - issue
239  //this->cameraNode = State::getCameraNode();
240  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
241  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
242
243  /*
[6868]244  this->velocity = this->getAbsDirX()*travelSpeed;
245  this->mouseDir = this->getAbsDir();
246  this->pitchDir = this->getAbsDir();
[10017]247  */
[6868]248
[9965]249
[9869]250  //   GLGuiButton* button = new GLGuiPushButton();
251  //    button->show();
252  //    button->setLabel("orxonox");
253  //    button->setBindNode(this);
254  //     GLGuiBar* bar = new GLGuiBar();
255  //     bar->show();
256  //     bar->setValue(7.0);
257  //     bar->setMaximum(10);
258  //     bar->setSize2D( 20, 100);
259  //     bar->setAbsCoor2D( 10, 200);
[6868]260
261  //add events to the eventlist
[6997]262  registerEvent(KeyMapper::PEV_FORWARD);
263  registerEvent(KeyMapper::PEV_BACKWARD);
[6868]264  registerEvent(KeyMapper::PEV_LEFT);
265  registerEvent(KeyMapper::PEV_RIGHT);
266  //registerEvent(SDLK_q);
267  //registerEvent(SDLK_e);
268  registerEvent(KeyMapper::PEV_FIRE1);
[9958]269  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
[6868]270  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
271  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
272  //registerEvent(SDLK_PAGEUP);
273  //registerEvent(SDLK_PAGEDOWN);
274  registerEvent(EV_MOUSE_MOTION);
275
[9965]276  this->weaponMan.setSlotCount(6);
[6868]277
[9965]278  this->weaponMan.setSlotPosition(0, Vector(-2.6, .1, -3.0));
279  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]280
[9965]281  this->weaponMan.setSlotPosition(1, Vector(-2.6, .1, 3.0));
282  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]283
[9965]284  this->weaponMan.setSlotPosition(2, Vector(-1.5, .5, -.5));
285  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[6868]286
[9965]287  this->weaponMan.setSlotPosition(3, Vector(-1.5, .5, .5));
288  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[6868]289
[9965]290  this->weaponMan.setSlotPosition(4, Vector(-1.5, -.5, .5));
291  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
[6868]292
[9965]293  this->weaponMan.setSlotPosition(5, Vector(-1.5, -.5, -.5));
294  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9961]295
296  this->secWeaponMan.setSlotCount(6);
297
[10004]298  this->secWeaponMan.setSlotPosition(0, Vector(1.5, -1, 0));
[9961]299  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
300
301  this->secWeaponMan.setSlotPosition(1, Vector(2.6, .1, 3.0));
302  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
303
304  this->secWeaponMan.setSlotPosition(2, Vector(1.5, .5, -.5));
305  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
306
307  this->secWeaponMan.setSlotPosition(3, Vector(1.5, .5, .5));
308  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
309
310  this->secWeaponMan.setSlotPosition(4, Vector(1.5, -.5, .5));
311  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
312
313  this->secWeaponMan.setSlotPosition(5, Vector(1.5, -.5, -.5));
314  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9965]315
[9869]316  //
317  //   this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
318  //   this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
319  //
320  //   this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
321  //   this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
[6868]322
[9965]323  this->weaponMan.getFixedTarget()->setParent(this);
324  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]325
[9998]326 
[9961]327  this->secWeaponMan.getFixedTarget()->setParent(this);
328  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]329  this->secWeaponMan.setRotationSpeed(0);
[6868]330
[9965]331  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
[9961]332
[6868]333  this->burstEmitter = new DotEmitter(200, 0.0, .01);
334  this->burstEmitter->setParent(this);
335  this->burstEmitter->setRelCoor(-1, .5, 0);
336  this->burstEmitter->setName("SpaceShip_Burst_emitter");
337
338  this->burstSystem = new SpriteParticles(1000);
339  this->burstSystem->addEmitter(this->burstEmitter);
340  this->burstSystem->setName("SpaceShip_Burst_System");
341  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
342  this->burstSystem->setLifeSpan(1.0, .3);
343  this->burstSystem->setRadius(0.0, 1.0);
344  this->burstSystem->setRadius(0.05, 1.0);
345  this->burstSystem->setRadius(.5, .8);
346  this->burstSystem->setRadius(1.0, 0);
347  this->burstSystem->setColor(0.0, .7,.7,1,.7);
348  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
349  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
350  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
[9008]351
[10017]352  /*
[7954]353  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) );
354  registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) );
355  registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) );
356  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
357  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
358  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
359  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
360  registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) );
361  registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) );
[10017]362  */
363
364  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
365  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
366  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
367  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
368  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
369  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
[10023]370  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
[10017]371
372  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
373
374  //this->airFriction = 0.5f;
[10019]375  this->travelDistancePlus = Vector2D(38.0, 43.0);
376  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
377  this->travelSpeed = 80;
[10017]378  this->cameraSpeed = 40;
[10019]379  this->cameraLook = 0.0f;
380  this->acceleration = 120.0f;
381  //this->airFriction = 0.0f;
[10017]382
[10020]383  srand(time(0));  //initaialize RNG
384
[10017]385  this->travelNode->debugDraw();
[10032]386
387  this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical);
[6868]388}
389
390
391/**
392 * loads the Settings of a SpaceShip from an XML-element.
393 * @param root the XML-element to load the Spaceship's properties from
394 */
395void SpaceShip::loadParams(const TiXmlElement* root)
396{
397  Playable::loadParams(root);
[10032]398
399  LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML);
[6868]400}
401
[10032]402
[10017]403void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
[7056]404{
[10032]405  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
[7056]406}
[6868]407
[10017]408void SpaceShip::setTravelSpeed(float travelSpeed)
409{
410  this->travelSpeed = travelSpeed;
411}
[7056]412
[10017]413/*
414void SpaceShip::setTravelHeight(float travelHeight)
415{
416  if (this->toTravelHeight == NULL)
417    this->toTravelHeight = new float;
418  *this->toTravelHeight = travelHeight;
419}
420*/
421
[10019]422/*
[10017]423void SpaceShip::setTravelDistance(const Vector2D& distance)
424{
425  this->travelDistance = distance;
426}
427
428void SpaceShip::setTravelDistance(float x, float y)
429{
430  this->setTravelDistance(Vector2D(x, y));
431}
[10019]432*/
[10017]433
434
435
[7085]436void SpaceShip::reset()
437{
[10017]438  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
[7056]439
[10017]440  //xMouse = yMouse = 0;
[7085]441
442  this->setHealth(80);
443  this->velocity = Vector(0.0, 0.0, 0.0);
444}
445
446
[6868]447void SpaceShip::enter()
448{
[10004]449  this->secWeaponMan.showCrosshair();
450  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
[10019]451  //this->attachCamera();
[10032]452 // this->setPlaymode(Playable::Horizontal);
[6868]453}
454
455void SpaceShip::leave()
456{
[10004]457  this->secWeaponMan.hideCrosshair();
458  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
[10019]459  //this->detachCamera();
[6868]460}
461
462
463/**
464 *  effect that occurs after the SpaceShip is spawned
465*/
466void SpaceShip::postSpawn ()
467{
468  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
469}
470
471/**
472 *  the action occuring if the spaceship left the game
473*/
474void SpaceShip::leftWorld ()
475{}
476
477WorldEntity* ref = NULL;
478/**
479 *  this function is called, when two entities collide
480 * @param entity: the world entity with whom it collides
481 *
482 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
483 */
484void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
485{
486}
487
488/**
489 *  draws the spaceship after transforming it.
490*/
491void SpaceShip::draw () const
492{
493  WorldEntity::draw();
494
495  //this->debug(0);
496}
497
498/**
499 *  the function called for each passing timeSnap
500 * @param time The timespan passed since last update
501*/
502void SpaceShip::tick (float time)
503{
[9961]504  // Playable::tick(time);$
[6868]505
[9961]506  // Own Tick Setup, as a different fire routine is used on the weapon manager
[9965]507  this->weaponMan.tick(time);
[9961]508  this->secWeaponMan.tick(time);
509
[10020]510  if( this->systemFailure() )
511    bFire = bSecFire = false;
512
[9965]513  if( this->bFire)
[9961]514  {
[9965]515    this->weaponMan.fire();
[9961]516  }
517  if( this->bSecFire)
518  {
519    this->secWeaponMan.fire();
[9965]520    this->bSecFire = !this->bSecFire;   // FIXME This currently is needed to prevent "echo fires" of a second rocket after its cooldown has passed
[9961]521  }
522
[9970]523
524  // Shield Regeneration and other regular calculations on the ship
525  this->regen(time);
526
527  // Weapon Regeneration and other regular calculations on the ship
[9965]528  this->weaponRegen(time);
529
[9970]530  // current engine speed output
531  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
532
533
[10017]534  /*
[8708]535  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
[9869]536  {
[6868]537    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
538    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
539    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
540    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
541
542    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
543
544    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
545    xMouse = yMouse = 0;
[9869]546  }
[10017]547*/
[6868]548
[9235]549  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
[7954]550  //if (this->getOwner() == this->getHostID())
[6868]551
[10017]552  //is->calculateVelocity(time);
[6959]553
[6868]554
[10017]555  //vector move = velocity*time;
556
557  /*
[6868]558  //orient the velocity in the direction of the spaceship.
559  travelSpeed = velocity.len();
560  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
561  velocity = (velocity.getNormalized())*travelSpeed;
[10017]562  */
563  this->movement(time);
564
565   // TRYING TO FIX PNode.
566  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
567  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
568
[6868]569  this->burstEmitter->setEmissionRate(travelSpeed);
570  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
571
572  //orient the spaceship in direction of the mouse
[10017]573  /*
[9869]574  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
575  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6868]576    this->setAbsDir( rotQuat);
[9869]577  //this->setAbsDirSoft(mouseDir,5);
[10017]578  */
[6868]579
580  // this is the air friction (necessary for a smooth control)
[10017]581  /*
[6868]582  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
583  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
[10017]584  */
[6868]585
586  //other physics (gravity)
587  //if(travelSpeed < 120)
588  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
589
590  //hoover effect
591  //cycle += time;
592  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
593
594  //readjust
595  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
596  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
597
598  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
599
[10017]600  /*
[6868]601  this->shiftCoor(move);
[10017]602  */
[6868]603
[9869]604  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
[6868]605
606}
607
[10017]608/*
[6868]609void SpaceShip::calculateVelocity (float time)
610{
611  Vector accel(0.0, 0.0, 0.0);
[10017]612  /* calculate the direction in which the craft is heading  */
[6868]613  /*
614  if( this->bUp )
[9869]615  {
616    accel += (this->getAbsDirX())*acceleration;
[6868]617
[9869]618  }
[6868]619
620  if( this->bDown )
[9869]621  {
622    accel -= (this->getAbsDirX())*0.5*acceleration;
[6868]623
[9869]624  }
[6868]625
[10017]626  if( this->bLeft)
[6868]627  {
628    this->shiftDir(Quaternion(time, Vector(0,1,0)));
629  }
[10017]630  if( this->bRight)
[6868]631  {
632    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
633  }
634
635
[10017]636  if( this->bRollL)
[6868]637  {
638    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
639  }
[10017]640  if( this->bRollR)
[6868]641  {
642    mouseDir *= Quaternion(time*2, Vector(1,0,0));
643
644  }
645  if (this->bAscend )
646  {
647    this->shiftDir(Quaternion(time, Vector(0,0,1)));
648  }
649  if (this->bDescend )
650  {
651    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
652  }
653
[6959]654  velocity += accel*time*10;
[9961]655
[6868]656}
[10017]657*/
[6868]658
659/**
660 * @todo switch statement ??
661 */
662void SpaceShip::process(const Event &event)
663{
[9961]664  //Playable::process(event);
[6868]665
666  if( event.type == KeyMapper::PEV_LEFT)
[10017]667    this->bLeft = event.bPressed;
[6868]668  else if( event.type == KeyMapper::PEV_RIGHT)
[10017]669    this->bRight = event.bPressed;
[6997]670  else if( event.type == KeyMapper::PEV_FORWARD)
[10017]671    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6997]672  else if( event.type == KeyMapper::PEV_BACKWARD)
[10017]673    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[9961]674  else if( event.type == KeyMapper::PEV_FIRE2)
675    this->bSecFire = event.bPressed;
[9958]676  else if( event.type == KeyMapper::PEV_FIRE1)
[9965]677    this->bFire = event.bPressed;
[10017]678
679
680  /*
[6868]681  else if( event.type == EV_MOUSE_MOTION)
682  {
[9961]683
[6868]684    this->xMouse += event.xRel;
685    this->yMouse += event.yRel;
686  }
[10017]687  */
[6868]688}
689
[9235]690void SpaceShip::destroy( WorldEntity* killer )
[9008]691{
692  PRINTF(0)("spaceship destroy\n");
693}
[6868]694
[9008]695void SpaceShip::respawn( )
696{
697  toList( OM_PLAYERS );
698}
[6868]699
700
[9957]701void SpaceShip::damage(float pDamage, float eDamage){
[9950]702if( this->shieldActive) {
703    if( this->shieldCur > pDamage) {
704      this->shieldCur = this->shieldCur - pDamage;
705    }
706    else { // shield <= pDamage
[9987]707      this->shieldCur -=pDamage;
708      this->shieldActive = false; //shield collapses
[9950]709      pDamage = pDamage - this->shieldCur;
710      if( !this->shieldActive) {
[9957]711        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
[9953]712        this->electronicCur -= eDamage;
[9950]713      }
714    }
715  }
716  else {
717    this->armorCur = this->armorCur - pDamage;
718    this->electronicCur = this->electronicCur - eDamage;
719  }
720  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
721}
722
[9987]723
[9957]724void SpaceShip::regen(float time){
725  float tmp;
[9963]726  if (this->armorCur != this->armorMax || this->armorRegen != 0){
727    tmp = this->armorCur + this->armorRegen * time;
728    if ( tmp > electronicMax)
729      this->armorCur = this->armorMax;
730    else
731      this->armorCur = tmp;
732  }
733  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
[9970]734    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
[9957]735    if( tmp > shieldMax)
[9953]736      this->shieldCur = this->shieldMax;
737    else
[9957]738      this->shieldCur = tmp;
739    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
[9953]740  }
[9970]741
742  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
743
[9963]744  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
[9957]745    tmp = this->electronicCur + this->electronicRegen * time;
746    if ( tmp > electronicMax)
[9953]747      this->electronicCur = this->electronicMax;
748    else
[9957]749      this->electronicCur = tmp;
[9953]750  }
[9950]751}
[9965]752
[9987]753
[9965]754/**
755 * Weapon regeneration
756 * does not use any reactor capacity, as it wouldn't work in a consistent way.
757 */
758void SpaceShip::weaponRegen(float time)
759{
760  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
761  Weapon* weapon;
[9987]762  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
[9965]763  {
764    weapon = this->weaponMan.getWeapon(i);
765    if( weapon != NULL && weapon->isActive())
766    {
767      weapon->increaseEnergy( energy);
768    }
769
770  }
771  // weaponMan.increaseAmmunition( weapon, energy);
772}
[9975]773
[9987]774
[9975]775void SpaceShip::enterPlaymode(Playable::Playmode playmode)
776{
[10017]777  switch(playmode)
778  {
779    case Playable::Full3D:
780      /*
781      if (State::getCameraNode != NULL)
782      {
783        Vector absCoor = this->getAbsCoor();
784        this->setParent(PNode::getNullParent());
785        this->setAbsCoor(absCoor);
786        State::getCameraNode()->setParentSoft(&this->cameraNode);
787        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
788        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
789        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
790
791      }
792      */
793      //break;
794
[10032]795      break;
796    case Playable::Horizontal:
[10017]797      if (State::getCameraNode != NULL)
798      {
799        this->debugNode(1);
800        this->travelNode->debugNode(1);
801
802        this->travelNode->setAbsCoor(this->getAbsCoor());
803        this->travelNode->updateNode(0.01f);
804
805        this->setParent(this->travelNode);
806        this->setRelCoor(0,0,0);
807
808        State::getCameraNode()->setParentSoft(this->travelNode);
[10032]809        State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
[10017]810        State::getCameraTargetNode()->setParentSoft(this->travelNode);
[10019]811        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
[10017]812
813        this->debugNode(1);
814        this->travelNode->debugNode(1);
815      }
816      break;
817
818    default:
819      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
820  }
[9975]821}
822
[10017]823/**
824 * @brief calculate the velocity
825 * @param time the timeslice since the last frame
826*/
[9987]827
[9975]828void SpaceShip::movement (float dt)
829{
[10019]830  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
831  //the ship doesn't slide too much.
832  float airCoeff = 2.5;
833
834  float pi = 3.14;
835
[10032]836 
[10017]837
[10032]838  switch(this->getPlaymode())
[10017]839  {
[10032]840    case Playable::Horizontal:
[10017]841    {
[10032]842      // these routines will change the travel movement into zero in a short amout of time, if the player
843      // doesn't press any buttons.
844      if (this->velocity.x >= 0)
[10017]845      {
[10032]846        if (this->velocity.x > airCoeff*this->acceleration * dt)
847          this->velocity.x -= airCoeff* this->acceleration * dt;
848        else
849          this->velocity.x = 0;
[10017]850      }
851      else
852      {
[10032]853        if (this->velocity.x < -airCoeff*this->acceleration * dt)
854          this->velocity.x += airCoeff* this->acceleration * dt;
855        else
856          this->velocity.x = 0;
[10017]857      }
[10032]858      if (this->velocity.z >= 0)
[10017]859      {
[10032]860        if (this->velocity.z > airCoeff*this->acceleration * dt)
861          this->velocity.z -= airCoeff* this->acceleration * dt;
862        else
863          this->velocity.z = 0;
[10017]864      }
865      else
866      {
[10032]867        if (this->velocity.z < -airCoeff*this->acceleration * dt)
868          this->velocity.z += airCoeff* this->acceleration * dt;
869        else
870          this->velocity.z = 0;
[10017]871      }
[10032]872   
873      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
874      // and its continuing to slide a bit.
875      Vector oldCoor = this->getRelCoor();
876      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
877      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
878      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
879      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
880   
881      if( this->systemFailure() )
882        bForward = bBackward = bLeft = bRight = false;
883   
884   
885      if( this->bForward )
[10019]886      {
[10032]887        if(this->getRelCoor().x < this->travelDistancePlus.x)
888        {
889          if (this->velocity.x < this->travelSpeed)
890          {
891            this->velocity.x += (airCoeff + 1.0)*this->acceleration*dt;
892          }
893          else
894          {
895            this->velocity.x = this->travelSpeed;
896          }
897        }
898        else
899        {
900          this->velocity.x = 0.0f;
901        }
[10019]902      }
[10032]903   
904      if( this->bBackward )
[10019]905      {
[10032]906        if(this->getRelCoor().x > this->travelDistanceMinus.x)
907        {
908          if (this->velocity.x > -this->travelSpeed)
909          {
910            this->velocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
911          }
912          else
913          {
914            this->velocity.x = -this->travelSpeed;
915          }
916        }
917        else
918        {
919          this->velocity.x = 0.0f;
920        }
[10019]921      }
[10032]922   
923      if( this->bLeft)
[10019]924      {
[10032]925        if(this->getRelCoor().z > this->travelDistanceMinus.y)
926        {
927          if (this->velocity.z > -this->travelSpeed)
928          {
929            this->velocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
930          }
931          else
932          {
933            this->velocity.z = -this->travelSpeed;
934          }
935        }
936        else
937        {
938          this->velocity.z = 0.0f;
939        }
940        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 3);
[10019]941      }
[10032]942   
943      if( this->bRight)
[10019]944      {
[10032]945        if(this->getRelCoor().z < this->travelDistancePlus.y)
946        {
947          if (this->velocity.z < this->travelSpeed)
948          {
949            this->velocity.z += (airCoeff + 1.0)*this->acceleration*dt;
950          }
951          else
952          {
953            this->velocity.z = this->travelSpeed;
954          }
955        }
956        else
957        {
958          this->velocity.z = 0.0f;
959        }
960        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 3);
[10019]961      }
[10032]962      if (!this->bRight && !this->bLeft)
963      {
964        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 3);
965      }
966
967      this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
968      this->shiftCoor (this->velocity * dt);
969
970
971      break;
[10019]972    }
[10032]973    case Playable::Vertical:
[10019]974    {
[10032]975      break;
[10019]976    }
[10017]977    default:
[10032]978      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
[10017]979  }
[9975]980}
[10017]981
[10032]982void SpaceShip::setPlaymodeXML(const std::string& playmode)
983{
984  this->setPlaymode(Playable::stringToPlaymode(playmode));
985}
986
Note: See TracBrowser for help on using the repository browser.