Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10139 was 10139, checked in by muellmic, 17 years ago

trying to adjust interface

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