Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10233 was 10233, checked in by muellmic, 14 years ago

energy bars are now changing its colors, adding hud overlay - very strange coordinate behaviour tough, awaiting fix

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