Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/space_ships/space_ship.cc @ 10286

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

code cleanup, rebuilt spikeball

File size: 32.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
24#include "weapons/test_gun.h"
[9970]25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
[10023]28#include "weapons/swarm_launcher.h"
[10170]29#include "weapons/spike_launcher.h"
[10196]30#include "weapons/spike_thrower.h"
[9970]31#include "weapons/boomerang_gun.h"
[6868]32#include "weapons/turret.h"
33#include "weapons/cannon.h"
34
[10120]35#include "elements/glgui_energywidgetvertical.h"
[10230]36#include "glgui_bar.h"
[10120]37
[9869]38#include "particles/dot_emitter.h"
[10073]39#include "particles/emitter_node.h"
[9869]40#include "particles/sprite_particles.h"
[10081]41#include "effects/trail.h"
[6868]42
[10154]43#include "effects/wobblegrid.h"
44
[7193]45#include "util/loading/factory.h"
[6868]46#include "key_mapper.h"
47
48#include "network_game_manager.h"
[8708]49#include "shared_network_data.h"
[6868]50
51#include "power_ups/weapon_power_up.h"
52#include "power_ups/param_power_up.h"
53
54#include "graphics_engine.h"
55
56#include "plane.h"
57
58#include "state.h"
59#include "player.h"
[10237]60#include "camera.h"
[6868]61
[10113]62
[7193]63#include "util/loading/load_param.h"
[10020]64#include "time.h"
[6868]65
[10096]66#include "track/track.h"
[7056]67
[10096]68
[6868]69// #include "lib/gui/gl_gui/glgui_bar.h"
70// #include "lib/gui/gl_gui/glgui_pushbutton.h"
71
72
[9869]73#include "class_id_DEPRECATED.h"
74ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP);
75CREATE_FACTORY(SpaceShip);
[9406]76
[9061]77#include "script_class.h"
[9869]78CREATE_SCRIPTABLE_CLASS(SpaceShip,
79                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
80                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
81                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
82                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
83                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
84                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
85                        //Coordinates
86                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
87                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
88                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
89                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
[10017]90                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
[9061]91                       );
[6868]92
93/**
94 *  destructs the spaceship, deletes alocated memory
95 */
96SpaceShip::~SpaceShip ()
97{
[6986]98  this->setPlayer(NULL);
[6868]99}
100
101/**
102 * loads a Spaceships information from a specified file.
103 * @param fileName the name of the File to load the spaceship from (absolute path)
104 */
[7221]105SpaceShip::SpaceShip(const std::string& fileName)
[10019]106    : secWeaponMan(this) //,
107    //supportedPlaymodes(Playable::Vertical) ,
108    //playmode(Playable::Vertical)
[6868]109{
110  this->init();
111  TiXmlDocument doc(fileName);
112
113  if(!doc.LoadFile())
114  {
[7221]115    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
[6868]116    return;
117  }
118
119  this->loadParams(doc.RootElement());
120}
121
122/**
123 *  creates a new Spaceship from Xml Data
124 * @param root the xml element containing spaceship data
125
126   @todo add more parameters to load
127*/
128SpaceShip::SpaceShip(const TiXmlElement* root)
[10019]129    : secWeaponMan(this) //,
130    //supportedPlaymodes(Playable::Vertical) ,
131    //playmode(Playable::Vertical)
[6868]132{
133  this->init();
[10081]134  //this->setParentMode(PNODE_REPARENT_DELETE_CHILDREN);
[6868]135  if (root != NULL)
136    this->loadParams(root);
137
138}
139
140
141/**
142 * initializes a Spaceship
143 */
144void SpaceShip::init()
145{
[9998]146
[10022]147  srand(time(0));   //initialize Random Nomber Generator
148
[9869]149  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
150  this->registerObject(this, SpaceShip::_objectList);
[6868]151  PRINTF(4)("SPACESHIP INIT\n");
[10121]152  this->weaponMan.setParentEntity( this);
[6868]153  //weapons:
[10098]154 
[9996]155  Weapon* wpRight1 = new LightBlaster ();
[10073]156  wpRight1->setName( "LightBlaster");
[9996]157  Weapon* wpLeft1 = new LightBlaster ();
[10073]158  wpLeft1->setName( "LightBlaster");
[6868]159
[9996]160  Weapon* wpRight2 = new MediumBlaster ();
[10073]161  wpRight2->setName( "MediumBlaster");
[9996]162  Weapon* wpLeft2 = new MediumBlaster ();
[10073]163  wpLeft2->setName( "MediumBlaster");
[10252]164
[10152]165  Weapon* wpRight3 = new HeavyBlaster (1);
[10073]166  wpRight3->setName( "HeavyBlaster");
[10152]167  Weapon* wpLeft3 = new HeavyBlaster (0);
[10073]168  wpLeft3->setName( "HeavyBlaster");
[9996]169
[10173]170  Weapon* cannon = new SwarmLauncher();
171  cannon->setName( "SwarmLauncher");
[6868]172
[10252]173  Weapon* spike = new SpikeThrower();
174  spike->setName( "SpikeThrower" );
[9961]175
[10261]176//  Weapon* spike2 = new SpikeLauncher();
177//  spike2->setName( "SpikeLauncher" );
[10196]178
[10098]179  this->weaponMan.addWeapon( wpLeft1, 0, 0);
180  this->weaponMan.addWeapon( wpRight1, 0, 1);
[9998]181
[10121]182  this->weaponMan.addWeapon( wpLeft2, 1, 2);
183  this->weaponMan.addWeapon( wpRight2, 1, 3);
[9961]184
[10121]185  this->weaponMan.addWeapon( wpLeft3, 2, 4);
186  this->weaponMan.addWeapon( wpRight3, 2, 5);
[10113]187
[10152]188  this->weaponMan.addWeapon( wpLeft1, 3, 0);
189  this->weaponMan.addWeapon( wpRight1, 3, 1);
190
191  this->weaponMan.addWeapon( wpLeft2, 3, 2);
192  this->weaponMan.addWeapon( wpRight2, 3, 3);
193
194  this->weaponMan.addWeapon( wpLeft3, 3, 4);
195  this->weaponMan.addWeapon( wpRight3, 3, 5);
196
[10121]197  this->secWeaponMan.addWeapon( cannon, 0, 0);
[10252]198  this->secWeaponMan.addWeapon( spike, 1, 1);
[10261]199//  this->secWeaponMan.addWeapon( spike2, 2, 2);
[9998]200
[10252]201
[10152]202  this->weaponMan.changeWeaponConfig(3);
[10271]203  this->secWeaponMan.changeWeaponConfig(1);
[10113]204
[10188]205  curWeaponPrimary    = 3;
[10286]206  curWeaponSecondary  = 0;
[9965]207
208  Playable::weaponConfigChanged();
209
[9970]210  reactorOutput     = 10;
[9965]211
[9970]212  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
213  engineSpeedBase   = 5;
214  shieldRegen       = 2;
215
216  shieldEnergyShare = 0.3;
217  weaponEnergyShare = 0.3;
218  engineEnergyShare = 0.4;
219
220  shieldCur         = 20;
221  shieldMax         = 100;
[10252]222  shieldTH          = .2 * shieldMax;   // shield power must be 20% before shield kicks in again
[9970]223
[10119]224  this->setHealth( 20);
225  this->setHealthMax( 100);
226
[10024]227  electronicCur = 50;
[10022]228  electronicMax = 50;
[10020]229  electronicRegen   = 3;
230  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
231
[6868]232
[9970]233  this->loadModel("models/ships/mantawing.obj");
[10017]234  //this->setVisibiliy(false);
[6868]235
[10017]236  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
237 
[6868]238
[9970]239  this->setHealthMax(shieldMax);
240  this->setHealth(shieldCur);
[6868]241
[10017]242  this->travelNode = new PNode();
243
244  // camera - issue
245  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
246  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
247
[10120]248  // widget handling
[10139]249  /*
[10120]250  this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
251  this->electronicWidget->setDisplayedName(std::string(this->getClassName()) + " Electronics:");
252  this->electronicWidget->setSize2D(30,400);
253  this->electronicWidget->setAbsCoor2D(150,200);
254  this->electronicWidget->shiftDir2D(270);
255  this->updateElectronicWidget();
256  this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
257  this->shieldWidget->setDisplayedName(std::string(this->getClassName()) + " Shield:");
258  this->shieldWidget->setSize2D(30,400);
259  this->shieldWidget->setAbsCoor2D(200,200);
260  this->shieldWidget->shiftDir2D(270);
261  this->updateShieldWidget();
262  if (this->hasPlayer())
263  {
264    State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
265    State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
266  }
[10139]267  */
268  this->electronicWidget = NULL;
269  this->shieldWidget = NULL;
[6868]270
271  //add events to the eventlist
[6997]272  registerEvent(KeyMapper::PEV_FORWARD);
273  registerEvent(KeyMapper::PEV_BACKWARD);
[6868]274  registerEvent(KeyMapper::PEV_LEFT);
275  registerEvent(KeyMapper::PEV_RIGHT);
276  //registerEvent(SDLK_q);
277  //registerEvent(SDLK_e);
278  registerEvent(KeyMapper::PEV_FIRE1);
[9958]279  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
[6868]280  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
281  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
282  //registerEvent(SDLK_PAGEUP);
283  //registerEvent(SDLK_PAGEDOWN);
284  registerEvent(EV_MOUSE_MOTION);
285
[10073]286  this->weaponMan.setParentEntity( this);
287  this->secWeaponMan.setParentEntity( this);
288
[9965]289  this->weaponMan.setSlotCount(6);
[6868]290
[10152]291  this->weaponMan.setSlotPosition(0, Vector(0.0, 0, -3.0));
[9965]292  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]293
[10152]294  this->weaponMan.setSlotPosition(1, Vector(0.0, 0, 3.0));
[9965]295  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]296
[10286]297  this->weaponMan.setSlotPosition(2, Vector(1.0, 0, -1.5));
[9965]298  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[6868]299
[10286]300  this->weaponMan.setSlotPosition(3, Vector(1.0, 0, 1.5));
[9965]301  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[6868]302
[10133]303  this->weaponMan.setSlotPosition(4, Vector(1.5, 0, .5));
[9965]304  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
[6868]305
[10133]306  this->weaponMan.setSlotPosition(5, Vector(1.5, 0, -.5));
[9965]307  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9961]308
309  this->secWeaponMan.setSlotCount(6);
310
[10073]311  this->secWeaponMan.setSlotPosition(0, Vector(1.5, 0, 0));
[9961]312  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
313
[10073]314  this->secWeaponMan.setSlotPosition(1, Vector(2.6, 0, 3.0));
[9961]315  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
316
[10073]317  this->secWeaponMan.setSlotPosition(2, Vector(1.5, 0, -.5));
[9961]318  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
319
[10073]320  this->secWeaponMan.setSlotPosition(3, Vector(1.5, 0, .5));
[9961]321  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
322
[10073]323  this->secWeaponMan.setSlotPosition(4, Vector(1.5, 0, .5));
[9961]324  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
325
[10073]326  this->secWeaponMan.setSlotPosition(5, Vector(1.5, 0, -.5));
[9961]327  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9965]328
[6868]329
[9965]330  this->weaponMan.getFixedTarget()->setParent(this);
331  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]332
[9998]333 
[9961]334  this->secWeaponMan.getFixedTarget()->setParent(this);
335  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]336  this->secWeaponMan.setRotationSpeed(0);
[6868]337
[9965]338  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
[9961]339
[6868]340
[10017]341  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
342  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
343  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
344  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
345  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
346  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
[10023]347  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
[10017]348  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
349
350  //this->airFriction = 0.5f;
[10019]351  this->travelDistancePlus = Vector2D(38.0, 43.0);
352  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
[10017]353  this->cameraSpeed = 40;
[10019]354  this->cameraLook = 0.0f;
355  //this->airFriction = 0.0f;
[10017]356
[10020]357  srand(time(0));  //initaialize RNG
358
[10017]359  this->travelNode->debugDraw();
[10032]360
361  this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical);
[10081]362
[10261]363  /// FIXME
364  this->trail = new Trail( 5, 10, .2, this);
[10081]365  this->trail->setTexture( "maps/engine.png");
366
[10261]367  this->trailL = new Trail( 5, 10, .2, this);
[10081]368  this->trailL->setTexture( "maps/engine.png");
369
[10261]370  this->trailR = new Trail( 5, 10, .2, this);
[10081]371  this->trailR->setTexture( "maps/engine.png");
[10098]372
[10113]373
[10098]374  this->toList(OM_GROUP_00);
[10154]375
[10155]376  //FIXME Just testaddition to show the wobblegrid
[10261]377/*
[10156]378  this->test  = new Wobblegrid(5);
[10159]379  test->setTexture("maps/blub.png");
[10154]380 
381  test->setAbsCoor( this->getAbsCoor() + Vector(0, 2, 0));
382  test->setParent( this);
[10261]383*/
[10154]384 
[6868]385}
386
387
388/**
389 * loads the Settings of a SpaceShip from an XML-element.
390 * @param root the XML-element to load the Spaceship's properties from
391 */
392void SpaceShip::loadParams(const TiXmlElement* root)
393{
394  Playable::loadParams(root);
[10032]395
396  LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML);
[10270]397  LoadParam(root, "cameraDistance", this, SpaceShip, setCameraDistance);
398  LoadParam(root, "cameraFovy", this, SpaceShip, setCameraFovy);
[10281]399
400  State::getCamera()->setViewMode(Camera::ViewTop);
[6868]401}
402
[10032]403
[10017]404void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
[7056]405{
[10032]406  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
[7056]407}
[6868]408
[7085]409void SpaceShip::reset()
410{
[10017]411  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
[7056]412
[10017]413  //xMouse = yMouse = 0;
[7085]414
415  this->setHealth(80);
416  this->velocity = Vector(0.0, 0.0, 0.0);
417}
418
419
[6868]420void SpaceShip::enter()
421{
[10004]422  this->secWeaponMan.showCrosshair();
[10095]423  this->toList( OM_GROUP_01 );
[10270]424  State::getPlayer()->hud().setRadarCenterNode(this->travelNode);
[10233]425  State::getPlayer()->hud().setOverlayActive(true);
[10194]426  //dynamic_cast <OrxGui::GLGuiEnergyWidgetVertical*> (State::getPlayer()->hud().getArmorWidget())->setDisplayedName("Armor");
[10004]427  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
[10019]428  //this->attachCamera();
[10032]429 // this->setPlaymode(Playable::Horizontal);
[6868]430}
431
432void SpaceShip::leave()
433{
[10004]434  this->secWeaponMan.hideCrosshair();
[10095]435  this->toList( OM_GROUP_00);
[10233]436  State::getPlayer()->hud().setOverlayActive(false);
[10237]437  State::getCamera()->setEventHandling(true);
[10270]438  State::getPlayer()->hud().setRadarCenterNode(NULL);
[10004]439  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
[10019]440  //this->detachCamera();
[6868]441}
442
443
444/**
445 *  effect that occurs after the SpaceShip is spawned
446*/
447void SpaceShip::postSpawn ()
448{
[10261]449  if(this->hasPlayer())
450    Playable::postSpawn();
451
[6868]452  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
453}
454
455/**
456 *  the action occuring if the spaceship left the game
457*/
458void SpaceShip::leftWorld ()
[10073]459{
460
[10064]461}
[6868]462
463WorldEntity* ref = NULL;
464
465/**
466 *  draws the spaceship after transforming it.
467*/
468void SpaceShip::draw () const
469{
470  WorldEntity::draw();
471
[10081]472  glMatrixMode(GL_MODELVIEW);
473  glPushMatrix();
474
475  float matrix[4][4];
476  glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
477  this->getAbsDir().matrix (matrix);
478  glMultMatrixf((float*)matrix);
479  //glScalef(2.0, 2.0, 2.0);  // no double rescale
[10282]480        // FIXME
[10081]481  this->trail->draw();
482 
483  glTranslatef(0,0,-.5);
484  this->trailL->draw();
485
486  glTranslatef(0,0,1);
487  this->trailR->draw();
[10282]488
[10081]489  glPopMatrix();
[6868]490  //this->debug(0);
491}
492
493/**
494 *  the function called for each passing timeSnap
495 * @param time The timespan passed since last update
496*/
497void SpaceShip::tick (float time)
498{
[10261]499  // Playable::tick(time);
500
501 // this->test->tick(time);
502
[9961]503  // Own Tick Setup, as a different fire routine is used on the weapon manager
[9965]504  this->weaponMan.tick(time);
[9961]505  this->secWeaponMan.tick(time);
506
[10020]507  if( this->systemFailure() )
508    bFire = bSecFire = false;
509
[10037]510  // fire reqeust/release for primary weapons
[9965]511  if( this->bFire)
512    this->weaponMan.fire();
[10036]513  else
514    this->weaponMan.releaseFire();
515
[10037]516  // fire reqeust/release for secondary weapons
[9961]517  if( this->bSecFire)
518    this->secWeaponMan.fire();
[10036]519  else
520    this->secWeaponMan.releaseFire();
[10096]521   
522  // Tracktick
523  if(this->entityTrack)
524    this->entityTrack->tick(time);
[9961]525
[9970]526
527  // Shield Regeneration and other regular calculations on the ship
528  this->regen(time);
529
530  // Weapon Regeneration and other regular calculations on the ship
[9965]531  this->weaponRegen(time);
532
[9970]533  // current engine speed output
534  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
535
[10043]536  // calculation of maxSpeed and acceleration:
537  this->travelSpeed = this->engineSpeedCur * 5;
538  this->acceleration = this->travelSpeed * 2;
[9970]539
[10017]540  this->movement(time);
541
542   // TRYING TO FIX PNode.
543  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
544  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
545
[10095]546 
547  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
548  this->oldPos    = this->getAbsCoor();
549
[10261]550//FIXME
[10081]551  this->trail->tick(time);
552  this->trailL->tick(time);
553  this->trailR->tick(time);
[10095]554
[6868]555  //orient the spaceship in direction of the mouse
[10017]556  /*
[9869]557  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
558  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6868]559    this->setAbsDir( rotQuat);
[9869]560  //this->setAbsDirSoft(mouseDir,5);
[10017]561  */
562  /*
[6868]563  this->shiftCoor(move);
[10017]564  */
[6868]565
[9869]566  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
[6868]567
568}
569
570/**
571 * @todo switch statement ??
572 */
573void SpaceShip::process(const Event &event)
574{
[9961]575  //Playable::process(event);
[10113]576 
[6868]577  if( event.type == KeyMapper::PEV_LEFT)
[10017]578    this->bLeft = event.bPressed;
[6868]579  else if( event.type == KeyMapper::PEV_RIGHT)
[10017]580    this->bRight = event.bPressed;
[6997]581  else if( event.type == KeyMapper::PEV_FORWARD)
[10017]582    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6997]583  else if( event.type == KeyMapper::PEV_BACKWARD)
[10017]584    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[9961]585  else if( event.type == KeyMapper::PEV_FIRE2)
586    this->bSecFire = event.bPressed;
[9958]587  else if( event.type == KeyMapper::PEV_FIRE1)
[9965]588    this->bFire = event.bPressed;
[10113]589  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
590  {
591    this->nextWeaponConfig();
592  }
593  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
594    this->previousWeaponConfig();
[10017]595
[10237]596  if (!(State::getCamera()->getEventHandling()))
597  {
598    if( event.type == KeyMapper::PEV_VIEW0)
599    {
600      State::getCamera()->setViewMode(Camera::ViewNormal);
601      State::getCameraTargetNode()->setParent(this);
602      State::getCamera()->setParent(this);
603    }
604    else if( event.type == KeyMapper::PEV_VIEW1)
605    {
606      State::getCamera()->setViewMode(Camera::ViewBehind);
607      State::getCameraTargetNode()->setParent(this);
608      State::getCamera()->setParent(this);
609    }
610    else if( event.type == KeyMapper::PEV_VIEW2)
611    {
612      State::getCamera()->setViewMode(Camera::ViewFront);
613      State::getCameraTargetNode()->setParent(this);
614      State::getCamera()->setParent(this);
615    }
616    else if( event.type == KeyMapper::PEV_VIEW3)
617    {
618      State::getCamera()->setViewMode(Camera::ViewLeft);
619      State::getCameraTargetNode()->setParent(this);
620      State::getCamera()->setParent(this);
621    }
622    else if( event.type == KeyMapper::PEV_VIEW4)
623    {
624      State::getCamera()->setViewMode(Camera::ViewRight);
625      State::getCameraTargetNode()->setParent(this);
626      State::getCamera()->setParent(this);
627    }
628    else if( event.type == KeyMapper::PEV_VIEW5)
629    {
630      State::getCamera()->setViewMode(Camera::ViewTop);
631      State::getCameraTargetNode()->setParent(this->travelNode);
632      State::getCamera()->setParent(this->travelNode);
633    }
634  }
[10017]635
[10237]636
[10017]637  /*
[6868]638  else if( event.type == EV_MOUSE_MOTION)
639  {
[9961]640
[6868]641    this->xMouse += event.xRel;
642    this->yMouse += event.yRel;
643  }
[10017]644  */
[6868]645}
646
[9235]647void SpaceShip::destroy( WorldEntity* killer )
[9008]648{
[10261]649  if(this->hasPlayer())
650    Playable::destroy( killer);
651
[10107]652  PRINTF(5)("spaceship destroy\n");
[10261]653
[10073]654  EmitterNode* node  = NULL;
655  DotEmitter* emitter = NULL;
656  SpriteParticles*  explosionParticles  = NULL;
[10064]657
[10107]658  explosionParticles = new SpriteParticles(200);
659  explosionParticles->setName("SpaceShipExplosionParticles");
660  explosionParticles->setLifeSpan(.2, .3);
[10073]661  explosionParticles->setRadius(0.0, 10.0);
662  explosionParticles->setRadius(.5, 6.0);
663  explosionParticles->setRadius(1.0, 3.0);
[10098]664  explosionParticles->setColor(0.0, 1,1,1,.9);
665  explosionParticles->setColor(0.1,  1,1,0,.9);
666  explosionParticles->setColor(0.5, .8,.4,0,.5);
[10107]667  explosionParticles->setColor(1.0, .2,.2,.2,.5);
[10064]668
[10073]669 
[10107]670  emitter = new DotEmitter( 2000, 70, 360);
671  //emitter->setSpread( 0, M_2_PI);
672  emitter->setEmissionRate( 200.0);
[10098]673  //emitter->setEmissionVelocity( 200.0);
[10107]674  //emitter->setSystem( explosionParticles);
675  //emitter->setAbsCoor( this->getAbsCoor());
[10064]676
[10107]677  node  = new EmitterNode( .1f);
[10073]678  node->setupParticle( emitter, explosionParticles);
[10107]679  node->setAbsDir( this->getAbsDir());
680  node->setVelocity( this->getVelocity() * .9f);
[10073]681  node->setAbsCoor( this->getAbsCoor());
[10107]682  if( !node->start())
683    PRINTF(0)("Explosion node not correctly started!");
[10095]684/*
[10081]685  PNode* node          = new PNode();
686  node->setAbsCoor(this->getAbsCoor());
687  Explosion* explosion = new Explosion();
688  explosion->explode( node, Vector(5,5,5));
[10095]689*/
[10261]690/*
[10107]691  if( this->hasPlayer())
692  {
693        this->setAbsCoor(Vector(-10000,10000,10000));
694        this->hide();
695  }
696  else
[10261]697  {*/
[10113]698    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
[10261]699  //}
[10107]700
[9008]701}
[6868]702
[9008]703void SpaceShip::respawn( )
704{
[10261]705  Playable::respawn();
[9008]706}
[6868]707
708
[9957]709void SpaceShip::damage(float pDamage, float eDamage){
[10117]710  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
[10081]711
[10064]712  if( this->shieldActive) {
[9950]713    if( this->shieldCur > pDamage) {
714      this->shieldCur = this->shieldCur - pDamage;
715    }
716    else { // shield <= pDamage
[9987]717      this->shieldCur -=pDamage;
718      this->shieldActive = false; //shield collapses
[10081]719      pDamage += this->shieldCur;
[9950]720      if( !this->shieldActive) {
[9957]721        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
[9953]722        this->electronicCur -= eDamage;
[9950]723      }
724    }
725  }
726  else {
727    this->armorCur = this->armorCur - pDamage;
728    this->electronicCur = this->electronicCur - eDamage;
729  }
730  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
[10064]731    this->destroy(this);
[10119]732
[10120]733  updateElectronicWidget();
734  updateShieldWidget();
735
[10119]736  this->setHealth( this->armorCur);
[9950]737}
738
[9987]739
[9957]740void SpaceShip::regen(float time){
741  float tmp;
[9963]742  if (this->armorCur != this->armorMax || this->armorRegen != 0){
743    tmp = this->armorCur + this->armorRegen * time;
744    if ( tmp > electronicMax)
745      this->armorCur = this->armorMax;
746    else
747      this->armorCur = tmp;
748  }
749  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
[9970]750    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
[9957]751    if( tmp > shieldMax)
[9953]752      this->shieldCur = this->shieldMax;
753    else
[9957]754      this->shieldCur = tmp;
755    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
[10120]756
757    updateShieldWidget();
[9953]758  }
[9970]759
760  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
761
[9963]762  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
[9957]763    tmp = this->electronicCur + this->electronicRegen * time;
764    if ( tmp > electronicMax)
[9953]765      this->electronicCur = this->electronicMax;
766    else
[9957]767      this->electronicCur = tmp;
[10120]768
769    updateElectronicWidget();
[9953]770  }
[10120]771
[9950]772}
[9965]773
[9987]774
[9965]775/**
776 * Weapon regeneration
777 * does not use any reactor capacity, as it wouldn't work in a consistent way.
778 */
779void SpaceShip::weaponRegen(float time)
780{
781  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
782  Weapon* weapon;
[9987]783  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
[9965]784  {
785    weapon = this->weaponMan.getWeapon(i);
786    if( weapon != NULL && weapon->isActive())
787    {
788      weapon->increaseEnergy( energy);
789    }
790
791  }
792  // weaponMan.increaseAmmunition( weapon, energy);
793}
[9975]794
[9987]795
[9975]796void SpaceShip::enterPlaymode(Playable::Playmode playmode)
797{
[10017]798  switch(playmode)
799  {
800    case Playable::Full3D:
801      /*
802      if (State::getCameraNode != NULL)
803      {
804        Vector absCoor = this->getAbsCoor();
805        this->setParent(PNode::getNullParent());
806        this->setAbsCoor(absCoor);
807        State::getCameraNode()->setParentSoft(&this->cameraNode);
808        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
809        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
810        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
811
812      }
813      */
814      //break;
815
[10032]816      break;
817    case Playable::Horizontal:
[10017]818      if (State::getCameraNode != NULL)
819      {
820        this->debugNode(1);
821        this->travelNode->debugNode(1);
822
823        this->travelNode->setAbsCoor(this->getAbsCoor());
824        this->travelNode->updateNode(0.01f);
[10284]825       
[10098]826        this->setParent(this->travelNode);
[10284]827       
828        // If track exists put travelNode on trackNode of track
829        if (this->entityTrack)
830           this->travelNode->setParent(this->entityTrack->getTrackNode());
831           
832       
833       
[10017]834        this->setRelCoor(0,0,0);
835
[10098]836        State::getCameraNode()->setParentSoft(this->travelNode);
[10284]837        //State::getCameraNode()->setParentSoft(this->entityTrack->getTrackNode());
[10237]838        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
[10098]839        State::getCameraTargetNode()->setParentSoft(this->travelNode);
[10284]840        //State::getCameraTargetNode()->setParentSoft(this->entityTrack->getTrackNode());
[10237]841        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
842        this->setCameraMode(Camera::ViewTop);
843        State::getCamera()->setEventHandling(false);
844        registerEvent(KeyMapper::PEV_VIEW0);
845        registerEvent(KeyMapper::PEV_VIEW1);
846        registerEvent(KeyMapper::PEV_VIEW2);
847        registerEvent(KeyMapper::PEV_VIEW3);
848        registerEvent(KeyMapper::PEV_VIEW4);
849        registerEvent(KeyMapper::PEV_VIEW5);
[10017]850
[10237]851        State::getCamera()->setParentMode(PNODE_MOVEMENT);
852
[10017]853        this->debugNode(1);
854        this->travelNode->debugNode(1);
855      }
856      break;
857
858    default:
859      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
860  }
[9975]861}
862
[10017]863/**
864 * @brief calculate the velocity
865 * @param time the timeslice since the last frame
866*/
[9987]867
[9975]868void SpaceShip::movement (float dt)
869{
[10019]870  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
871  //the ship doesn't slide too much.
872  float airCoeff = 2.5;
873  float pi = 3.14;
[10098]874 
[10017]875
[10032]876  switch(this->getPlaymode())
[10017]877  {
[10032]878    case Playable::Horizontal:
[10017]879    {
[10032]880      // these routines will change the travel movement into zero in a short amout of time, if the player
881      // doesn't press any buttons.
[10098]882      if (this->travelVelocity.x >= 0)
[10017]883      {
[10098]884        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
885          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
[10032]886        else
[10098]887          this->travelVelocity.x = 0;
[10017]888      }
889      else
890      {
[10098]891        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
892          this->travelVelocity.x += airCoeff* this->acceleration * dt;
[10032]893        else
[10098]894          this->travelVelocity.x = 0;
[10017]895      }
[10098]896      if (this->travelVelocity.z >= 0)
[10017]897      {
[10098]898        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
899          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
[10032]900        else
[10098]901          this->travelVelocity.z = 0;
[10017]902      }
903      else
904      {
[10098]905        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
906          this->travelVelocity.z += airCoeff* this->acceleration * dt;
[10032]907        else
[10098]908          this->travelVelocity.z = 0;
[10017]909      }
[10032]910   
911      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
912      // and its continuing to slide a bit.
913      Vector oldCoor = this->getRelCoor();
914      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
915      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
916      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
917      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
918   
919      if( this->systemFailure() )
920        bForward = bBackward = bLeft = bRight = false;
921   
922      if( this->bForward )
[10019]923      {
[10032]924        if(this->getRelCoor().x < this->travelDistancePlus.x)
925        {
[10098]926          if (this->travelVelocity.x < this->travelSpeed)
[10032]927          {
[10098]928            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
[10032]929          }
930          else
931          {
[10098]932            this->travelVelocity.x = this->travelSpeed;
[10032]933          }
934        }
935        else
936        {
[10098]937          this->travelVelocity.x = 0.0f;
[10032]938        }
[10019]939      }
[10032]940   
941      if( this->bBackward )
[10019]942      {
[10032]943        if(this->getRelCoor().x > this->travelDistanceMinus.x)
944        {
[10098]945          if (this->travelVelocity.x > -this->travelSpeed)
[10032]946          {
[10098]947            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
[10032]948          }
949          else
950          {
[10098]951            this->travelVelocity.x = -this->travelSpeed;
[10032]952          }
953        }
954        else
955        {
[10098]956          this->travelVelocity.x = 0.0f;
[10032]957        }
[10019]958      }
[10032]959   
960      if( this->bLeft)
[10019]961      {
[10032]962        if(this->getRelCoor().z > this->travelDistanceMinus.y)
963        {
[10098]964          if (this->travelVelocity.z > -this->travelSpeed)
[10032]965          {
[10098]966            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
[10032]967          }
968          else
969          {
[10098]970            this->travelVelocity.z = -this->travelSpeed;
[10032]971          }
972        }
973        else
974        {
[10098]975          this->travelVelocity.z = 0.0f;
[10032]976        }
[10043]977        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
[10019]978      }
[10032]979   
980      if( this->bRight)
[10019]981      {
[10032]982        if(this->getRelCoor().z < this->travelDistancePlus.y)
983        {
[10098]984          if (this->travelVelocity.z < this->travelSpeed)
[10032]985          {
[10098]986            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
[10032]987          }
988          else
989          {
[10098]990            this->travelVelocity.z = this->travelSpeed;
[10032]991          }
992        }
993        else
994        {
[10098]995          this->travelVelocity.z = 0.0f;
[10032]996        }
[10043]997        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
[10019]998      }
[10032]999      if (!this->bRight && !this->bLeft)
1000      {
[10043]1001        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
[10032]1002      }
1003
[10043]1004    //normalisation of the vectors (vector sum must be <= travelspeed)
[10098]1005    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
[10043]1006    if (xzNorm > this->travelSpeed)
1007    {
[10098]1008      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
1009      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
[10043]1010    }
[10032]1011
[10099]1012    //this moves camera and ship along the travel path.
[10284]1013    if (!this->entityTrack)
1014       this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
[10099]1015
[10043]1016    break;
[10019]1017    }
[10032]1018    case Playable::Vertical:
1019      break;
[10017]1020    default:
[10032]1021      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
[10017]1022  }
[10099]1023   //set new coordinates calculated through key- events.
[10098]1024  this->shiftCoor (this->travelVelocity * dt);
[9975]1025}
[10017]1026
[10032]1027void SpaceShip::setPlaymodeXML(const std::string& playmode)
1028{
1029  this->setPlaymode(Playable::stringToPlaymode(playmode));
1030}
1031
[10113]1032/**
1033 * @brief jumps to the next WeaponConfiguration
1034 */
1035void SpaceShip::nextWeaponConfig()
1036{
[10117]1037  PRINTF(0)("Requested next weapon config!\n");
1038  this->weaponMan.nextWeaponConfig();
[10113]1039  Playable::weaponConfigChanged();
1040}
1041
1042/**
1043 * @brief moves to the last WeaponConfiguration
1044 */
1045void SpaceShip::previousWeaponConfig()
1046{
[10117]1047  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
[10113]1048  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
1049  Playable::weaponConfigChanged();
[10117]1050}
1051
[10274]1052void SpaceShip::hit( float damage, WorldEntity* killer)
[10117]1053{
1054  this->damage(killer->getDamage(),0);
1055}
[10120]1056
1057void SpaceShip::updateElectronicWidget()
1058{
[10139]1059  if (this->electronicWidget != NULL)
1060  { //if it exists already: update it
1061     this->electronicWidget->setMaximum(this->electronicMax);
1062     this->electronicWidget->setValue(this->electronicCur);
1063  }
1064  else
1065  { //create the widget
1066    this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
[10230]1067    this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
[10194]1068    //this->electronicWidget->setDisplayedName("Electronics:");
[10139]1069    //this->electronicWidget->setSize2D(100,20);
1070    //this->electronicWidget->setAbsCoor2D(150,200);
1071    this->updateElectronicWidget();
1072    if (this->hasPlayer())
1073      State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1074  }
[10120]1075}
1076
1077void SpaceShip::updateShieldWidget()
1078{
[10139]1079  if (this->shieldWidget != NULL)
1080  { 
1081    this->shieldWidget->setMaximum(this->shieldMax);
1082    this->shieldWidget->setValue(this->shieldCur);;
1083  }
1084  else
1085  {
1086    this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
[10230]1087    this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
[10194]1088    //this->shieldWidget->setDisplayedName("Shield:");
[10139]1089    //his->shieldWidget->setSize2D(100,20);
1090    //this->shieldWidget->setAbsCoor2D(200,200);
1091    this->updateShieldWidget();
1092    if (this->hasPlayer())
1093      State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1094  }
[10120]1095}
[10270]1096
1097void SpaceShip::setCameraDistance(float dist)
1098{
1099  State::getCamera()->setViewTopDistance(dist);
1100}
1101
1102void SpaceShip::setCameraFovy(float fovy)
1103{
1104  State::getCamera()->setViewTopFovy(fovy);
1105}
Note: See TracBrowser for help on using the repository browser.