Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

initial modularisation in spaceship config

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