Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/space_ship.cc @ 10739

Last change on this file since 10739 was 10739, checked in by rennerc, 17 years ago

die enemy die

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