Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc @ 10538

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

minor updates

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