Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

finished systemFailure(), included RNG

File size: 27.8 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Knecht
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
22#include "util/loading/resource_manager.h"
23
24#include "weapons/test_gun.h"
25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
28#include "weapons/swarm_missile.h"
29#include "weapons/boomerang_gun.h"
30#include "weapons/turret.h"
31#include "weapons/cannon.h"
32
33#include "particles/dot_emitter.h"
34#include "particles/sprite_particles.h"
35
36#include "util/loading/factory.h"
37#include "key_mapper.h"
38
39#include "network_game_manager.h"
40#include "shared_network_data.h"
41
42#include "power_ups/weapon_power_up.h"
43#include "power_ups/param_power_up.h"
44
45#include "graphics_engine.h"
46
47#include "plane.h"
48
49#include "state.h"
50#include "player.h"
51
52#include "util/loading/load_param.h"
53#include "time.h"
54
55
56// #include "lib/gui/gl_gui/glgui_bar.h"
57// #include "lib/gui/gl_gui/glgui_pushbutton.h"
58
59
60#include "class_id_DEPRECATED.h"
61ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP);
62CREATE_FACTORY(SpaceShip);
63
64#include "script_class.h"
65CREATE_SCRIPTABLE_CLASS(SpaceShip,
66                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
67                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
68                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
69                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
70                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
71                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
72                        //Coordinates
73                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
74                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
75                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
76                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
77                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
78                       );
79
80/**
81 *  destructs the spaceship, deletes alocated memory
82 */
83SpaceShip::~SpaceShip ()
84{
85  this->setPlayer(NULL);
86}
87
88/**
89 * loads a Spaceships information from a specified file.
90 * @param fileName the name of the File to load the spaceship from (absolute path)
91 */
92SpaceShip::SpaceShip(const std::string& fileName)
93    : secWeaponMan(this) //,
94    //supportedPlaymodes(Playable::Vertical) ,
95    //playmode(Playable::Vertical)
96{
97  this->init();
98  TiXmlDocument doc(fileName);
99
100  if(!doc.LoadFile())
101  {
102    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
103    return;
104  }
105
106  this->loadParams(doc.RootElement());
107}
108
109/**
110 *  creates a new Spaceship from Xml Data
111 * @param root the xml element containing spaceship data
112
113   @todo add more parameters to load
114*/
115SpaceShip::SpaceShip(const TiXmlElement* root)
116    : secWeaponMan(this) //,
117    //supportedPlaymodes(Playable::Vertical) ,
118    //playmode(Playable::Vertical)
119{
120  this->init();
121  if (root != NULL)
122    this->loadParams(root);
123
124}
125
126
127/**
128 * initializes a Spaceship
129 */
130void SpaceShip::init()
131{
132
133  srand(time(0));   //initialize Random Nomber Generator
134
135  this->setSupportedPlaymodes(Playable::Vertical);
136
137  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
138  this->registerObject(this, SpaceShip::_objectList);
139
140  PRINTF(4)("SPACESHIP INIT\n");
141
142  secWeaponMan.showCrosshair();
143
144  //weapons:
145
146  Weapon* wpRight1 = new LightBlaster ();
147  wpRight1->setName("Light Blaster Right");
148  Weapon* wpLeft1 = new LightBlaster ();
149  wpLeft1->setName("Medium Blaster Left");
150
151  Weapon* wpRight2 = new MediumBlaster ();
152  wpRight2->setName("Medium Blaster Right");
153  Weapon* wpLeft2 = new MediumBlaster ();
154  wpLeft2->setName("Medium Blaster Left");
155
156  Weapon* wpRight3 = new HeavyBlaster ();
157  wpRight3->setName("Heavy Blaster Right");
158  Weapon* wpLeft3 = new HeavyBlaster ();
159  wpLeft3->setName("Heavy Blaster Left");
160
161  Weapon* cannon = new SwarmMissile();
162  cannon->setName("Swarm Missile");
163 
164
165
166  this->weaponMan.addWeapon( wpLeft1, 0, 0);
167  this->weaponMan.addWeapon( wpRight1, 0, 1);
168  this->weaponMan.addWeapon( wpLeft2, 0, 2);
169  this->weaponMan.addWeapon( wpRight2, 0, 3);
170  this->weaponMan.addWeapon( wpLeft3, 0, 4);
171  this->weaponMan.addWeapon( wpRight3, 0, 5);
172
173  this->secWeaponMan.addWeapon( cannon, 0, 0);
174
175  this->weaponMan.changeWeaponConfig(0);
176
177  wpRight1->requestAction(WA_ACTIVATE);
178  wpLeft1->requestAction(WA_ACTIVATE);
179  wpRight2->requestAction(WA_ACTIVATE);
180  wpLeft2->requestAction(WA_ACTIVATE);
181  wpRight3->requestAction(WA_ACTIVATE);
182  wpLeft3->requestAction(WA_ACTIVATE);
183
184  cannon->requestAction(WA_ACTIVATE);
185
186  curWeaponPrimary    = 0;
187  curWeaponSecondary  = 0;
188
189  Playable::weaponConfigChanged();
190
191  reactorOutput     = 10;
192
193  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
194  engineSpeedBase   = 5;
195  shieldRegen       = 2;
196
197  shieldEnergyShare = 0.3;
198  weaponEnergyShare = 0.3;
199  engineEnergyShare = 0.4;
200
201  shieldCur         = 20;
202  shieldMax         = 100;
203  shieldTH          = .2* shieldMax;   // shield power must be 20% before shield kicks in again
204
205  electronicCur = 1;
206  electronicMax = 50;
207  electronicRegen   = 3;
208  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
209
210  /*
211  this->addWeapon(wpLeft, 1, 0);
212  this->addWeapon(wpRight,1 ,1);
213  //this->addWeapon(cannon, 0, 6);
214
215  this->getWeaponManager().changeWeaponConfig(1);
216  */
217
218  this->loadModel("models/ships/mantawing.obj");
219  //this->setVisibiliy(false);
220
221  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
222 
223  /*
224  xMouse = yMouse = 0;
225  yInvert = 1;
226  mouseSensitivity = 0.001;
227  airViscosity = 0.9;
228  controlVelocityX = 25;
229  controlVelocityY = 150;
230  shipInertia = 1.5;
231  */
232  //  cycle = 0.0;
233
234  this->setHealthMax(shieldMax);
235  this->setHealth(shieldCur);
236
237  this->travelNode = new PNode();
238
239  // camera - issue
240  //this->cameraNode = State::getCameraNode();
241  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
242  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
243
244  /*
245  this->velocity = this->getAbsDirX()*travelSpeed;
246  this->mouseDir = this->getAbsDir();
247  this->pitchDir = this->getAbsDir();
248  */
249
250
251  //   GLGuiButton* button = new GLGuiPushButton();
252  //    button->show();
253  //    button->setLabel("orxonox");
254  //    button->setBindNode(this);
255  //     GLGuiBar* bar = new GLGuiBar();
256  //     bar->show();
257  //     bar->setValue(7.0);
258  //     bar->setMaximum(10);
259  //     bar->setSize2D( 20, 100);
260  //     bar->setAbsCoor2D( 10, 200);
261
262  //add events to the eventlist
263  registerEvent(KeyMapper::PEV_FORWARD);
264  registerEvent(KeyMapper::PEV_BACKWARD);
265  registerEvent(KeyMapper::PEV_LEFT);
266  registerEvent(KeyMapper::PEV_RIGHT);
267  //registerEvent(SDLK_q);
268  //registerEvent(SDLK_e);
269  registerEvent(KeyMapper::PEV_FIRE1);
270  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
271  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
272  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
273  //registerEvent(SDLK_PAGEUP);
274  //registerEvent(SDLK_PAGEDOWN);
275  registerEvent(EV_MOUSE_MOTION);
276
277  this->weaponMan.setSlotCount(6);
278
279  this->weaponMan.setSlotPosition(0, Vector(-2.6, .1, -3.0));
280  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
281
282  this->weaponMan.setSlotPosition(1, Vector(-2.6, .1, 3.0));
283  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
284
285  this->weaponMan.setSlotPosition(2, Vector(-1.5, .5, -.5));
286  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
287
288  this->weaponMan.setSlotPosition(3, Vector(-1.5, .5, .5));
289  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
290
291  this->weaponMan.setSlotPosition(4, Vector(-1.5, -.5, .5));
292  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
293
294  this->weaponMan.setSlotPosition(5, Vector(-1.5, -.5, -.5));
295  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
296
297  this->secWeaponMan.setSlotCount(6);
298
299  this->secWeaponMan.setSlotPosition(0, Vector(1.5, -1, 0));
300  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
301
302  this->secWeaponMan.setSlotPosition(1, Vector(2.6, .1, 3.0));
303  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
304
305  this->secWeaponMan.setSlotPosition(2, Vector(1.5, .5, -.5));
306  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
307
308  this->secWeaponMan.setSlotPosition(3, Vector(1.5, .5, .5));
309  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
310
311  this->secWeaponMan.setSlotPosition(4, Vector(1.5, -.5, .5));
312  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
313
314  this->secWeaponMan.setSlotPosition(5, Vector(1.5, -.5, -.5));
315  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
316
317  //
318  //   this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
319  //   this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
320  //
321  //   this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
322  //   this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
323
324  this->weaponMan.getFixedTarget()->setParent(this);
325  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
326
327 
328  this->secWeaponMan.getFixedTarget()->setParent(this);
329  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
330  this->secWeaponMan.setRotationSpeed(0);
331
332  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
333
334  this->burstEmitter = new DotEmitter(200, 0.0, .01);
335  this->burstEmitter->setParent(this);
336  this->burstEmitter->setRelCoor(-1, .5, 0);
337  this->burstEmitter->setName("SpaceShip_Burst_emitter");
338
339  this->burstSystem = new SpriteParticles(1000);
340  this->burstSystem->addEmitter(this->burstEmitter);
341  this->burstSystem->setName("SpaceShip_Burst_System");
342  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
343  this->burstSystem->setLifeSpan(1.0, .3);
344  this->burstSystem->setRadius(0.0, 1.0);
345  this->burstSystem->setRadius(0.05, 1.0);
346  this->burstSystem->setRadius(.5, .8);
347  this->burstSystem->setRadius(1.0, 0);
348  this->burstSystem->setColor(0.0, .7,.7,1,.7);
349  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
350  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
351  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
352
353  /*
354  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) );
355  registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) );
356  registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) );
357  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
358  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
359  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
360  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
361  registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) );
362  registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) );
363  */
364
365  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
366  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
367  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
368  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
369  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
370  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
371
372  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
373
374  //this->airFriction = 0.5f;
375  this->travelDistancePlus = Vector2D(38.0, 43.0);
376  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
377  this->travelSpeed = 80;
378  this->cameraSpeed = 40;
379  this->cameraLook = 0.0f;
380  this->acceleration = 120.0f;
381  //this->airFriction = 0.0f;
382
383  srand(time(0));  //initaialize RNG
384
385  this->travelNode->debugDraw();
386}
387
388
389/**
390 * loads the Settings of a SpaceShip from an XML-element.
391 * @param root the XML-element to load the Spaceship's properties from
392 */
393void SpaceShip::loadParams(const TiXmlElement* root)
394{
395  Playable::loadParams(root);
396}
397
398void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
399{
400  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
401}
402
403void SpaceShip::setTravelSpeed(float travelSpeed)
404{
405  this->travelSpeed = travelSpeed;
406}
407
408/*
409void SpaceShip::setTravelHeight(float travelHeight)
410{
411  if (this->toTravelHeight == NULL)
412    this->toTravelHeight = new float;
413  *this->toTravelHeight = travelHeight;
414}
415*/
416
417/*
418void SpaceShip::setTravelDistance(const Vector2D& distance)
419{
420  this->travelDistance = distance;
421}
422
423void SpaceShip::setTravelDistance(float x, float y)
424{
425  this->setTravelDistance(Vector2D(x, y));
426}
427*/
428
429
430
431void SpaceShip::reset()
432{
433  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
434
435  //xMouse = yMouse = 0;
436
437  this->setHealth(80);
438  this->velocity = Vector(0.0, 0.0, 0.0);
439}
440
441
442void SpaceShip::enter()
443{
444  this->secWeaponMan.showCrosshair();
445  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
446  if (!this->setPlaymode(Playable::Vertical))
447    std::cout << "playmode not supported";
448  //this->attachCamera();
449}
450
451void SpaceShip::leave()
452{
453  this->secWeaponMan.hideCrosshair();
454  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
455  //this->detachCamera();
456}
457
458
459/**
460 *  effect that occurs after the SpaceShip is spawned
461*/
462void SpaceShip::postSpawn ()
463{
464  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
465}
466
467/**
468 *  the action occuring if the spaceship left the game
469*/
470void SpaceShip::leftWorld ()
471{}
472
473WorldEntity* ref = NULL;
474/**
475 *  this function is called, when two entities collide
476 * @param entity: the world entity with whom it collides
477 *
478 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
479 */
480void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
481{
482}
483
484/**
485 *  draws the spaceship after transforming it.
486*/
487void SpaceShip::draw () const
488{
489  WorldEntity::draw();
490
491  //this->debug(0);
492}
493
494/**
495 *  the function called for each passing timeSnap
496 * @param time The timespan passed since last update
497*/
498void SpaceShip::tick (float time)
499{
500  // Playable::tick(time);$
501
502  // Own Tick Setup, as a different fire routine is used on the weapon manager
503  this->weaponMan.tick(time);
504  this->secWeaponMan.tick(time);
505
506  if( this->systemFailure() )
507    bFire = bSecFire = false;
508
509  if( this->bFire)
510  {
511    this->weaponMan.fire();
512  }
513  if( this->bSecFire)
514  {
515    this->secWeaponMan.fire();
516    this->bSecFire = !this->bSecFire;   // FIXME This currently is needed to prevent "echo fires" of a second rocket after its cooldown has passed
517  }
518
519
520  // Shield Regeneration and other regular calculations on the ship
521  this->regen(time);
522
523  // Weapon Regeneration and other regular calculations on the ship
524  this->weaponRegen(time);
525
526  // current engine speed output
527  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
528
529
530  /*
531  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
532  {
533    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
534    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
535    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
536    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
537
538    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
539
540    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
541    xMouse = yMouse = 0;
542  }
543*/
544
545  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
546  //if (this->getOwner() == this->getHostID())
547
548  //is->calculateVelocity(time);
549
550
551  //vector move = velocity*time;
552
553  /*
554  //orient the velocity in the direction of the spaceship.
555  travelSpeed = velocity.len();
556  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
557  velocity = (velocity.getNormalized())*travelSpeed;
558  */
559  this->movement(time);
560
561   // TRYING TO FIX PNode.
562  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
563  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
564
565  this->burstEmitter->setEmissionRate(travelSpeed);
566  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
567
568  //orient the spaceship in direction of the mouse
569  /*
570  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
571  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
572    this->setAbsDir( rotQuat);
573  //this->setAbsDirSoft(mouseDir,5);
574  */
575
576  // this is the air friction (necessary for a smooth control)
577  /*
578  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
579  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
580  */
581
582  //other physics (gravity)
583  //if(travelSpeed < 120)
584  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
585
586  //hoover effect
587  //cycle += time;
588  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
589
590  //readjust
591  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
592  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
593
594  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
595
596  /*
597  this->shiftCoor(move);
598  */
599
600  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
601
602}
603
604/*
605void SpaceShip::calculateVelocity (float time)
606{
607  Vector accel(0.0, 0.0, 0.0);
608  /* calculate the direction in which the craft is heading  */
609  /*
610  if( this->bUp )
611  {
612    accel += (this->getAbsDirX())*acceleration;
613
614  }
615
616  if( this->bDown )
617  {
618    accel -= (this->getAbsDirX())*0.5*acceleration;
619
620  }
621
622  if( this->bLeft)
623  {
624    this->shiftDir(Quaternion(time, Vector(0,1,0)));
625  }
626  if( this->bRight)
627  {
628    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
629  }
630
631
632  if( this->bRollL)
633  {
634    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
635  }
636  if( this->bRollR)
637  {
638    mouseDir *= Quaternion(time*2, Vector(1,0,0));
639
640  }
641  if (this->bAscend )
642  {
643    this->shiftDir(Quaternion(time, Vector(0,0,1)));
644  }
645  if (this->bDescend )
646  {
647    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
648  }
649
650  velocity += accel*time*10;
651
652}
653*/
654
655/**
656 * @todo switch statement ??
657 */
658void SpaceShip::process(const Event &event)
659{
660  //Playable::process(event);
661
662  if( event.type == KeyMapper::PEV_LEFT)
663    this->bLeft = event.bPressed;
664  else if( event.type == KeyMapper::PEV_RIGHT)
665    this->bRight = event.bPressed;
666  else if( event.type == KeyMapper::PEV_FORWARD)
667    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
668  else if( event.type == KeyMapper::PEV_BACKWARD)
669    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
670  else if( event.type == KeyMapper::PEV_FIRE2)
671    this->bSecFire = event.bPressed;
672  else if( event.type == KeyMapper::PEV_FIRE1)
673    this->bFire = event.bPressed;
674
675
676  /*
677  else if( event.type == EV_MOUSE_MOTION)
678  {
679
680    this->xMouse += event.xRel;
681    this->yMouse += event.yRel;
682  }
683  */
684}
685
686void SpaceShip::destroy( WorldEntity* killer )
687{
688  PRINTF(0)("spaceship destroy\n");
689}
690
691void SpaceShip::respawn( )
692{
693  toList( OM_PLAYERS );
694}
695
696
697void SpaceShip::damage(float pDamage, float eDamage){
698if( this->shieldActive) {
699    if( this->shieldCur > pDamage) {
700      this->shieldCur = this->shieldCur - pDamage;
701    }
702    else { // shield <= pDamage
703      this->shieldCur -=pDamage;
704      this->shieldActive = false; //shield collapses
705      pDamage = pDamage - this->shieldCur;
706      if( !this->shieldActive) {
707        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
708        this->electronicCur -= eDamage;
709      }
710    }
711  }
712  else {
713    this->armorCur = this->armorCur - pDamage;
714    this->electronicCur = this->electronicCur - eDamage;
715  }
716  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
717}
718
719
720void SpaceShip::regen(float time){
721  float tmp;
722  if (this->armorCur != this->armorMax || this->armorRegen != 0){
723    tmp = this->armorCur + this->armorRegen * time;
724    if ( tmp > electronicMax)
725      this->armorCur = this->armorMax;
726    else
727      this->armorCur = tmp;
728  }
729  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
730    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
731    if( tmp > shieldMax)
732      this->shieldCur = this->shieldMax;
733    else
734      this->shieldCur = tmp;
735    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
736  }
737
738  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
739
740  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
741    tmp = this->electronicCur + this->electronicRegen * time;
742    if ( tmp > electronicMax)
743      this->electronicCur = this->electronicMax;
744    else
745      this->electronicCur = tmp;
746  }
747}
748
749
750/**
751 * Weapon regeneration
752 * does not use any reactor capacity, as it wouldn't work in a consistent way.
753 */
754void SpaceShip::weaponRegen(float time)
755{
756  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
757  Weapon* weapon;
758  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
759  {
760    weapon = this->weaponMan.getWeapon(i);
761    if( weapon != NULL && weapon->isActive())
762    {
763      weapon->increaseEnergy( energy);
764    }
765
766  }
767  // weaponMan.increaseAmmunition( weapon, energy);
768}
769
770
771void SpaceShip::enterPlaymode(Playable::Playmode playmode)
772{
773  switch(playmode)
774  {
775    case Playable::Full3D:
776      /*
777      if (State::getCameraNode != NULL)
778      {
779        Vector absCoor = this->getAbsCoor();
780        this->setParent(PNode::getNullParent());
781        this->setAbsCoor(absCoor);
782        State::getCameraNode()->setParentSoft(&this->cameraNode);
783        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
784        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
785        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
786
787      }
788      */
789      //break;
790
791
792    case Playable::Vertical:
793      if (State::getCameraNode != NULL)
794      {
795        this->debugNode(1);
796        this->travelNode->debugNode(1);
797
798        this->travelNode->setAbsCoor(this->getAbsCoor());
799        this->travelNode->updateNode(0.01f);
800
801        this->setParent(this->travelNode);
802        this->setRelCoor(0,0,0);
803
804        State::getCameraNode()->setParentSoft(this->travelNode);
805        State::getCameraNode()->setRelCoorSoft(-0.01, 40,0);
806        State::getCameraTargetNode()->setParentSoft(this->travelNode);
807        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
808
809        this->debugNode(1);
810        this->travelNode->debugNode(1);
811      }
812      break;
813
814    default:
815      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
816  }
817  std::cout << Playable::playmodeToString(this->getPlaymode());
818}
819
820/**
821 * @brief calculate the velocity
822 * @param time the timeslice since the last frame
823*/
824
825void SpaceShip::movement (float dt)
826{
827  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
828  //the ship doesn't slide too much.
829  float airCoeff = 2.5;
830
831  float pi = 3.14;
832
833  // these routines will change the travel movement into zero in a short amout of time, if the player
834  // doesn't press any buttons.
835  if (this->velocity.x >= 0)
836  {
837    if (this->velocity.x > airCoeff*this->acceleration * dt)
838      this->velocity.x -= airCoeff* this->acceleration * dt;
839    else
840      this->velocity.x = 0;
841  }
842  else
843  {
844    if (this->velocity.x < -airCoeff*this->acceleration * dt)
845      this->velocity.x += airCoeff* this->acceleration * dt;
846    else
847      this->velocity.x = 0;
848  }
849  if (this->velocity.z >= 0)
850  {
851    if (this->velocity.z > airCoeff*this->acceleration * dt)
852      this->velocity.z -= airCoeff* this->acceleration * dt;
853    else
854      this->velocity.z = 0;
855  }
856  else
857  {
858    if (this->velocity.z < -airCoeff*this->acceleration * dt)
859      this->velocity.z += airCoeff* this->acceleration * dt;
860    else
861      this->velocity.z = 0;
862  }
863
864  // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
865  // and its continuing to slide a bit.
866  Vector oldCoor = this->getRelCoor();
867  if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
868  if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
869  if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
870  if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
871
872  if( this->systemFailure() )
873    bForward = bBackward = bLeft = bRight = false;
874
875
876  if( this->bForward )
877  {
878    if(this->getRelCoor().x < this->travelDistancePlus.x)
879    {
880      if (this->velocity.x < this->travelSpeed)
881      {
882        this->velocity.x += (airCoeff + 1.0)*this->acceleration*dt;
883      }
884      else
885      {
886        this->velocity.x = this->travelSpeed;
887      }
888    }
889    else
890    {
891      this->velocity.x = 0.0f;
892    }
893  }
894
895  if( this->bBackward )
896  {
897    if(this->getRelCoor().x > this->travelDistanceMinus.x)
898    {
899      if (this->velocity.x > -this->travelSpeed)
900      {
901        this->velocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
902      }
903      else
904      {
905        this->velocity.x = -this->travelSpeed;
906      }
907    }
908    else
909    {
910      this->velocity.x = 0.0f;
911    }
912  }
913
914  if( this->bLeft)
915  {
916    if(this->getRelCoor().z > this->travelDistanceMinus.y)
917    {
918      if (this->velocity.z > -this->travelSpeed)
919      {
920        this->velocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
921      }
922      else
923      {
924        this->velocity.z = -this->travelSpeed;
925      }
926    }
927    else
928    {
929      this->velocity.z = 0.0f;
930    }
931  }
932
933  if( this->bRight)
934  {
935    if(this->getRelCoor().z < this->travelDistancePlus.y)
936    {
937      if (this->velocity.z < this->travelSpeed)
938      {
939        this->velocity.z += (airCoeff + 1.0)*this->acceleration*dt;
940      }
941      else
942      {
943        this->velocity.z = this->travelSpeed;
944      }
945    }
946    else
947    {
948      this->velocity.z = 0.0f;
949    }
950  }
951 
952  this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
953  this->shiftCoor (this->velocity * dt);
954 
955  float angle = this->velocity.z / travelSpeed * pi / 3;
956  if (angle > pi/3) angle = pi/3;
957  if (angle < -pi/3) angle = -pi/3;
958  this->setRelDirSoft(Quaternion(angle, Vector(1,0,0)), 5.0f);
959
960  /*
961  switch(this->getPlaymode())
962  {
963    case Playable::Vertical:
964      break;
965    default:
966      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
967  }
968  */
969}
970
Note: See TracBrowser for help on using the repository browser.