Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bump

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