Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10064 was 10064, checked in by marcscha, 17 years ago

weapon, collision, particles

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