Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Armor → Health interconnection for UI

File size: 29.2 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/trail.h"
37
38#include "util/loading/factory.h"
39#include "key_mapper.h"
40
41#include "network_game_manager.h"
42#include "shared_network_data.h"
43
44#include "power_ups/weapon_power_up.h"
45#include "power_ups/param_power_up.h"
46
47#include "graphics_engine.h"
48
49#include "plane.h"
50
51#include "state.h"
52#include "player.h"
53
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, 1, 0);
176  this->weaponMan.addWeapon( wpRight2, 1, 1);
177  this->weaponMan.addWeapon( wpLeft3, 2, 0);
178  this->weaponMan.addWeapon( wpRight3, 2, 1);
179
180  this->secWeaponMan.addWeapon( cannon, 0, 0);
181
182/*
183  wpRight1->requestAction(WA_ACTIVATE);
184  wpLeft1->requestAction(WA_ACTIVATE);
185
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 
194  this->weaponMan.changeWeaponConfig(1);
195  this->secWeaponMan.changeWeaponConfig(0);
196
197  curWeaponPrimary    = 1;
198  curWeaponSecondary  = 0;
199
200  Playable::weaponConfigChanged();
201
202  reactorOutput     = 10;
203
204  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
205  engineSpeedBase   = 5;
206  shieldRegen       = 2;
207
208  shieldEnergyShare = 0.3;
209  weaponEnergyShare = 0.3;
210  engineEnergyShare = 0.4;
211
212  shieldCur         = 20;
213  shieldMax         = 100;
214  shieldTH          = .2* shieldMax;   // shield power must be 20% before shield kicks in again
215
216  this->setHealth( 20);
217  this->setHealthMax( 100);
218
219  electronicCur = 50;
220  electronicMax = 50;
221  electronicRegen   = 3;
222  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
223
224
225  this->loadModel("models/ships/mantawing.obj");
226  //this->setVisibiliy(false);
227
228  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
229 
230
231  this->setHealthMax(shieldMax);
232  this->setHealth(shieldCur);
233
234  this->travelNode = new PNode();
235
236  // camera - issue
237  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
238  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
239
240
241
242  //add events to the eventlist
243  registerEvent(KeyMapper::PEV_FORWARD);
244  registerEvent(KeyMapper::PEV_BACKWARD);
245  registerEvent(KeyMapper::PEV_LEFT);
246  registerEvent(KeyMapper::PEV_RIGHT);
247  //registerEvent(SDLK_q);
248  //registerEvent(SDLK_e);
249  registerEvent(KeyMapper::PEV_FIRE1);
250  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
251  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
252  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
253  //registerEvent(SDLK_PAGEUP);
254  //registerEvent(SDLK_PAGEDOWN);
255  registerEvent(EV_MOUSE_MOTION);
256
257  this->weaponMan.setParentEntity( this);
258  this->secWeaponMan.setParentEntity( this);
259
260  this->weaponMan.setSlotCount(6);
261
262  this->weaponMan.setSlotPosition(0, Vector(-2.6, 0, -3.0));
263  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
264
265  this->weaponMan.setSlotPosition(1, Vector(-2.6, 0, 3.0));
266  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
267
268  this->weaponMan.setSlotPosition(2, Vector(-1.5, 0, -.5));
269  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
270
271  this->weaponMan.setSlotPosition(3, Vector(-1.5, 0, .5));
272  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
273
274  this->weaponMan.setSlotPosition(4, Vector(-1.5, 0, .5));
275  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
276
277  this->weaponMan.setSlotPosition(5, Vector(-1.5, 0, -.5));
278  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
279
280  this->secWeaponMan.setSlotCount(6);
281
282  this->secWeaponMan.setSlotPosition(0, Vector(1.5, 0, 0));
283  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
284
285  this->secWeaponMan.setSlotPosition(1, Vector(2.6, 0, 3.0));
286  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
287
288  this->secWeaponMan.setSlotPosition(2, Vector(1.5, 0, -.5));
289  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
290
291  this->secWeaponMan.setSlotPosition(3, Vector(1.5, 0, .5));
292  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
293
294  this->secWeaponMan.setSlotPosition(4, Vector(1.5, 0, .5));
295  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
296
297  this->secWeaponMan.setSlotPosition(5, Vector(1.5, 0, -.5));
298  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
299
300
301  this->weaponMan.getFixedTarget()->setParent(this);
302  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
303
304 
305  this->secWeaponMan.getFixedTarget()->setParent(this);
306  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
307  this->secWeaponMan.setRotationSpeed(0);
308
309  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
310
311
312  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
313  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
314  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
315  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
316  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
317  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
318  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
319  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
320
321  //this->airFriction = 0.5f;
322  this->travelDistancePlus = Vector2D(38.0, 43.0);
323  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
324  this->cameraSpeed = 40;
325  this->cameraLook = 0.0f;
326  //this->airFriction = 0.0f;
327
328  srand(time(0));  //initaialize RNG
329
330  this->travelNode->debugDraw();
331
332  this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical);
333
334 
335  this->trail = new Trail( 10, 10, .2, this);
336  //this->trail->setParent( this);
337  this->trail->setTexture( "maps/engine.png");
338
339  this->trailL = new Trail( 10, 10, .2, this);
340  //this->trailL->setParent( this);
341  this->trailL->setTexture( "maps/engine.png");
342
343  this->trailR = new Trail( 10, 10, .2, this);
344  //this->trailR->setParent( this);
345  this->trailR->setTexture( "maps/engine.png");
346
347
348  this->toList(OM_GROUP_00);
349}
350
351
352/**
353 * loads the Settings of a SpaceShip from an XML-element.
354 * @param root the XML-element to load the Spaceship's properties from
355 */
356void SpaceShip::loadParams(const TiXmlElement* root)
357{
358  Playable::loadParams(root);
359
360  LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML);
361}
362
363
364void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
365{
366  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
367}
368
369void SpaceShip::reset()
370{
371  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
372
373  //xMouse = yMouse = 0;
374
375  this->setHealth(80);
376  this->velocity = Vector(0.0, 0.0, 0.0);
377}
378
379
380void SpaceShip::enter()
381{
382  this->secWeaponMan.showCrosshair();
383  this->toList( OM_GROUP_01 );
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  //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}
424
425/**
426 *  draws the spaceship after transforming it.
427*/
428void SpaceShip::draw () const
429{
430  WorldEntity::draw();
431
432  glMatrixMode(GL_MODELVIEW);
433  glPushMatrix();
434
435  float matrix[4][4];
436  glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
437  this->getAbsDir().matrix (matrix);
438  glMultMatrixf((float*)matrix);
439  //glScalef(2.0, 2.0, 2.0);  // no double rescale
440 
441  this->trail->draw();
442 
443  glTranslatef(0,0,-.5);
444  this->trailL->draw();
445
446  glTranslatef(0,0,1);
447  this->trailR->draw();
448  glPopMatrix();
449  //this->debug(0);
450}
451
452/**
453 *  the function called for each passing timeSnap
454 * @param time The timespan passed since last update
455*/
456void SpaceShip::tick (float time)
457{
458  // Playable::tick(time);$
459
460  // Own Tick Setup, as a different fire routine is used on the weapon manager
461  this->weaponMan.tick(time);
462  this->secWeaponMan.tick(time);
463
464  if( this->systemFailure() )
465    bFire = bSecFire = false;
466
467  // fire reqeust/release for primary weapons
468  if( this->bFire)
469    this->weaponMan.fire();
470  else
471    this->weaponMan.releaseFire();
472
473  // fire reqeust/release for secondary weapons
474  if( this->bSecFire)
475    this->secWeaponMan.fire();
476  else
477    this->secWeaponMan.releaseFire();
478   
479  // Tracktick
480  if(this->entityTrack)
481    this->entityTrack->tick(time);
482
483
484  // Shield Regeneration and other regular calculations on the ship
485  this->regen(time);
486
487  // Weapon Regeneration and other regular calculations on the ship
488  this->weaponRegen(time);
489
490  // current engine speed output
491  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
492
493  // calculation of maxSpeed and acceleration:
494  this->travelSpeed = this->engineSpeedCur * 5;
495  this->acceleration = this->travelSpeed * 2;
496
497  this->movement(time);
498
499   // TRYING TO FIX PNode.
500  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
501  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
502
503 
504  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
505  this->oldPos    = this->getAbsCoor();
506
507  this->trail->tick(time);
508  this->trailL->tick(time);
509  this->trailR->tick(time);
510
511  this->weaponMan.setParentEntity( this);
512  this->secWeaponMan.setParentEntity( this);
513
514  //orient the spaceship in direction of the mouse
515  /*
516  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
517  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
518    this->setAbsDir( rotQuat);
519  //this->setAbsDirSoft(mouseDir,5);
520  */
521
522  // this is the air friction (necessary for a smooth control)
523  /*
524  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
525  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
526  */
527
528  //other physics (gravity)
529  //if(travelSpeed < 120)
530  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
531
532  //hoover effect
533  //cycle += time;
534  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
535
536  //readjust
537  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
538  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
539
540  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
541
542  /*
543  this->shiftCoor(move);
544  */
545
546  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
547
548}
549
550/**
551 * @todo switch statement ??
552 */
553void SpaceShip::process(const Event &event)
554{
555  //Playable::process(event);
556 
557  if( event.type == KeyMapper::PEV_LEFT)
558    this->bLeft = event.bPressed;
559  else if( event.type == KeyMapper::PEV_RIGHT)
560    this->bRight = event.bPressed;
561  else if( event.type == KeyMapper::PEV_FORWARD)
562    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
563  else if( event.type == KeyMapper::PEV_BACKWARD)
564    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
565  else if( event.type == KeyMapper::PEV_FIRE2)
566    this->bSecFire = event.bPressed;
567  else if( event.type == KeyMapper::PEV_FIRE1)
568    this->bFire = event.bPressed;
569  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
570  {
571    this->nextWeaponConfig();
572  }
573  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
574    this->previousWeaponConfig();
575
576
577  /*
578  else if( event.type == EV_MOUSE_MOTION)
579  {
580
581    this->xMouse += event.xRel;
582    this->yMouse += event.yRel;
583  }
584  */
585}
586
587void SpaceShip::destroy( WorldEntity* killer )
588{
589  PRINTF(5)("spaceship destroy\n");
590 
591  EmitterNode* node  = NULL;
592  DotEmitter* emitter = NULL;
593  SpriteParticles*  explosionParticles  = NULL;
594
595  explosionParticles = new SpriteParticles(200);
596  explosionParticles->setName("SpaceShipExplosionParticles");
597  explosionParticles->setLifeSpan(.2, .3);
598  explosionParticles->setRadius(0.0, 10.0);
599  explosionParticles->setRadius(.5, 6.0);
600  explosionParticles->setRadius(1.0, 3.0);
601  explosionParticles->setColor(0.0, 1,1,1,.9);
602  explosionParticles->setColor(0.1,  1,1,0,.9);
603  explosionParticles->setColor(0.5, .8,.4,0,.5);
604  explosionParticles->setColor(1.0, .2,.2,.2,.5);
605
606 
607  emitter = new DotEmitter( 2000, 70, 360);
608  //emitter->setSpread( 0, M_2_PI);
609  emitter->setEmissionRate( 200.0);
610  //emitter->setEmissionVelocity( 200.0);
611  //emitter->setSystem( explosionParticles);
612  //emitter->setAbsCoor( this->getAbsCoor());
613
614  node  = new EmitterNode( .1f);
615  node->setupParticle( emitter, explosionParticles);
616  node->setAbsDir( this->getAbsDir());
617  node->setVelocity( this->getVelocity() * .9f);
618  node->setAbsCoor( this->getAbsCoor());
619  if( !node->start())
620    PRINTF(0)("Explosion node not correctly started!");
621
622/*
623  PNode* node          = new PNode();
624  node->setAbsCoor(this->getAbsCoor());
625  Explosion* explosion = new Explosion();
626  explosion->explode( node, Vector(5,5,5));
627*/
628
629  if( this->hasPlayer())
630  {
631        this->setAbsCoor(Vector(-10000,10000,10000));
632        this->hide();
633  }
634  else
635  {
636    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
637  }
638
639}
640
641void SpaceShip::respawn( )
642{
643  this->unhide();
644  /*for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
645  {
646    if( dynamic_cast<WorldEntity*>(*it) != NULL)
647      dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_00);
648  }*/
649  /*
650  if( this->hasPlayer())
651  {
652    this->toList( OM_GROUP_01);
653    for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
654        {
655          if( likely( dynamic_cast<Weapon*>(*it) != NULL))
656                dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_00_PROJ);
657        }
658  }
659  else
660  {
661    this->toList( OM_GROUP_00);
662    for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
663        {
664          if( likely( dynamic_cast<Weapon*>(*it) != NULL))
665                dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_01_PROJ);
666        }
667  }*/
668
669}
670
671
672void SpaceShip::damage(float pDamage, float eDamage){
673  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
674
675  if( this->shieldActive) {
676    if( this->shieldCur > pDamage) {
677      this->shieldCur = this->shieldCur - pDamage;
678    }
679    else { // shield <= pDamage
680      this->shieldCur -=pDamage;
681      this->shieldActive = false; //shield collapses
682      pDamage += this->shieldCur;
683      if( !this->shieldActive) {
684        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
685        this->electronicCur -= eDamage;
686      }
687    }
688  }
689  else {
690    this->armorCur = this->armorCur - pDamage;
691    this->electronicCur = this->electronicCur - eDamage;
692  }
693  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
694    this->destroy(this);
695
696  this->setHealth( this->armorCur);
697}
698
699
700void SpaceShip::regen(float time){
701  float tmp;
702  if (this->armorCur != this->armorMax || this->armorRegen != 0){
703    tmp = this->armorCur + this->armorRegen * time;
704    if ( tmp > electronicMax)
705      this->armorCur = this->armorMax;
706    else
707      this->armorCur = tmp;
708  }
709  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
710    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
711    if( tmp > shieldMax)
712      this->shieldCur = this->shieldMax;
713    else
714      this->shieldCur = tmp;
715    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
716  }
717
718  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
719
720  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
721    tmp = this->electronicCur + this->electronicRegen * time;
722    if ( tmp > electronicMax)
723      this->electronicCur = this->electronicMax;
724    else
725      this->electronicCur = tmp;
726  }
727}
728
729
730/**
731 * Weapon regeneration
732 * does not use any reactor capacity, as it wouldn't work in a consistent way.
733 */
734void SpaceShip::weaponRegen(float time)
735{
736  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
737  Weapon* weapon;
738  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
739  {
740    weapon = this->weaponMan.getWeapon(i);
741    if( weapon != NULL && weapon->isActive())
742    {
743      weapon->increaseEnergy( energy);
744    }
745
746  }
747  // weaponMan.increaseAmmunition( weapon, energy);
748}
749
750
751void SpaceShip::enterPlaymode(Playable::Playmode playmode)
752{
753  switch(playmode)
754  {
755    case Playable::Full3D:
756      /*
757      if (State::getCameraNode != NULL)
758      {
759        Vector absCoor = this->getAbsCoor();
760        this->setParent(PNode::getNullParent());
761        this->setAbsCoor(absCoor);
762        State::getCameraNode()->setParentSoft(&this->cameraNode);
763        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
764        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
765        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
766
767      }
768      */
769      //break;
770
771      break;
772    case Playable::Horizontal:
773      if (State::getCameraNode != NULL)
774      {
775        this->debugNode(1);
776        this->travelNode->debugNode(1);
777
778        this->travelNode->setAbsCoor(this->getAbsCoor());
779        this->travelNode->updateNode(0.01f);
780
781        this->setParent(this->travelNode);
782        this->setRelCoor(0,0,0);
783
784        State::getCameraNode()->setParentSoft(this->travelNode);
785        //State::getCameraNode()->setParentSoft(this);
786        State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
787        State::getCameraTargetNode()->setParentSoft(this->travelNode);
788        //State::getCameraTargetNode()->setParentSoft(this);
789        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
790
791        this->debugNode(1);
792        this->travelNode->debugNode(1);
793      }
794      break;
795
796    default:
797      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
798  }
799}
800
801/**
802 * @brief calculate the velocity
803 * @param time the timeslice since the last frame
804*/
805
806void SpaceShip::movement (float dt)
807{
808  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
809  //the ship doesn't slide too much.
810  float airCoeff = 2.5;
811  float pi = 3.14;
812 
813
814  switch(this->getPlaymode())
815  {
816    case Playable::Horizontal:
817    {
818      // these routines will change the travel movement into zero in a short amout of time, if the player
819      // doesn't press any buttons.
820      if (this->travelVelocity.x >= 0)
821      {
822        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
823          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
824        else
825          this->travelVelocity.x = 0;
826      }
827      else
828      {
829        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
830          this->travelVelocity.x += airCoeff* this->acceleration * dt;
831        else
832          this->travelVelocity.x = 0;
833      }
834      if (this->travelVelocity.z >= 0)
835      {
836        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
837          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
838        else
839          this->travelVelocity.z = 0;
840      }
841      else
842      {
843        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
844          this->travelVelocity.z += airCoeff* this->acceleration * dt;
845        else
846          this->travelVelocity.z = 0;
847      }
848   
849      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
850      // and its continuing to slide a bit.
851      Vector oldCoor = this->getRelCoor();
852      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
853      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
854      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
855      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
856   
857      if( this->systemFailure() )
858        bForward = bBackward = bLeft = bRight = false;
859   
860      if( this->bForward )
861      {
862        if(this->getRelCoor().x < this->travelDistancePlus.x)
863        {
864          if (this->travelVelocity.x < this->travelSpeed)
865          {
866            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
867          }
868          else
869          {
870            this->travelVelocity.x = this->travelSpeed;
871          }
872        }
873        else
874        {
875          this->travelVelocity.x = 0.0f;
876        }
877      }
878   
879      if( this->bBackward )
880      {
881        if(this->getRelCoor().x > this->travelDistanceMinus.x)
882        {
883          if (this->travelVelocity.x > -this->travelSpeed)
884          {
885            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
886          }
887          else
888          {
889            this->travelVelocity.x = -this->travelSpeed;
890          }
891        }
892        else
893        {
894          this->travelVelocity.x = 0.0f;
895        }
896      }
897   
898      if( this->bLeft)
899      {
900        if(this->getRelCoor().z > this->travelDistanceMinus.y)
901        {
902          if (this->travelVelocity.z > -this->travelSpeed)
903          {
904            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
905          }
906          else
907          {
908            this->travelVelocity.z = -this->travelSpeed;
909          }
910        }
911        else
912        {
913          this->travelVelocity.z = 0.0f;
914        }
915        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
916      }
917   
918      if( this->bRight)
919      {
920        if(this->getRelCoor().z < this->travelDistancePlus.y)
921        {
922          if (this->travelVelocity.z < this->travelSpeed)
923          {
924            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
925          }
926          else
927          {
928            this->travelVelocity.z = this->travelSpeed;
929          }
930        }
931        else
932        {
933          this->travelVelocity.z = 0.0f;
934        }
935        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
936      }
937      if (!this->bRight && !this->bLeft)
938      {
939        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
940      }
941
942    //normalisation of the vectors (vector sum must be <= travelspeed)
943    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
944    if (xzNorm > this->travelSpeed)
945    {
946      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
947      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
948    }
949
950    //this moves camera and ship along the travel path.
951    this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
952
953    break;
954    }
955    case Playable::Vertical:
956      break;
957    default:
958      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
959  }
960   //set new coordinates calculated through key- events.
961  this->shiftCoor (this->travelVelocity * dt);
962}
963
964void SpaceShip::setPlaymodeXML(const std::string& playmode)
965{
966  this->setPlaymode(Playable::stringToPlaymode(playmode));
967}
968
969/**
970 * @brief jumps to the next WeaponConfiguration
971 */
972void SpaceShip::nextWeaponConfig()
973{
974  PRINTF(0)("Requested next weapon config!\n");
975  this->weaponMan.nextWeaponConfig();
976  Playable::weaponConfigChanged();
977}
978
979/**
980 * @brief moves to the last WeaponConfiguration
981 */
982void SpaceShip::previousWeaponConfig()
983{
984  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
985  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
986  Playable::weaponConfigChanged();
987}
988
989void SpaceShip::hit(float damage, WorldEntity* killer)
990{
991  this->damage(killer->getDamage(),0);
992  killer->collidesWith( this, this->getAbsCoor());
993  //this->collidesWith(killer, this->getAbsCoor());
994}
Note: See TracBrowser for help on using the repository browser.