Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Last update, collision working again. SEGFAULT problems

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