Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Fixes and cleanups

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