Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

this and that
might be buggy

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