Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10133 was 10133, checked in by nicolasc, 17 years ago

animation in heavy blaster

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