Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaned space_ship.cc

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