Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

interface changes

File size: 32.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/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  //dynamic_cast <OrxGui::GLGuiEnergyWidgetVertical*> (State::getPlayer()->hud().getArmorWidget())->setDisplayedName("Armor");
427  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
428  //this->attachCamera();
429 // this->setPlaymode(Playable::Horizontal);
430}
431
432void SpaceShip::leave()
433{
434  this->secWeaponMan.hideCrosshair();
435  this->toList( OM_GROUP_00);
436  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
437  //this->detachCamera();
438}
439
440
441/**
442 *  effect that occurs after the SpaceShip is spawned
443*/
444void SpaceShip::postSpawn ()
445{
446  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
447}
448
449/**
450 *  the action occuring if the spaceship left the game
451*/
452void SpaceShip::leftWorld ()
453{
454
455}
456
457WorldEntity* ref = NULL;
458/**
459 *  this function is called, when two entities collide
460 * @param entity: the world entity with whom it collides
461 *
462 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
463 */
464void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
465{
466}
467
468/**
469 *  draws the spaceship after transforming it.
470*/
471void SpaceShip::draw () const
472{
473  WorldEntity::draw();
474
475  glMatrixMode(GL_MODELVIEW);
476  glPushMatrix();
477
478  float matrix[4][4];
479  glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
480  this->getAbsDir().matrix (matrix);
481  glMultMatrixf((float*)matrix);
482  //glScalef(2.0, 2.0, 2.0);  // no double rescale
483 
484  this->trail->draw();
485 
486  glTranslatef(0,0,-.5);
487  this->trailL->draw();
488
489  glTranslatef(0,0,1);
490  this->trailR->draw();
491  glPopMatrix();
492  //this->debug(0);
493}
494
495/**
496 *  the function called for each passing timeSnap
497 * @param time The timespan passed since last update
498*/
499void SpaceShip::tick (float time)
500{
501  // Playable::tick(time);$
502  this->test->tick(time);
503  // Own Tick Setup, as a different fire routine is used on the weapon manager
504  this->weaponMan.tick(time);
505  this->secWeaponMan.tick(time);
506
507  if( this->systemFailure() )
508    bFire = bSecFire = false;
509
510  // fire reqeust/release for primary weapons
511  if( this->bFire)
512    this->weaponMan.fire();
513  else
514    this->weaponMan.releaseFire();
515
516  // fire reqeust/release for secondary weapons
517  if( this->bSecFire)
518    this->secWeaponMan.fire();
519  else
520    this->secWeaponMan.releaseFire();
521   
522  // Tracktick
523  if(this->entityTrack)
524    this->entityTrack->tick(time);
525
526
527  // Shield Regeneration and other regular calculations on the ship
528  this->regen(time);
529
530  // Weapon Regeneration and other regular calculations on the ship
531  this->weaponRegen(time);
532
533  // current engine speed output
534  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
535
536  // calculation of maxSpeed and acceleration:
537  this->travelSpeed = this->engineSpeedCur * 5;
538  this->acceleration = this->travelSpeed * 2;
539
540  this->movement(time);
541
542   // TRYING TO FIX PNode.
543  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
544  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
545
546 
547  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
548  this->oldPos    = this->getAbsCoor();
549
550  this->trail->tick(time);
551  this->trailL->tick(time);
552  this->trailR->tick(time);
553
554  this->weaponMan.setParentEntity( this);
555  this->secWeaponMan.setParentEntity( this);
556
557  //orient the spaceship in direction of the mouse
558  /*
559  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
560  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
561    this->setAbsDir( rotQuat);
562  //this->setAbsDirSoft(mouseDir,5);
563  */
564
565  // this is the air friction (necessary for a smooth control)
566  /*
567  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
568  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
569  */
570
571  //other physics (gravity)
572  //if(travelSpeed < 120)
573  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
574
575  //hoover effect
576  //cycle += time;
577  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
578
579  //readjust
580  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
581  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
582
583  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
584
585  /*
586  this->shiftCoor(move);
587  */
588
589  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
590
591}
592
593/**
594 * @todo switch statement ??
595 */
596void SpaceShip::process(const Event &event)
597{
598  //Playable::process(event);
599 
600  if( event.type == KeyMapper::PEV_LEFT)
601    this->bLeft = event.bPressed;
602  else if( event.type == KeyMapper::PEV_RIGHT)
603    this->bRight = event.bPressed;
604  else if( event.type == KeyMapper::PEV_FORWARD)
605    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
606  else if( event.type == KeyMapper::PEV_BACKWARD)
607    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
608  else if( event.type == KeyMapper::PEV_FIRE2)
609    this->bSecFire = event.bPressed;
610  else if( event.type == KeyMapper::PEV_FIRE1)
611    this->bFire = event.bPressed;
612  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
613  {
614    this->nextWeaponConfig();
615  }
616  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
617    this->previousWeaponConfig();
618
619
620  /*
621  else if( event.type == EV_MOUSE_MOTION)
622  {
623
624    this->xMouse += event.xRel;
625    this->yMouse += event.yRel;
626  }
627  */
628}
629
630void SpaceShip::destroy( WorldEntity* killer )
631{
632  PRINTF(5)("spaceship destroy\n");
633 
634  EmitterNode* node  = NULL;
635  DotEmitter* emitter = NULL;
636  SpriteParticles*  explosionParticles  = NULL;
637
638  explosionParticles = new SpriteParticles(200);
639  explosionParticles->setName("SpaceShipExplosionParticles");
640  explosionParticles->setLifeSpan(.2, .3);
641  explosionParticles->setRadius(0.0, 10.0);
642  explosionParticles->setRadius(.5, 6.0);
643  explosionParticles->setRadius(1.0, 3.0);
644  explosionParticles->setColor(0.0, 1,1,1,.9);
645  explosionParticles->setColor(0.1,  1,1,0,.9);
646  explosionParticles->setColor(0.5, .8,.4,0,.5);
647  explosionParticles->setColor(1.0, .2,.2,.2,.5);
648
649 
650  emitter = new DotEmitter( 2000, 70, 360);
651  //emitter->setSpread( 0, M_2_PI);
652  emitter->setEmissionRate( 200.0);
653  //emitter->setEmissionVelocity( 200.0);
654  //emitter->setSystem( explosionParticles);
655  //emitter->setAbsCoor( this->getAbsCoor());
656
657  node  = new EmitterNode( .1f);
658  node->setupParticle( emitter, explosionParticles);
659  node->setAbsDir( this->getAbsDir());
660  node->setVelocity( this->getVelocity() * .9f);
661  node->setAbsCoor( this->getAbsCoor());
662  if( !node->start())
663    PRINTF(0)("Explosion node not correctly started!");
664
665/*
666  PNode* node          = new PNode();
667  node->setAbsCoor(this->getAbsCoor());
668  Explosion* explosion = new Explosion();
669  explosion->explode( node, Vector(5,5,5));
670*/
671
672  if( this->hasPlayer())
673  {
674        this->setAbsCoor(Vector(-10000,10000,10000));
675        this->hide();
676  }
677  else
678  {
679    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
680  }
681
682}
683
684void SpaceShip::respawn( )
685{
686  this->unhide();
687  /*for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
688  {
689    if( dynamic_cast<WorldEntity*>(*it) != NULL)
690      dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_00);
691  }*/
692  /*
693  if( this->hasPlayer())
694  {
695    this->toList( OM_GROUP_01);
696    for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
697        {
698          if( likely( dynamic_cast<Weapon*>(*it) != NULL))
699                dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_00_PROJ);
700        }
701  }
702  else
703  {
704    this->toList( OM_GROUP_00);
705    for(ObjectList<PNode>::const_iterator it = this->getNodesChildren().begin(); it != this->getNodesChildren().end(); it++)
706        {
707          if( likely( dynamic_cast<Weapon*>(*it) != NULL))
708                dynamic_cast<WorldEntity*>(*it)->toList( OM_GROUP_01_PROJ);
709        }
710  }*/
711
712}
713
714
715void SpaceShip::damage(float pDamage, float eDamage){
716  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
717
718  if( this->shieldActive) {
719    if( this->shieldCur > pDamage) {
720      this->shieldCur = this->shieldCur - pDamage;
721    }
722    else { // shield <= pDamage
723      this->shieldCur -=pDamage;
724      this->shieldActive = false; //shield collapses
725      pDamage += this->shieldCur;
726      if( !this->shieldActive) {
727        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
728        this->electronicCur -= eDamage;
729      }
730    }
731  }
732  else {
733    this->armorCur = this->armorCur - pDamage;
734    this->electronicCur = this->electronicCur - eDamage;
735  }
736  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
737    this->destroy(this);
738
739  updateElectronicWidget();
740  updateShieldWidget();
741
742  this->setHealth( this->armorCur);
743}
744
745
746void SpaceShip::regen(float time){
747  float tmp;
748  if (this->armorCur != this->armorMax || this->armorRegen != 0){
749    tmp = this->armorCur + this->armorRegen * time;
750    if ( tmp > electronicMax)
751      this->armorCur = this->armorMax;
752    else
753      this->armorCur = tmp;
754  }
755  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
756    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
757    if( tmp > shieldMax)
758      this->shieldCur = this->shieldMax;
759    else
760      this->shieldCur = tmp;
761    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
762
763    updateShieldWidget();
764  }
765
766  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
767
768  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
769    tmp = this->electronicCur + this->electronicRegen * time;
770    if ( tmp > electronicMax)
771      this->electronicCur = this->electronicMax;
772    else
773      this->electronicCur = tmp;
774
775    updateElectronicWidget();
776  }
777
778}
779
780
781/**
782 * Weapon regeneration
783 * does not use any reactor capacity, as it wouldn't work in a consistent way.
784 */
785void SpaceShip::weaponRegen(float time)
786{
787  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
788  Weapon* weapon;
789  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
790  {
791    weapon = this->weaponMan.getWeapon(i);
792    if( weapon != NULL && weapon->isActive())
793    {
794      weapon->increaseEnergy( energy);
795    }
796
797  }
798  // weaponMan.increaseAmmunition( weapon, energy);
799}
800
801
802void SpaceShip::enterPlaymode(Playable::Playmode playmode)
803{
804  switch(playmode)
805  {
806    case Playable::Full3D:
807      /*
808      if (State::getCameraNode != NULL)
809      {
810        Vector absCoor = this->getAbsCoor();
811        this->setParent(PNode::getNullParent());
812        this->setAbsCoor(absCoor);
813        State::getCameraNode()->setParentSoft(&this->cameraNode);
814        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
815        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
816        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
817
818      }
819      */
820      //break;
821
822      break;
823    case Playable::Horizontal:
824      if (State::getCameraNode != NULL)
825      {
826        this->debugNode(1);
827        this->travelNode->debugNode(1);
828
829        this->travelNode->setAbsCoor(this->getAbsCoor());
830        this->travelNode->updateNode(0.01f);
831
832        this->setParent(this->travelNode);
833        this->setRelCoor(0,0,0);
834
835        State::getCameraNode()->setParentSoft(this->travelNode);
836        //State::getCameraNode()->setParentSoft(this);
837        State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
838        State::getCameraTargetNode()->setParentSoft(this->travelNode);
839        //State::getCameraTargetNode()->setParentSoft(this);
840        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
841
842        this->debugNode(1);
843        this->travelNode->debugNode(1);
844      }
845      break;
846
847    default:
848      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
849  }
850}
851
852/**
853 * @brief calculate the velocity
854 * @param time the timeslice since the last frame
855*/
856
857void SpaceShip::movement (float dt)
858{
859  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
860  //the ship doesn't slide too much.
861  float airCoeff = 2.5;
862  float pi = 3.14;
863 
864
865  switch(this->getPlaymode())
866  {
867    case Playable::Horizontal:
868    {
869      // these routines will change the travel movement into zero in a short amout of time, if the player
870      // doesn't press any buttons.
871      if (this->travelVelocity.x >= 0)
872      {
873        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
874          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
875        else
876          this->travelVelocity.x = 0;
877      }
878      else
879      {
880        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
881          this->travelVelocity.x += airCoeff* this->acceleration * dt;
882        else
883          this->travelVelocity.x = 0;
884      }
885      if (this->travelVelocity.z >= 0)
886      {
887        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
888          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
889        else
890          this->travelVelocity.z = 0;
891      }
892      else
893      {
894        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
895          this->travelVelocity.z += airCoeff* this->acceleration * dt;
896        else
897          this->travelVelocity.z = 0;
898      }
899   
900      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
901      // and its continuing to slide a bit.
902      Vector oldCoor = this->getRelCoor();
903      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
904      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
905      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
906      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
907   
908      if( this->systemFailure() )
909        bForward = bBackward = bLeft = bRight = false;
910   
911      if( this->bForward )
912      {
913        if(this->getRelCoor().x < this->travelDistancePlus.x)
914        {
915          if (this->travelVelocity.x < this->travelSpeed)
916          {
917            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
918          }
919          else
920          {
921            this->travelVelocity.x = this->travelSpeed;
922          }
923        }
924        else
925        {
926          this->travelVelocity.x = 0.0f;
927        }
928      }
929   
930      if( this->bBackward )
931      {
932        if(this->getRelCoor().x > this->travelDistanceMinus.x)
933        {
934          if (this->travelVelocity.x > -this->travelSpeed)
935          {
936            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
937          }
938          else
939          {
940            this->travelVelocity.x = -this->travelSpeed;
941          }
942        }
943        else
944        {
945          this->travelVelocity.x = 0.0f;
946        }
947      }
948   
949      if( this->bLeft)
950      {
951        if(this->getRelCoor().z > this->travelDistanceMinus.y)
952        {
953          if (this->travelVelocity.z > -this->travelSpeed)
954          {
955            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
956          }
957          else
958          {
959            this->travelVelocity.z = -this->travelSpeed;
960          }
961        }
962        else
963        {
964          this->travelVelocity.z = 0.0f;
965        }
966        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
967      }
968   
969      if( this->bRight)
970      {
971        if(this->getRelCoor().z < this->travelDistancePlus.y)
972        {
973          if (this->travelVelocity.z < this->travelSpeed)
974          {
975            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
976          }
977          else
978          {
979            this->travelVelocity.z = this->travelSpeed;
980          }
981        }
982        else
983        {
984          this->travelVelocity.z = 0.0f;
985        }
986        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
987      }
988      if (!this->bRight && !this->bLeft)
989      {
990        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
991      }
992
993    //normalisation of the vectors (vector sum must be <= travelspeed)
994    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
995    if (xzNorm > this->travelSpeed)
996    {
997      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
998      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
999    }
1000
1001    //this moves camera and ship along the travel path.
1002    this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
1003
1004    break;
1005    }
1006    case Playable::Vertical:
1007      break;
1008    default:
1009      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
1010  }
1011   //set new coordinates calculated through key- events.
1012  this->shiftCoor (this->travelVelocity * dt);
1013}
1014
1015void SpaceShip::setPlaymodeXML(const std::string& playmode)
1016{
1017  this->setPlaymode(Playable::stringToPlaymode(playmode));
1018}
1019
1020/**
1021 * @brief jumps to the next WeaponConfiguration
1022 */
1023void SpaceShip::nextWeaponConfig()
1024{
1025  PRINTF(0)("Requested next weapon config!\n");
1026  this->weaponMan.nextWeaponConfig();
1027  Playable::weaponConfigChanged();
1028}
1029
1030/**
1031 * @brief moves to the last WeaponConfiguration
1032 */
1033void SpaceShip::previousWeaponConfig()
1034{
1035  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
1036  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
1037  Playable::weaponConfigChanged();
1038}
1039
1040void SpaceShip::hit(float damage, WorldEntity* killer)
1041{
1042  this->damage(killer->getDamage(),0);
1043  killer->collidesWith( this, this->getAbsCoor());
1044  //this->collidesWith(killer, this->getAbsCoor());
1045}
1046
1047void SpaceShip::updateElectronicWidget()
1048{
1049  if (this->electronicWidget != NULL)
1050  { //if it exists already: update it
1051     this->electronicWidget->setMaximum(this->electronicMax);
1052     this->electronicWidget->setValue(this->electronicCur);
1053  }
1054  else
1055  { //create the widget
1056    this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1057    this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1058    //this->electronicWidget->setDisplayedName("Electronics:");
1059    //this->electronicWidget->setSize2D(100,20);
1060    //this->electronicWidget->setAbsCoor2D(150,200);
1061    this->updateElectronicWidget();
1062    if (this->hasPlayer())
1063      State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1064  }
1065}
1066
1067void SpaceShip::updateShieldWidget()
1068{
1069  if (this->shieldWidget != NULL)
1070  { 
1071    this->shieldWidget->setMaximum(this->shieldMax);
1072    this->shieldWidget->setValue(this->shieldCur);;
1073  }
1074  else
1075  {
1076    this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1077    this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1078    //this->shieldWidget->setDisplayedName("Shield:");
1079    //his->shieldWidget->setSize2D(100,20);
1080    //this->shieldWidget->setAbsCoor2D(200,200);
1081    this->updateShieldWidget();
1082    if (this->hasPlayer())
1083      State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1084  }
1085}
Note: See TracBrowser for help on using the repository browser.