Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/world_entities/space_ships/space_ship.cc @ 10648

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

some additional weapons, started weapons cleanup

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