Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

work of last week: radar set-up, Gui- Widget class draws itself according to Dir of 2d- element.

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