Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

addition of local travel speed, removal of path hacks

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