Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

xfer, should work…

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    std::map<int, MountPoint*>::iterator it = this->mountPointMap.begin();
483    for( ;it != this->mountPointMap.end(); it++)
484    {
485      WeaponSlot* ws = dynamic_cast<WeaponSlot*>((*it).second->getMount());
486      if( ws != NULL && ws->isA(WeaponSlot::staticClassID()))
487      {
488        int slot = ws->getWeaponSlot();
489//        int side = ws->getWeaponSide(); //FIXME / REMOVE: is not used// HACK needed for some weapons (left/right)
490        this->getWeaponManager().setSlotPosition(slot, (*it).second->getCenter());
491        this->getWeaponManager().setSlotDirection(slot, ws->getRelDir());
492//         PRINTF(0)("setting slot %i\n", slot);
493//         (*it).second->getCenter().debug();
494      }
495    }
496  this->bInit = true;
497  }
498  printf (" done\n");
499
500  // Playable::tick(time);
501
502 // this->test->tick(time);
503
504  printf("Calling WM tick\n");
505  // Own Tick Setup, as a different fire routine is used on the weapon manager
506  this->weaponMan.tick(time);
507  this->secWeaponMan.tick(time);
508  printf("WM ticks done\n" );
509
510
511  if( this->systemFailure() )
512    bFire = bSecFire = false;
513
514  // fire reqeust/release for primary weapons
515  if( this->bFire)
516    this->weaponMan.fire();
517  else
518    this->weaponMan.releaseFire();
519
520  // fire reqeust/release for secondary weapons
521  if( this->bSecFire)
522    this->secWeaponMan.fire();
523  else
524    this->secWeaponMan.releaseFire();
525
526  // Tracktick
527  if(this->entityTrack)
528    this->entityTrack->tick(time);
529
530
531  // Shield Regeneration and other regular calculations on the ship
532  this->regen(time);
533
534  // Weapon Regeneration and other regular calculations on the ship
535  this->weaponRegen(time);
536
537  // current engine speed output
538  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
539
540  // calculation of maxSpeed and acceleration:
541  this->travelSpeed = this->engineSpeedCur * 5;
542  this->acceleration = this->travelSpeed * 2;
543
544  this->movement(time);
545
546   // TRYING TO FIX PNode.
547  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
548  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
549
550
551  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
552  this->oldPos    = this->getAbsCoor();
553
554//FIXME
555//   this->trail->tick(time);
556//   this->trailL->tick(time);
557//   this->trailR->tick(time);
558
559  if (!this->isTravelDistanceInit)
560  {
561    this->updateTravelDistance();
562    //this->isTravelDistanceInit = true;
563  }
564
565  //orient the spaceship in direction of the mouse
566  /*
567  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
568  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
569    this->setAbsDir( rotQuat);
570  //this->setAbsDirSoft(mouseDir,5);
571  */
572  /*
573  this->shiftCoor(move);
574  */
575
576  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
577
578}
579
580/**
581 * @todo switch statement ??
582 */
583void SpaceShip::process(const Event &event)
584{
585  //Playable::process(event);
586
587  if( event.type == KeyMapper::PEV_LEFT)
588    this->bLeft = event.bPressed;
589  else if( event.type == KeyMapper::PEV_RIGHT)
590  {
591    this->bRight = event.bPressed;
592//     printf("ShipCoors: %f , %f, %f \n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
593  }
594  else if( event.type == KeyMapper::PEV_FORWARD)
595  {
596    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
597
598  }
599  else if( event.type == KeyMapper::PEV_BACKWARD)
600    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
601  else if( event.type == KeyMapper::PEV_FIRE2)
602    this->bSecFire = event.bPressed;
603  else if( event.type == KeyMapper::PEV_FIRE1)
604    this->bFire = event.bPressed;
605  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
606  {
607    this->nextWeaponConfig();
608  }
609  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
610    this->previousWeaponConfig();
611
612  if (!(State::getCamera()->getEventHandling()))
613  {
614    //PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
615    if( event.type == KeyMapper::PEV_VIEW0)
616    {
617      State::getCamera()->setViewMode(Camera::ViewNormal);
618      State::getCameraTargetNode()->setParent(this);
619      State::getCamera()->setParent(this);
620    }
621    else if( event.type == KeyMapper::PEV_VIEW1)
622    {
623      State::getCamera()->setViewMode(Camera::ViewBehind);
624      State::getCameraTargetNode()->setParent(this);
625      State::getCamera()->setParent(this);
626    }
627    else if( event.type == KeyMapper::PEV_VIEW2)
628    {
629      State::getCamera()->setViewMode(Camera::ViewFront);
630      State::getCameraTargetNode()->setParent(this);
631      State::getCamera()->setParent(this);
632    }
633    else if( event.type == KeyMapper::PEV_VIEW3)
634    {
635      State::getCamera()->setViewMode(Camera::ViewLeft);
636      State::getCameraTargetNode()->setParent(this);
637      State::getCamera()->setParent(this);
638    }
639    else if( event.type == KeyMapper::PEV_VIEW4)
640    {
641      State::getCamera()->setViewMode(Camera::ViewRight);
642      State::getCameraTargetNode()->setParent(this);
643      State::getCamera()->setParent(this);
644    }
645    else if( event.type == KeyMapper::PEV_VIEW5)
646    {
647      State::getCamera()->setViewMode(Camera::ViewTop);
648      State::getCameraTargetNode()->setParent(this->travelNode);
649      State::getCamera()->setParent(this->travelNode);
650    }
651  }
652
653
654  /*
655  else if( event.type == EV_MOUSE_MOTION)
656  {
657
658    this->xMouse += event.xRel;
659    this->yMouse += event.yRel;
660  }
661  */
662}
663
664void SpaceShip::destroy( WorldEntity* killer )
665{
666  if(this->hasPlayer())
667    Playable::destroy( killer);
668
669  PRINTF(5)("spaceship destroy\n");
670
671  EmitterNode* node  = NULL;
672  DotEmitter* emitter = NULL;
673  SpriteParticles*  explosionParticles  = NULL;
674
675  explosionParticles = new SpriteParticles(200);
676  explosionParticles->setName("SpaceShipExplosionParticles");
677  explosionParticles->setLifeSpan(.2, .3);
678  explosionParticles->setRadius(0.0, 10.0);
679  explosionParticles->setRadius(.5, 6.0);
680  explosionParticles->setRadius(1.0, 3.0);
681  explosionParticles->setColor(0.0, 1,1,1,.9);
682  explosionParticles->setColor(0.1,  1,1,0,.9);
683  explosionParticles->setColor(0.5, .8,.4,0,.5);
684  explosionParticles->setColor(1.0, .2,.2,.2,.5);
685
686
687  emitter = new DotEmitter( 2000, 70, 360);
688  //emitter->setSpread( 0, M_2_PI);
689  emitter->setEmissionRate( 200.0);
690  //emitter->setEmissionVelocity( 200.0);
691  //emitter->setSystem( explosionParticles);
692  //emitter->setAbsCoor( this->getAbsCoor());
693
694  node  = new EmitterNode( .1f);
695  node->setupParticle( emitter, explosionParticles);
696  node->setAbsDir( this->getAbsDir());
697  node->setVelocity( this->getVelocity() * .9f);
698  node->setAbsCoor( this->getAbsCoor());
699  if( !node->start())
700    PRINTF(0)("Explosion node not correctly started!");
701/*
702  PNode* node          = new PNode();
703  node->setAbsCoor(this->getAbsCoor());
704  Explosion* explosion = new Explosion();
705  explosion->explode( node, Vector(5,5,5));
706*/
707/*
708  if( this->hasPlayer())
709  {
710        this->setAbsCoor(Vector(-10000,10000,10000));
711        this->hide();
712  }
713  else
714  {*/
715  //!< ancient test hack
716//     this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
717  //}
718
719}
720
721void SpaceShip::respawn( )
722{
723  Playable::respawn();
724}
725
726
727void SpaceShip::damage(float pDamage, float eDamage){
728  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
729
730  static float tmp = this->decreaseShield(pDamage);
731  if( tmp > 0)
732  if ( this->decreaseHealth(tmp / 2) > 0)
733    this->destroy(this);
734
735}
736
737
738
739/**
740 * Weapon regeneration
741 * does not use any reactor capacity, as it wouldn't work in a consistent way.
742 */
743void SpaceShip::weaponRegen(float time)
744{
745  float energy = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time ;
746  Weapon* weapon;
747  int weaponCount = 0;
748  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
749    weapon = this->weaponMan.getWeapon(i);
750    if( weapon != NULL) {
751      if (weapon->isActive() )
752      weaponCount++;
753    }
754  }
755
756  if (weaponCount == 0)
757    return;
758  energy *= ( this->weaponMan.getSlotCount() / (float)weaponCount );
759
760  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
761    weapon = this->weaponMan.getWeapon(i);
762    if( weapon != NULL) {
763      if (weapon->isActive() )
764        weapon->increaseEnergy( energy);
765    }
766  }
767  // weaponMan.increaseAmmunition( weapon, energy);
768}
769
770
771void SpaceShip::enterPlaymode(Playable::Playmode playmode){
772  switch(playmode)
773  {
774    case Playable::Full3D:
775      /*
776      if (State::getCameraNode != NULL)
777      {
778        Vector absCoor = this->getAbsCoor();
779        this->setParent(PNode::getNullParent());
780        this->setAbsCoor(absCoor);
781        State::getCameraNode()->setParentSoft(&this->cameraNode);
782        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
783        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
784        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
785
786      }
787      */
788      //break;
789
790      break;
791    case Playable::Horizontal:
792      if (State::getCameraNode != NULL)
793      {
794        this->debugNode(1);
795        this->travelNode->debugNode(1);
796
797        this->travelNode->setAbsCoor(this->getAbsCoor());
798        this->travelNode->updateNode(0.01f);
799
800        this->isTravelDistanceInit = false;
801
802        if(this->entityTrack)
803           this->travelNode->setParent(this->entityTrack->getTrackNode());
804
805        this->setParent(this->travelNode);
806        this->setRelCoor(0,0,0);
807
808        State::getCameraNode()->setParentSoft(this->travelNode);
809        //State::getCameraNode()->setParentSoft(this);
810        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
811        State::getCameraTargetNode()->setParentSoft(this->travelNode);
812        //State::getCameraTargetNode()->setParentSoft(this);
813        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
814        this->setCameraMode(Camera::ViewTop);
815        State::getCamera()->setEventHandling(false);
816        registerEvent(KeyMapper::PEV_VIEW0);
817        registerEvent(KeyMapper::PEV_VIEW1);
818        registerEvent(KeyMapper::PEV_VIEW2);
819        registerEvent(KeyMapper::PEV_VIEW3);
820        registerEvent(KeyMapper::PEV_VIEW4);
821        registerEvent(KeyMapper::PEV_VIEW5);
822
823        State::getCamera()->setParentMode(PNODE_ALL);
824
825        //this->updateTravelDistance();
826
827        this->debugNode(1);
828        this->travelNode->debugNode(1);
829      }
830      break;
831
832
833    case Playable::Vertical:
834    {
835        this->travelNode->setAbsCoor(this->getAbsCoor());
836        this->travelNode->updateNode(0.01f);
837
838        this->isTravelDistanceInit = false;
839
840        if(this->entityTrack)
841           this->travelNode->setParent(this->entityTrack->getTrackNode());
842
843        this->setParent(this->travelNode);
844        this->setRelCoor(0,0,0);
845
846        State::getCameraNode()->setParentSoft(this->travelNode);
847        //State::getCameraNode()->setParentSoft(this);
848        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
849        State::getCameraTargetNode()->setParentSoft(this->travelNode);
850        //State::getCameraTargetNode()->setParentSoft(this);
851        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
852        //this->setCameraMode(Camera::ViewNormal);
853        State::getCamera()->setEventHandling(false);
854
855        PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
856        State::getCamera()->setViewMode(Camera::ViewNormal);
857        State::getCameraTargetNode()->setParent(this);
858        State::getCamera()->setParent(this);
859
860
861        registerEvent(KeyMapper::PEV_VIEW0);
862        registerEvent(KeyMapper::PEV_VIEW1);
863        registerEvent(KeyMapper::PEV_VIEW2);
864        registerEvent(KeyMapper::PEV_VIEW3);
865        registerEvent(KeyMapper::PEV_VIEW4);
866        registerEvent(KeyMapper::PEV_VIEW5);
867
868        State::getCamera()->setParentMode(PNODE_ALL);
869
870      break;
871    }
872
873    default:
874      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
875  }
876}
877
878/**
879 * @brief calculate the velocity
880 * @param time the timeslice since the last frame
881*/
882
883void SpaceShip::movement (float dt)
884{
885  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
886  //the ship doesn't slide too much.
887  float airCoeff = 2.5;
888  float pi = 3.14;
889
890  switch(this->getPlaymode())
891  {
892    case Playable::Horizontal:
893    {
894      // these routines will change the travel movement into zero in a short amout of time, if the player
895      // doesn't press any buttons.
896      if (this->travelVelocity.x >= 0)
897      {
898        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
899          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
900        else
901          this->travelVelocity.x = 0;
902      }
903      else
904      {
905        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
906          this->travelVelocity.x += airCoeff* this->acceleration * dt;
907        else
908          this->travelVelocity.x = 0;
909      }
910      if (this->travelVelocity.z >= 0)
911      {
912        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
913          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
914        else
915          this->travelVelocity.z = 0;
916      }
917      else
918      {
919        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
920          this->travelVelocity.z += airCoeff* this->acceleration * dt;
921        else
922          this->travelVelocity.z = 0;
923      }
924
925      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
926      // and its continuing to slide a bit.
927      Vector oldCoor = this->getRelCoor();
928      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
929      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
930      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
931      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
932
933      if( this->systemFailure() )
934        bForward = bBackward = bLeft = bRight = false;
935
936      if( this->bForward )
937      {
938        //printf("ShipCoorX: %f \n", this->getRelCoor().x);
939        if(this->getRelCoor().x < this->travelDistancePlus.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->bBackward )
957      {
958        if(this->getRelCoor().x > this->travelDistanceMinus.x)
959        {
960          if (this->travelVelocity.x > -this->travelSpeed)
961          {
962            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
963          }
964          else
965          {
966            this->travelVelocity.x = -this->travelSpeed;
967          }
968        }
969        else
970        {
971          this->travelVelocity.x = 0.0f;
972        }
973      }
974
975      if( this->bLeft)
976      {
977        if(this->getRelCoor().z > this->travelDistanceMinus.y)
978        {
979          if (this->travelVelocity.z > -this->travelSpeed)
980          {
981            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
982          }
983          else
984          {
985            this->travelVelocity.z = -this->travelSpeed;
986          }
987        }
988        else
989        {
990          this->travelVelocity.z = 0.0f;
991        }
992        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
993      }
994
995      if( this->bRight)
996      {
997        //printf("ShipCoorZ: %f \n", this->getRelCoor().z);
998        if(this->getRelCoor().z < this->travelDistancePlus.y)
999        {
1000          if (this->travelVelocity.z < this->travelSpeed)
1001          {
1002            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
1003          }
1004          else
1005          {
1006            this->travelVelocity.z = this->travelSpeed;
1007          }
1008        }
1009        else
1010        {
1011          this->travelVelocity.z = 0.0f;
1012        }
1013        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
1014      }
1015      if (!this->bRight && !this->bLeft)
1016      {
1017        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
1018      }
1019
1020    //normalisation of the vectors (vector sum must be <= travelspeed)
1021    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
1022    if (xzNorm > this->travelSpeed)
1023    {
1024      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
1025      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
1026    }
1027
1028    //this moves camera and ship along the travel path.
1029    if(!this->entityTrack)
1030       this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
1031
1032    break;
1033    }
1034    case Playable::Vertical:
1035    {
1036      if ( !entityTrack || !entityTrack->getActionBox() )
1037      {
1038        break;
1039      }
1040      ActionBox * box = entityTrack->getActionBox();
1041
1042      this->travelVelocity = Vector(0, 0, 0);
1043      float ssss = 50;
1044      if ( this->bForward )
1045      {
1046        this->travelVelocity += Vector( 0, ssss, 0 );
1047      }
1048      if ( this->bBackward )
1049      {
1050        this->travelVelocity += Vector( 0, -ssss, 0 );
1051      }
1052      if ( this->bLeft )
1053      {
1054        this->travelVelocity += Vector( 0, 0, -ssss );
1055      }
1056      if ( this->bRight )
1057      {
1058        this->travelVelocity += Vector( 0, 0, ssss );
1059      }
1060
1061      Vector ds = this->travelVelocity*dt;
1062      Vector newPos = this->getRelCoor() + ds;
1063      if ( newPos.y < -(box->getHeight_2()) || newPos.y > box->getHeight_2() )
1064      {
1065        this->travelVelocity.y = 0;
1066      }
1067      if ( newPos.z > box->getWidth_2() || newPos.z < -(box->getWidth_2()) )
1068      {
1069        this->travelVelocity.z = 0;
1070      }
1071    }
1072      break;
1073    default:
1074      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
1075  }
1076   //set new coordinates calculated through key- events.
1077  this->shiftCoor (this->travelVelocity * dt);
1078}
1079
1080void SpaceShip::setPlaymodeXML(const std::string& playmode)
1081{
1082  this->setPlaymode(Playable::stringToPlaymode(playmode));
1083}
1084
1085/**
1086 * @brief jumps to the next WeaponConfiguration
1087 */
1088void SpaceShip::nextWeaponConfig()
1089{
1090  PRINTF(0)("Requested next weapon config!\n");
1091  this->weaponMan.nextWeaponConfig();
1092  Playable::weaponConfigChanged();
1093}
1094
1095/**
1096 * @brief moves to the last WeaponConfiguration
1097 */
1098void SpaceShip::previousWeaponConfig()
1099{
1100  /*
1101  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
1102  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
1103  */
1104  this->weaponMan.previousWeaponConfig();
1105  Playable::weaponConfigChanged();
1106}
1107
1108void SpaceShip::hit( float damage, WorldEntity* killer)
1109{
1110  this->damage(killer->getDamage(),0);
1111}
1112
1113// void SpaceShip::updateElectronicWidget()
1114// {
1115//   if (this->electronicWidget != NULL)
1116//   { //if it exists already: update it
1117//      this->electronicWidget->setMaximum(this->electronicMax);
1118//      this->electronicWidget->setValue(this->electronicCur);
1119//   }
1120//   else
1121//   { //create the widget
1122//     this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1123//     this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1124//     //this->electronicWidget->setDisplayedName("Electronics:");
1125//     //this->electronicWidget->setSize2D(100,20);
1126//     //this->electronicWidget->setAbsCoor2D(150,200);
1127//     this->updateElectronicWidget();
1128//     if (this->hasPlayer())
1129//       State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1130//   }
1131// }
1132//
1133// void SpaceShip::updateShieldWidget()
1134// {
1135//   if (this->shieldWidget != NULL)
1136//   {
1137//     this->shieldWidget->setMaximum(this->shieldMax);
1138//     this->shieldWidget->setValue(this->shieldCur);;
1139//   }
1140//   else
1141//   {
1142//     this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1143//     this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1144//     //this->shieldWidget->setDisplayedName("Shield:");
1145//     //his->shieldWidget->setSize2D(100,20);
1146//     //this->shieldWidget->setAbsCoor2D(200,200);
1147//     this->updateShieldWidget();
1148//     if (this->hasPlayer())
1149//       State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1150//   }
1151// }
1152
1153void SpaceShip::setCameraDistance(float dist)
1154{
1155  Camera* c = State::getCamera();
1156  c->setViewTopDistance(dist);
1157
1158  if (this->hasPlayer())
1159    this->isTravelDistanceInit = false;
1160}
1161
1162void SpaceShip::setCameraFovy(float fovy)
1163{
1164
1165  Camera* c = State::getCamera();
1166  c->setViewTopFovy(fovy);
1167
1168  if (this->hasPlayer())
1169    this->isTravelDistanceInit = false;
1170}
1171
1172void SpaceShip::updateTravelDistance()
1173{
1174
1175  Camera* c = State::getCamera();
1176
1177  float x = 1.25 * this->actionWidthPercentage * fabsf(c->getAbsCoor().y) * tan(c->getFovy()*M_PI /360.0);
1178  float y = x / c->getAspectRatio() / this->actionWidthPercentage;
1179  //State::getCamera()->setAbsCoor(-5, 1000, 0);
1180
1181
1182  //State::getCamera()->getAbsCoor().print();
1183  //printf("CameraRelCoorY: %f \n", State::getCamera()->getRelCoor().y);
1184
1185  //printf("x: %f, y: %f \n", x, y);
1186  this->travelDistancePlus = Vector2D(y, x);
1187  this->travelDistanceMinus = Vector2D(-y, -x);
1188
1189  State::getPlayer()->hud().setOverlayPercentage(100-int(100*this->actionWidthPercentage));
1190//   PRINTF(0)("TravelDistance has been updated\n");
1191  this->isTravelDistanceInit = true;
1192}
1193
1194void SpaceShip::setActionWidthPercentage(int i)
1195{
1196  if (i>100) i=100;
1197  if (i<0) i=0;
1198  this->actionWidthPercentage = i/100.0;
1199
1200  if (this->hasPlayer())
1201    this->isTravelDistanceInit = false;
1202};
1203
1204
Note: See TracBrowser for help on using the repository browser.