Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc

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

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

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