Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

mouse can now be moved only within a restricted area

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  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    this->bFire = event.bPressed;
577  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
578  {
579    this->nextWeaponConfig();
580  }
581  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
582    this->previousWeaponConfig();
583  else if( event.type == EV_MOUSE_MOTION)
584  {
585//     printf("Spaceship Mouse Motion Event\n");
586//     dynamic_cast<Crosshair*>(this->weaponMan.getFixedTarget())->process(event);
587//     this->weaponMan.process(event);
588    this->xMouse += event.xRel;
589    this->yMouse += event.yRel;
590  //  printf("Mouse Coord: %f, %f\n", this->xMouse, this->yMouse);
591   
592    float maxMouseDist = 75.1;
593    #define sqr(a) ((a)*(a))
594    float mouseDist = sqrt( sqr(0.6*xMouse) + sqr(yMouse) );
595
596    if ( mouseDist > maxMouseDist )
597    {
598        xMouse /= mouseDist/maxMouseDist;
599        yMouse /= mouseDist/maxMouseDist;
600    }
601
602    this->weaponMan.getFixedTarget()->setRelCoor(100000, -100 * this->mouseSensitivity * yMouse, 100 * this->mouseSensitivity * xMouse);
603    this->secWeaponMan.getFixedTarget()->setRelCoor(50000, -50 * this->mouseSensitivity * yMouse, 50 * this->mouseSensitivity * xMouse);
604  }
605  else if (!(State::getCamera()->getEventHandling()))
606  {
607    //PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
608    if( event.type == KeyMapper::PEV_VIEW0)
609    {
610      State::getCamera()->setViewMode(Camera::ViewNormal);
611      State::getCameraTargetNode()->setParent(this);
612      State::getCamera()->setParent(this);
613    }
614    else if( event.type == KeyMapper::PEV_VIEW1)
615    {
616      State::getCamera()->setViewMode(Camera::ViewBehind);
617      State::getCameraTargetNode()->setParent(this);
618      State::getCamera()->setParent(this);
619    }
620    else if( event.type == KeyMapper::PEV_VIEW2)
621    {
622      State::getCamera()->setViewMode(Camera::ViewFront);
623      State::getCameraTargetNode()->setParent(this);
624      State::getCamera()->setParent(this);
625    }
626    else if( event.type == KeyMapper::PEV_VIEW3)
627    {
628      State::getCamera()->setViewMode(Camera::ViewLeft);
629      State::getCameraTargetNode()->setParent(this);
630      State::getCamera()->setParent(this);
631    }
632    else if( event.type == KeyMapper::PEV_VIEW4)
633    {
634      State::getCamera()->setViewMode(Camera::ViewRight);
635      State::getCameraTargetNode()->setParent(this);
636      State::getCamera()->setParent(this);
637    }
638    else if( event.type == KeyMapper::PEV_VIEW5)
639    {
640      State::getCamera()->setViewMode(Camera::ViewTop);
641      State::getCameraTargetNode()->setParent(this->travelNode);
642      State::getCamera()->setParent(this->travelNode);
643    }
644  }
645
646}
647
648void SpaceShip::destroy( WorldEntity* killer )
649{
650  if(this->hasPlayer())
651    Playable::destroy( killer);
652
653  PRINTF(5)("spaceship destroy\n");
654
655  EmitterNode* node  = NULL;
656  DotEmitter* emitter = NULL;
657  SpriteParticles*  explosionParticles  = NULL;
658
659  explosionParticles = new SpriteParticles(200);
660  explosionParticles->setName("SpaceShipExplosionParticles");
661  explosionParticles->setLifeSpan(.2, .3);
662  explosionParticles->setRadius(0.0, 10.0);
663  explosionParticles->setRadius(.5, 6.0);
664  explosionParticles->setRadius(1.0, 3.0);
665  explosionParticles->setColor(0.0, 1,1,1,.9);
666  explosionParticles->setColor(0.1,  1,1,0,.9);
667  explosionParticles->setColor(0.5, .8,.4,0,.5);
668  explosionParticles->setColor(1.0, .2,.2,.2,.5);
669
670
671  emitter = new DotEmitter( 2000, 70, 360);
672  //emitter->setSpread( 0, M_2_PI);
673  emitter->setEmissionRate( 200.0);
674  //emitter->setEmissionVelocity( 200.0);
675  //emitter->setSystem( explosionParticles);
676  //emitter->setAbsCoor( this->getAbsCoor());
677
678  node  = new EmitterNode( .1f);
679  node->setupParticle( emitter, explosionParticles);
680  node->setAbsDir( this->getAbsDir());
681  node->setVelocity( this->getVelocity() * .9f);
682  node->setAbsCoor( this->getAbsCoor());
683  if( !node->start())
684    PRINTF(0)("Explosion node not correctly started!");
685/*
686  PNode* node          = new PNode();
687  node->setAbsCoor(this->getAbsCoor());
688  Explosion* explosion = new Explosion();
689  explosion->explode( node, Vector(5,5,5));
690*/
691/*
692  if( this->hasPlayer())
693  {
694        this->setAbsCoor(Vector(-10000,10000,10000));
695        this->hide();
696  }
697  else
698  {*/
699    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
700  //}
701
702}
703
704void SpaceShip::respawn( )
705{
706  Playable::respawn();
707}
708
709
710void SpaceShip::damage(float pDamage, float eDamage){
711  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
712
713  static float tmp = this->decreaseShield(pDamage);
714  if( tmp > 0)
715  if ( this->decreaseHealth(tmp / 2) > 0)
716    this->destroy(this);
717
718}
719
720
721
722/**
723 * Weapon regeneration
724 * does not use any reactor capacity, as it wouldn't work in a consistent way.
725 */
726void SpaceShip::weaponRegen(float time)
727{
728  float energy = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time ;
729  Weapon* weapon;
730  int weaponCount = 0;
731  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
732    weapon = this->weaponMan.getWeapon(i);
733    if( weapon != NULL && weapon->isActive()) {
734      weaponCount++;
735    }
736  }
737
738  if (weaponCount == 0)
739    return;
740  energy *= ( this->weaponMan.getSlotCount() / (float)weaponCount );
741
742  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
743    weapon = this->weaponMan.getWeapon(i);
744    if( weapon != NULL && weapon->isActive()) {
745      weapon->increaseEnergy( energy);
746    }
747  }
748  // weaponMan.increaseAmmunition( weapon, energy);
749}
750
751
752void SpaceShip::enterPlaymode(Playable::Playmode playmode){
753  switch(playmode)
754  {
755    case Playable::Full3D:
756      /*
757      if (State::getCameraNode != NULL)
758      {
759        Vector absCoor = this->getAbsCoor();
760        this->setParent(PNode::getNullParent());
761        this->setAbsCoor(absCoor);
762        State::getCameraNode()->setParentSoft(&this->cameraNode);
763        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
764        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
765        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
766
767      }
768      */
769      //break;
770
771      break;
772    case Playable::Horizontal:
773      if (State::getCameraNode != NULL)
774      {
775        this->debugNode(1);
776        this->travelNode->debugNode(1);
777
778        this->travelNode->setAbsCoor(this->getAbsCoor());
779        this->travelNode->updateNode(0.01f);
780
781        this->isTravelDistanceInit = false;
782
783        if(this->entityTrack)
784           this->travelNode->setParent(this->entityTrack->getTrackNode());
785
786        this->setParent(this->travelNode);
787        this->setRelCoor(0,0,0);
788
789        State::getCameraNode()->setParentSoft(this->travelNode);
790        //State::getCameraNode()->setParentSoft(this);
791        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
792        State::getCameraTargetNode()->setParentSoft(this->travelNode);
793        //State::getCameraTargetNode()->setParentSoft(this);
794        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
795        this->setCameraMode(Camera::ViewTop);
796        State::getCamera()->setEventHandling(false);
797        registerEvent(KeyMapper::PEV_VIEW0);
798        registerEvent(KeyMapper::PEV_VIEW1);
799        registerEvent(KeyMapper::PEV_VIEW2);
800        registerEvent(KeyMapper::PEV_VIEW3);
801        registerEvent(KeyMapper::PEV_VIEW4);
802        registerEvent(KeyMapper::PEV_VIEW5);
803
804        State::getCamera()->setParentMode(PNODE_ALL);
805
806        //this->updateTravelDistance();
807
808        this->debugNode(1);
809        this->travelNode->debugNode(1);
810      }
811      break;
812
813
814    case Playable::Vertical:
815    {
816        this->travelNode->setAbsCoor(this->getAbsCoor());
817        this->travelNode->updateNode(0.01f);
818
819        this->isTravelDistanceInit = false;
820
821        if(this->entityTrack)
822           this->travelNode->setParent(this->entityTrack->getTrackNode());
823
824        this->setParent(this->travelNode);
825        this->setRelCoor(0,0,0);
826
827        State::getCameraNode()->setParentSoft(this->travelNode);
828        //State::getCameraNode()->setParentSoft(this);
829        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
830        State::getCameraTargetNode()->setParentSoft(this->travelNode);
831        //State::getCameraTargetNode()->setParentSoft(this);
832        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
833        //this->setCameraMode(Camera::ViewNormal);
834        State::getCamera()->setEventHandling(false);
835
836        PRINTF(0)("SETCAMERA %x\n", State::getCamera());
837        State::getCamera()->setViewMode(Camera::ViewNormal);
838        State::getCameraTargetNode()->setParent(this);
839        State::getCamera()->setParent(this);
840
841
842        registerEvent(KeyMapper::PEV_VIEW0);
843        registerEvent(KeyMapper::PEV_VIEW1);
844        registerEvent(KeyMapper::PEV_VIEW2);
845        registerEvent(KeyMapper::PEV_VIEW3);
846        registerEvent(KeyMapper::PEV_VIEW4);
847        registerEvent(KeyMapper::PEV_VIEW5);
848
849        State::getCamera()->setParentMode(PNODE_ALL);
850
851      break;
852    }
853
854    default:
855      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
856  }
857}
858
859/**
860 * @brief calculate the velocity
861 * @param time the timeslice since the last frame
862*/
863
864void SpaceShip::movement (float dt)
865{
866  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
867  //the ship doesn't slide too much.
868  float airCoeff = 2.5;
869  float pi = 3.14;
870
871  switch(this->getPlaymode())
872  {
873    case Playable::Horizontal:
874    {
875      // these routines will change the travel movement into zero in a short amout of time, if the player
876      // doesn't press any buttons.
877      if (this->travelVelocity.x >= 0)
878      {
879        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
880          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
881        else
882          this->travelVelocity.x = 0;
883      }
884      else
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      if (this->travelVelocity.z >= 0)
892      {
893        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
894          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
895        else
896          this->travelVelocity.z = 0;
897      }
898      else
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
906      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
907      // and its continuing to slide a bit.
908      Vector oldCoor = this->getRelCoor();
909      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
910      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
911      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
912      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
913
914      if( this->systemFailure() )
915        bForward = bBackward = bLeft = bRight = false;
916
917      if( this->bForward )
918      {
919        //printf("ShipCoorX: %f \n", this->getRelCoor().x);
920        if(this->getRelCoor().x < this->travelDistancePlus.x)
921        {
922          if (this->travelVelocity.x < this->travelSpeed)
923          {
924            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
925          }
926          else
927          {
928            this->travelVelocity.x = this->travelSpeed;
929          }
930        }
931        else
932        {
933          this->travelVelocity.x = 0.0f;
934        }
935      }
936
937      if( this->bBackward )
938      {
939        if(this->getRelCoor().x > this->travelDistanceMinus.x)
940        {
941          if (this->travelVelocity.x > -this->travelSpeed)
942          {
943            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
944          }
945          else
946          {
947            this->travelVelocity.x = -this->travelSpeed;
948          }
949        }
950        else
951        {
952          this->travelVelocity.x = 0.0f;
953        }
954      }
955
956      if( this->bLeft)
957      {
958        if(this->getRelCoor().z > this->travelDistanceMinus.y)
959        {
960          if (this->travelVelocity.z > -this->travelSpeed)
961          {
962            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
963          }
964          else
965          {
966            this->travelVelocity.z = -this->travelSpeed;
967          }
968        }
969        else
970        {
971          this->travelVelocity.z = 0.0f;
972        }
973        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
974      }
975
976      if( this->bRight)
977      {
978        //printf("ShipCoorZ: %f \n", this->getRelCoor().z);
979        if(this->getRelCoor().z < this->travelDistancePlus.y)
980        {
981          if (this->travelVelocity.z < this->travelSpeed)
982          {
983            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
984          }
985          else
986          {
987            this->travelVelocity.z = this->travelSpeed;
988          }
989        }
990        else
991        {
992          this->travelVelocity.z = 0.0f;
993        }
994        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
995      }
996      if (!this->bRight && !this->bLeft)
997      {
998        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
999      }
1000
1001    //normalisation of the vectors (vector sum must be <= travelspeed)
1002    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
1003    if (xzNorm > this->travelSpeed)
1004    {
1005      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
1006      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
1007    }
1008
1009    //this moves camera and ship along the travel path.
1010    if(!this->entityTrack)
1011       this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
1012
1013    break;
1014    }
1015    case Playable::Vertical:
1016    {
1017      if ( !entityTrack || !entityTrack->getActionBox() )
1018      {
1019        break;
1020      }
1021      ActionBox * box = entityTrack->getActionBox();
1022
1023      this->travelVelocity = Vector(0, 0, 0);
1024      float ssss = 50;
1025      if ( this->bForward )
1026      {
1027        this->travelVelocity += Vector( 0, ssss, 0 );
1028      }
1029      if ( this->bBackward )
1030      {
1031        this->travelVelocity += Vector( 0, -ssss, 0 );
1032      }
1033      if ( this->bLeft )
1034      {
1035        this->travelVelocity += Vector( 0, 0, -ssss );
1036      }
1037      if ( this->bRight )
1038      {
1039        this->travelVelocity += Vector( 0, 0, ssss );
1040      }
1041
1042      Vector ds = this->travelVelocity*dt;
1043      Vector newPos = this->getRelCoor() + ds;
1044      if ( newPos.y < -(box->getHeight_2()) || newPos.y > box->getHeight_2() )
1045      {
1046        this->travelVelocity.y = 0;
1047      }
1048      if ( newPos.z > box->getWidth_2() || newPos.z < -(box->getWidth_2()) )
1049      {
1050        this->travelVelocity.z = 0;
1051      }
1052    }
1053      break;
1054    default:
1055      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
1056  }
1057   //set new coordinates calculated through key- events.
1058  this->shiftCoor (this->travelVelocity * dt);
1059}
1060
1061void SpaceShip::setPlaymodeXML(const std::string& playmode)
1062{
1063  this->setPlaymode(Playable::stringToPlaymode(playmode));
1064}
1065
1066/**
1067 * @brief jumps to the next WeaponConfiguration
1068 */
1069void SpaceShip::nextWeaponConfig()
1070{
1071  PRINTF(0)("Requested next weapon config!\n");
1072  this->weaponMan.nextWeaponConfig();
1073  Playable::weaponConfigChanged();
1074}
1075
1076/**
1077 * @brief moves to the last WeaponConfiguration
1078 */
1079void SpaceShip::previousWeaponConfig()
1080{
1081  /*
1082  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
1083  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
1084  */
1085  this->weaponMan.previousWeaponConfig();
1086  Playable::weaponConfigChanged();
1087}
1088
1089void SpaceShip::hit( float damage, WorldEntity* killer)
1090{
1091  this->damage(killer->getDamage(),0);
1092}
1093
1094// void SpaceShip::updateElectronicWidget()
1095// {
1096//   if (this->electronicWidget != NULL)
1097//   { //if it exists already: update it
1098//      this->electronicWidget->setMaximum(this->electronicMax);
1099//      this->electronicWidget->setValue(this->electronicCur);
1100//   }
1101//   else
1102//   { //create the widget
1103//     this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1104//     this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1105//     //this->electronicWidget->setDisplayedName("Electronics:");
1106//     //this->electronicWidget->setSize2D(100,20);
1107//     //this->electronicWidget->setAbsCoor2D(150,200);
1108//     this->updateElectronicWidget();
1109//     if (this->hasPlayer())
1110//       State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1111//   }
1112// }
1113//
1114// void SpaceShip::updateShieldWidget()
1115// {
1116//   if (this->shieldWidget != NULL)
1117//   {
1118//     this->shieldWidget->setMaximum(this->shieldMax);
1119//     this->shieldWidget->setValue(this->shieldCur);;
1120//   }
1121//   else
1122//   {
1123//     this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1124//     this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1125//     //this->shieldWidget->setDisplayedName("Shield:");
1126//     //his->shieldWidget->setSize2D(100,20);
1127//     //this->shieldWidget->setAbsCoor2D(200,200);
1128//     this->updateShieldWidget();
1129//     if (this->hasPlayer())
1130//       State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1131//   }
1132// }
1133
1134
1135void SpaceShip::setCameraDistance(float dist)
1136{
1137  Camera* c = State::getCamera();
1138  c->setViewTopDistance(dist);
1139
1140  if (this->hasPlayer())
1141    this->isTravelDistanceInit = false;
1142}
1143
1144void SpaceShip::setCameraFovy(float fovy)
1145{
1146
1147  Camera* c = State::getCamera();
1148  c->setViewTopFovy(fovy);
1149
1150  if (this->hasPlayer())
1151    this->isTravelDistanceInit = false;
1152}
1153
1154void SpaceShip::updateTravelDistance()
1155{
1156
1157  Camera* c = State::getCamera();
1158
1159  float x = 1.25 * this->actionWidthPercentage * fabsf(c->getAbsCoor().y) * tan(c->getFovy()*M_PI /360.0);
1160  float y = x / c->getAspectRatio() / this->actionWidthPercentage;
1161  //State::getCamera()->setAbsCoor(-5, 1000, 0);
1162
1163
1164  //State::getCamera()->getAbsCoor().print();
1165  //printf("CameraRelCoorY: %f \n", State::getCamera()->getRelCoor().y);
1166
1167  //printf("x: %f, y: %f \n", x, y);
1168  this->travelDistancePlus = Vector2D(y, x);
1169  this->travelDistanceMinus = Vector2D(-y, -x);
1170
1171  State::getPlayer()->hud().setOverlayPercentage(100-int(100*this->actionWidthPercentage));
1172//   PRINTF(0)("TravelDistance has been updated\n");
1173  this->isTravelDistanceInit = true;
1174}
1175
1176void SpaceShip::setActionWidthPercentage(int i)
1177{
1178  if (i>100) i=100;
1179  if (i<0) i=0;
1180  this->actionWidthPercentage = i/100.0;
1181
1182  if (this->hasPlayer())
1183    this->isTravelDistanceInit = false;
1184};
1185
1186
Note: See TracBrowser for help on using the repository browser.