Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

crosshair moves, shot do too, but not exactly where they should

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