Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Update in swarm launcher, swarm missile, mbolt

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