Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

EOD commit

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