Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10095 was 10095, checked in by marcscha, 17 years ago

velocity fix

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