Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10230 was 10230, checked in by muellmic, 17 years ago

interface changes

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