Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10297 was 10297, checked in by bknecht, 17 years ago

track now works a bit can be partly used now

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