Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10096 was 10096, checked in by bknecht, 17 years ago

connected the track with the worldentities and the spaceship

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