Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Collision fixes, further additions

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/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  PRINTF(0)("SPACESHIP COLLIDED WITH %s\n",entity->getCName());
421}
422
423/**
424 *  draws the spaceship after transforming it.
425*/
426void SpaceShip::draw () const
427{
428  WorldEntity::draw();
429
430  glMatrixMode(GL_MODELVIEW);
431  glPushMatrix();
432
433  float matrix[4][4];
434  glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
435  this->getAbsDir().matrix (matrix);
436  glMultMatrixf((float*)matrix);
437  //glScalef(2.0, 2.0, 2.0);  // no double rescale
438 
439  this->trail->draw();
440 
441  glTranslatef(0,0,-.5);
442  this->trailL->draw();
443
444  glTranslatef(0,0,1);
445  this->trailR->draw();
446  glPopMatrix();
447  //this->debug(0);
448}
449
450/**
451 *  the function called for each passing timeSnap
452 * @param time The timespan passed since last update
453*/
454void SpaceShip::tick (float time)
455{
456  // Playable::tick(time);$
457
458  // Own Tick Setup, as a different fire routine is used on the weapon manager
459  this->weaponMan.tick(time);
460  this->secWeaponMan.tick(time);
461
462  if( this->systemFailure() )
463    bFire = bSecFire = false;
464
465  // fire reqeust/release for primary weapons
466  if( this->bFire)
467    this->weaponMan.fire();
468  else
469    this->weaponMan.releaseFire();
470
471  // fire reqeust/release for secondary weapons
472  if( this->bSecFire)
473    this->secWeaponMan.fire();
474  else
475    this->secWeaponMan.releaseFire();
476   
477  // Tracktick
478  if(this->entityTrack)
479    this->entityTrack->tick(time);
480
481
482  // Shield Regeneration and other regular calculations on the ship
483  this->regen(time);
484
485  // Weapon Regeneration and other regular calculations on the ship
486  this->weaponRegen(time);
487
488  // current engine speed output
489  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
490
491  // calculation of maxSpeed and acceleration:
492  this->travelSpeed = this->engineSpeedCur * 5;
493  this->acceleration = this->travelSpeed * 2;
494
495  this->movement(time);
496
497   // TRYING TO FIX PNode.
498  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
499  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
500
501 
502  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
503  this->oldPos    = this->getAbsCoor();
504
505  this->trail->tick(time);
506  this->trailL->tick(time);
507  this->trailR->tick(time);
508
509  this->weaponMan.setParentEntity( this);
510  this->secWeaponMan.setParentEntity( this);
511
512  //orient the spaceship in direction of the mouse
513  /*
514  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
515  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
516    this->setAbsDir( rotQuat);
517  //this->setAbsDirSoft(mouseDir,5);
518  */
519
520  // this is the air friction (necessary for a smooth control)
521  /*
522  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
523  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
524  */
525
526  //other physics (gravity)
527  //if(travelSpeed < 120)
528  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
529
530  //hoover effect
531  //cycle += time;
532  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
533
534  //readjust
535  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
536  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
537
538  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
539
540  /*
541  this->shiftCoor(move);
542  */
543
544  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
545
546}
547
548/**
549 * @todo switch statement ??
550 */
551void SpaceShip::process(const Event &event)
552{
553  //Playable::process(event);
554 
555  if( event.type == KeyMapper::PEV_LEFT)
556    this->bLeft = event.bPressed;
557  else if( event.type == KeyMapper::PEV_RIGHT)
558    this->bRight = event.bPressed;
559  else if( event.type == KeyMapper::PEV_FORWARD)
560    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
561  else if( event.type == KeyMapper::PEV_BACKWARD)
562    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
563  else if( event.type == KeyMapper::PEV_FIRE2)
564    this->bSecFire = event.bPressed;
565  else if( event.type == KeyMapper::PEV_FIRE1)
566    this->bFire = event.bPressed;
567  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
568  {
569    this->nextWeaponConfig();
570  }
571  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
572    this->previousWeaponConfig();
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(5)("spaceship destroy\n");
588 
589  EmitterNode* node  = NULL;
590  DotEmitter* emitter = NULL;
591  SpriteParticles*  explosionParticles  = NULL;
592
593  explosionParticles = new SpriteParticles(200);
594  explosionParticles->setName("SpaceShipExplosionParticles");
595  explosionParticles->setLifeSpan(.2, .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,.5);
603
604 
605  emitter = new DotEmitter( 2000, 70, 360);
606  //emitter->setSpread( 0, M_2_PI);
607  emitter->setEmissionRate( 200.0);
608  //emitter->setEmissionVelocity( 200.0);
609  //emitter->setSystem( explosionParticles);
610  //emitter->setAbsCoor( this->getAbsCoor());
611
612  node  = new EmitterNode( .1f);
613  node->setupParticle( emitter, explosionParticles);
614  node->setAbsDir( this->getAbsDir());
615  node->setVelocity( this->getVelocity() * .9f);
616  node->setAbsCoor( this->getAbsCoor());
617  if( !node->start())
618    PRINTF(0)("Explosion node not correctly started!");
619
620/*
621  PNode* node          = new PNode();
622  node->setAbsCoor(this->getAbsCoor());
623  Explosion* explosion = new Explosion();
624  explosion->explode( node, Vector(5,5,5));
625*/
626
627  if( this->hasPlayer())
628  {
629        this->setAbsCoor(Vector(-10000,10000,10000));
630        this->hide();
631  }
632  else
633  {
634    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
635  }
636
637}
638
639void SpaceShip::respawn( )
640{
641  this->unhide();
642  /*for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
643  {
644    if( dynamic_cast<WorldEntity*>(*it) != NULL)
645      dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_00);
646  }*/
647  /*
648  if( this->hasPlayer())
649  {
650    this->toList( OM_GROUP_01);
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_00_PROJ);
655        }
656  }
657  else
658  {
659    this->toList( OM_GROUP_00);
660    for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
661        {
662          if( likely( dynamic_cast<Weapon*>(*it) != NULL))
663                dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_01_PROJ);
664        }
665  }*/
666
667}
668
669
670void SpaceShip::damage(float pDamage, float eDamage){
671  PRINTF(0)("ship hit for (%f,%f) \n",pDamage,eDamage);
672
673  if( this->shieldActive) {
674    if( this->shieldCur > pDamage) {
675      this->shieldCur = this->shieldCur - pDamage;
676    }
677    else { // shield <= pDamage
678      this->shieldCur -=pDamage;
679      this->shieldActive = false; //shield collapses
680      pDamage += this->shieldCur;
681      if( !this->shieldActive) {
682        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
683        this->electronicCur -= eDamage;
684      }
685    }
686  }
687  else {
688    this->armorCur = this->armorCur - pDamage;
689    this->electronicCur = this->electronicCur - eDamage;
690  }
691  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
692    this->destroy(this);
693}
694
695
696void SpaceShip::regen(float time){
697  float tmp;
698  if (this->armorCur != this->armorMax || this->armorRegen != 0){
699    tmp = this->armorCur + this->armorRegen * time;
700    if ( tmp > electronicMax)
701      this->armorCur = this->armorMax;
702    else
703      this->armorCur = tmp;
704  }
705  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
706    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
707    if( tmp > shieldMax)
708      this->shieldCur = this->shieldMax;
709    else
710      this->shieldCur = tmp;
711    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
712  }
713
714  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
715
716  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
717    tmp = this->electronicCur + this->electronicRegen * time;
718    if ( tmp > electronicMax)
719      this->electronicCur = this->electronicMax;
720    else
721      this->electronicCur = tmp;
722  }
723}
724
725
726/**
727 * Weapon regeneration
728 * does not use any reactor capacity, as it wouldn't work in a consistent way.
729 */
730void SpaceShip::weaponRegen(float time)
731{
732  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
733  Weapon* weapon;
734  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
735  {
736    weapon = this->weaponMan.getWeapon(i);
737    if( weapon != NULL && weapon->isActive())
738    {
739      weapon->increaseEnergy( energy);
740    }
741
742  }
743  // weaponMan.increaseAmmunition( weapon, energy);
744}
745
746
747void SpaceShip::enterPlaymode(Playable::Playmode playmode)
748{
749  switch(playmode)
750  {
751    case Playable::Full3D:
752      /*
753      if (State::getCameraNode != NULL)
754      {
755        Vector absCoor = this->getAbsCoor();
756        this->setParent(PNode::getNullParent());
757        this->setAbsCoor(absCoor);
758        State::getCameraNode()->setParentSoft(&this->cameraNode);
759        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
760        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
761        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
762
763      }
764      */
765      //break;
766
767      break;
768    case Playable::Horizontal:
769      if (State::getCameraNode != NULL)
770      {
771        this->debugNode(1);
772        this->travelNode->debugNode(1);
773
774        this->travelNode->setAbsCoor(this->getAbsCoor());
775        this->travelNode->updateNode(0.01f);
776
777        this->setParent(this->travelNode);
778        this->setRelCoor(0,0,0);
779
780        State::getCameraNode()->setParentSoft(this->travelNode);
781        //State::getCameraNode()->setParentSoft(this);
782        State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
783        State::getCameraTargetNode()->setParentSoft(this->travelNode);
784        //State::getCameraTargetNode()->setParentSoft(this);
785        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
786
787        this->debugNode(1);
788        this->travelNode->debugNode(1);
789      }
790      break;
791
792    default:
793      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
794  }
795}
796
797/**
798 * @brief calculate the velocity
799 * @param time the timeslice since the last frame
800*/
801
802void SpaceShip::movement (float dt)
803{
804  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
805  //the ship doesn't slide too much.
806  float airCoeff = 2.5;
807  float pi = 3.14;
808 
809
810  switch(this->getPlaymode())
811  {
812    case Playable::Horizontal:
813    {
814      // these routines will change the travel movement into zero in a short amout of time, if the player
815      // doesn't press any buttons.
816      if (this->travelVelocity.x >= 0)
817      {
818        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
819          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
820        else
821          this->travelVelocity.x = 0;
822      }
823      else
824      {
825        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
826          this->travelVelocity.x += airCoeff* this->acceleration * dt;
827        else
828          this->travelVelocity.x = 0;
829      }
830      if (this->travelVelocity.z >= 0)
831      {
832        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
833          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
834        else
835          this->travelVelocity.z = 0;
836      }
837      else
838      {
839        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
840          this->travelVelocity.z += airCoeff* this->acceleration * dt;
841        else
842          this->travelVelocity.z = 0;
843      }
844   
845      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
846      // and its continuing to slide a bit.
847      Vector oldCoor = this->getRelCoor();
848      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
849      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
850      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
851      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
852   
853      if( this->systemFailure() )
854        bForward = bBackward = bLeft = bRight = false;
855   
856      if( this->bForward )
857      {
858        if(this->getRelCoor().x < this->travelDistancePlus.x)
859        {
860          if (this->travelVelocity.x < this->travelSpeed)
861          {
862            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
863          }
864          else
865          {
866            this->travelVelocity.x = this->travelSpeed;
867          }
868        }
869        else
870        {
871          this->travelVelocity.x = 0.0f;
872        }
873      }
874   
875      if( this->bBackward )
876      {
877        if(this->getRelCoor().x > this->travelDistanceMinus.x)
878        {
879          if (this->travelVelocity.x > -this->travelSpeed)
880          {
881            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
882          }
883          else
884          {
885            this->travelVelocity.x = -this->travelSpeed;
886          }
887        }
888        else
889        {
890          this->travelVelocity.x = 0.0f;
891        }
892      }
893   
894      if( this->bLeft)
895      {
896        if(this->getRelCoor().z > this->travelDistanceMinus.y)
897        {
898          if (this->travelVelocity.z > -this->travelSpeed)
899          {
900            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
901          }
902          else
903          {
904            this->travelVelocity.z = -this->travelSpeed;
905          }
906        }
907        else
908        {
909          this->travelVelocity.z = 0.0f;
910        }
911        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
912      }
913   
914      if( this->bRight)
915      {
916        if(this->getRelCoor().z < this->travelDistancePlus.y)
917        {
918          if (this->travelVelocity.z < this->travelSpeed)
919          {
920            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
921          }
922          else
923          {
924            this->travelVelocity.z = this->travelSpeed;
925          }
926        }
927        else
928        {
929          this->travelVelocity.z = 0.0f;
930        }
931        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
932      }
933      if (!this->bRight && !this->bLeft)
934      {
935        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
936      }
937
938    //normalisation of the vectors (vector sum must be <= travelspeed)
939    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
940    if (xzNorm > this->travelSpeed)
941    {
942      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
943      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
944    }
945
946    //this moves camera and ship along the travel path.
947    this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
948
949    break;
950    }
951    case Playable::Vertical:
952      break;
953    default:
954      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
955  }
956   //set new coordinates calculated through key- events.
957  this->shiftCoor (this->travelVelocity * dt);
958}
959
960void SpaceShip::setPlaymodeXML(const std::string& playmode)
961{
962  this->setPlaymode(Playable::stringToPlaymode(playmode));
963}
964
965/**
966 * @brief jumps to the next WeaponConfiguration
967 */
968void SpaceShip::nextWeaponConfig()
969{
970 
971  this->curWeaponPrimary    = (curWeaponPrimary + 1) % 3;
972  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
973  Playable::weaponConfigChanged();
974}
975
976/**
977 * @brief moves to the last WeaponConfiguration
978 */
979void SpaceShip::previousWeaponConfig()
980{
981  this->curWeaponPrimary    = (curWeaponPrimary + 1) % 3;
982  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
983  Playable::weaponConfigChanged();
984}
Note: See TracBrowser for help on using the repository browser.