Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some bugfix (or not?)

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