Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bump

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