Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10734 was 10734, checked in by snellen, 17 years ago

better flight behaviour for the ActionBox Enemy by rennerc

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