Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

xfer, WFM…

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