Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Wobblegrid addition for alienweapons

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