Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

1) the centerpoint of the radar is now the centerpoint of the camera, so the enemies won't move in the radar when the player is moving; 2)fovy and cameradistance of each viewmode can now be set dynamically. so one would't have to readjust the fovy for each viewmode, when it has been manually changed once. (it's better to just change the fovy or distance for the viewmode where you need it); 3)unlike in the last revision, it doesnt have a soft- zoom- effect when setting a fovy anymore.

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#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  LoadParam(root, "cameraDistance", this, SpaceShip, setCameraDistance);
398  LoadParam(root, "cameraFovy", this, SpaceShip, setCameraFovy);
399}
400
401
402void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
403{
404  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
405}
406
407void SpaceShip::reset()
408{
409  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
410
411  //xMouse = yMouse = 0;
412
413  this->setHealth(80);
414  this->velocity = Vector(0.0, 0.0, 0.0);
415}
416
417
418void SpaceShip::enter()
419{
420  this->secWeaponMan.showCrosshair();
421  this->toList( OM_GROUP_01 );
422  State::getPlayer()->hud().setRadarCenterNode(this->travelNode);
423  State::getPlayer()->hud().setOverlayActive(true);
424  //dynamic_cast <OrxGui::GLGuiEnergyWidgetVertical*> (State::getPlayer()->hud().getArmorWidget())->setDisplayedName("Armor");
425  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
426  //this->attachCamera();
427 // this->setPlaymode(Playable::Horizontal);
428}
429
430void SpaceShip::leave()
431{
432  this->secWeaponMan.hideCrosshair();
433  this->toList( OM_GROUP_00);
434  State::getPlayer()->hud().setOverlayActive(false);
435  State::getCamera()->setEventHandling(true);
436  State::getPlayer()->hud().setRadarCenterNode(NULL);
437  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
438  //this->detachCamera();
439}
440
441
442/**
443 *  effect that occurs after the SpaceShip is spawned
444*/
445void SpaceShip::postSpawn ()
446{
447  if(this->hasPlayer())
448    Playable::postSpawn();
449
450  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
451}
452
453/**
454 *  the action occuring if the spaceship left the game
455*/
456void SpaceShip::leftWorld ()
457{
458
459}
460
461WorldEntity* ref = NULL;
462
463/**
464 *  draws the spaceship after transforming it.
465*/
466void SpaceShip::draw () const
467{
468  WorldEntity::draw();
469
470  glMatrixMode(GL_MODELVIEW);
471  glPushMatrix();
472
473  float matrix[4][4];
474  glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
475  this->getAbsDir().matrix (matrix);
476  glMultMatrixf((float*)matrix);
477  //glScalef(2.0, 2.0, 2.0);  // no double rescale
478  /*    // FIXME
479  this->trail->draw();
480 
481  glTranslatef(0,0,-.5);
482  this->trailL->draw();
483
484  glTranslatef(0,0,1);
485  this->trailR->draw();
486*/
487  glPopMatrix();
488  //this->debug(0);
489}
490
491/**
492 *  the function called for each passing timeSnap
493 * @param time The timespan passed since last update
494*/
495void SpaceShip::tick (float time)
496{
497  // Playable::tick(time);
498
499 // this->test->tick(time);
500
501  // Own Tick Setup, as a different fire routine is used on the weapon manager
502  this->weaponMan.tick(time);
503  this->secWeaponMan.tick(time);
504
505  if( this->systemFailure() )
506    bFire = bSecFire = false;
507
508  // fire reqeust/release for primary weapons
509  if( this->bFire)
510    this->weaponMan.fire();
511  else
512    this->weaponMan.releaseFire();
513
514  // fire reqeust/release for secondary weapons
515  if( this->bSecFire)
516    this->secWeaponMan.fire();
517  else
518    this->secWeaponMan.releaseFire();
519   
520  // Tracktick
521  if(this->entityTrack)
522    this->entityTrack->tick(time);
523
524
525  // Shield Regeneration and other regular calculations on the ship
526  this->regen(time);
527
528  // Weapon Regeneration and other regular calculations on the ship
529  this->weaponRegen(time);
530
531  // current engine speed output
532  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
533
534  // calculation of maxSpeed and acceleration:
535  this->travelSpeed = this->engineSpeedCur * 5;
536  this->acceleration = this->travelSpeed * 2;
537
538  this->movement(time);
539
540   // TRYING TO FIX PNode.
541  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
542  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
543
544 
545  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
546  this->oldPos    = this->getAbsCoor();
547
548//FIXME
549  this->trail->tick(time);
550  this->trailL->tick(time);
551  this->trailR->tick(time);
552
553  //orient the spaceship in direction of the mouse
554  /*
555  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
556  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
557    this->setAbsDir( rotQuat);
558  //this->setAbsDirSoft(mouseDir,5);
559  */
560  /*
561  this->shiftCoor(move);
562  */
563
564  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
565
566}
567
568/**
569 * @todo switch statement ??
570 */
571void SpaceShip::process(const Event &event)
572{
573  //Playable::process(event);
574 
575  if( event.type == KeyMapper::PEV_LEFT)
576    this->bLeft = event.bPressed;
577  else if( event.type == KeyMapper::PEV_RIGHT)
578    this->bRight = event.bPressed;
579  else if( event.type == KeyMapper::PEV_FORWARD)
580    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
581  else if( event.type == KeyMapper::PEV_BACKWARD)
582    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
583  else if( event.type == KeyMapper::PEV_FIRE2)
584    this->bSecFire = event.bPressed;
585  else if( event.type == KeyMapper::PEV_FIRE1)
586    this->bFire = event.bPressed;
587  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
588  {
589    this->nextWeaponConfig();
590  }
591  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
592    this->previousWeaponConfig();
593
594  if (!(State::getCamera()->getEventHandling()))
595  {
596    if( event.type == KeyMapper::PEV_VIEW0)
597    {
598      State::getCamera()->setViewMode(Camera::ViewNormal);
599      State::getCameraTargetNode()->setParent(this);
600      State::getCamera()->setParent(this);
601    }
602    else if( event.type == KeyMapper::PEV_VIEW1)
603    {
604      State::getCamera()->setViewMode(Camera::ViewBehind);
605      State::getCameraTargetNode()->setParent(this);
606      State::getCamera()->setParent(this);
607    }
608    else if( event.type == KeyMapper::PEV_VIEW2)
609    {
610      State::getCamera()->setViewMode(Camera::ViewFront);
611      State::getCameraTargetNode()->setParent(this);
612      State::getCamera()->setParent(this);
613    }
614    else if( event.type == KeyMapper::PEV_VIEW3)
615    {
616      State::getCamera()->setViewMode(Camera::ViewLeft);
617      State::getCameraTargetNode()->setParent(this);
618      State::getCamera()->setParent(this);
619    }
620    else if( event.type == KeyMapper::PEV_VIEW4)
621    {
622      State::getCamera()->setViewMode(Camera::ViewRight);
623      State::getCameraTargetNode()->setParent(this);
624      State::getCamera()->setParent(this);
625    }
626    else if( event.type == KeyMapper::PEV_VIEW5)
627    {
628      State::getCamera()->setViewMode(Camera::ViewTop);
629      State::getCameraTargetNode()->setParent(this->travelNode);
630      State::getCamera()->setParent(this->travelNode);
631    }
632  }
633
634
635  /*
636  else if( event.type == EV_MOUSE_MOTION)
637  {
638
639    this->xMouse += event.xRel;
640    this->yMouse += event.yRel;
641  }
642  */
643}
644
645void SpaceShip::destroy( WorldEntity* killer )
646{
647  if(this->hasPlayer())
648    Playable::destroy( killer);
649
650  PRINTF(5)("spaceship destroy\n");
651
652  EmitterNode* node  = NULL;
653  DotEmitter* emitter = NULL;
654  SpriteParticles*  explosionParticles  = NULL;
655
656  explosionParticles = new SpriteParticles(200);
657  explosionParticles->setName("SpaceShipExplosionParticles");
658  explosionParticles->setLifeSpan(.2, .3);
659  explosionParticles->setRadius(0.0, 10.0);
660  explosionParticles->setRadius(.5, 6.0);
661  explosionParticles->setRadius(1.0, 3.0);
662  explosionParticles->setColor(0.0, 1,1,1,.9);
663  explosionParticles->setColor(0.1,  1,1,0,.9);
664  explosionParticles->setColor(0.5, .8,.4,0,.5);
665  explosionParticles->setColor(1.0, .2,.2,.2,.5);
666
667 
668  emitter = new DotEmitter( 2000, 70, 360);
669  //emitter->setSpread( 0, M_2_PI);
670  emitter->setEmissionRate( 200.0);
671  //emitter->setEmissionVelocity( 200.0);
672  //emitter->setSystem( explosionParticles);
673  //emitter->setAbsCoor( this->getAbsCoor());
674
675  node  = new EmitterNode( .1f);
676  node->setupParticle( emitter, explosionParticles);
677  node->setAbsDir( this->getAbsDir());
678  node->setVelocity( this->getVelocity() * .9f);
679  node->setAbsCoor( this->getAbsCoor());
680  if( !node->start())
681    PRINTF(0)("Explosion node not correctly started!");
682/*
683  PNode* node          = new PNode();
684  node->setAbsCoor(this->getAbsCoor());
685  Explosion* explosion = new Explosion();
686  explosion->explode( node, Vector(5,5,5));
687*/
688/*
689  if( this->hasPlayer())
690  {
691        this->setAbsCoor(Vector(-10000,10000,10000));
692        this->hide();
693  }
694  else
695  {*/
696    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
697  //}
698
699}
700
701void SpaceShip::respawn( )
702{
703  Playable::respawn();
704}
705
706
707void SpaceShip::damage(float pDamage, float eDamage){
708  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
709
710  if( this->shieldActive) {
711    if( this->shieldCur > pDamage) {
712      this->shieldCur = this->shieldCur - pDamage;
713    }
714    else { // shield <= pDamage
715      this->shieldCur -=pDamage;
716      this->shieldActive = false; //shield collapses
717      pDamage += this->shieldCur;
718      if( !this->shieldActive) {
719        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
720        this->electronicCur -= eDamage;
721      }
722    }
723  }
724  else {
725    this->armorCur = this->armorCur - pDamage;
726    this->electronicCur = this->electronicCur - eDamage;
727  }
728  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
729    this->destroy(this);
730
731  updateElectronicWidget();
732  updateShieldWidget();
733
734  this->setHealth( this->armorCur);
735}
736
737
738void SpaceShip::regen(float time){
739  float tmp;
740  if (this->armorCur != this->armorMax || this->armorRegen != 0){
741    tmp = this->armorCur + this->armorRegen * time;
742    if ( tmp > electronicMax)
743      this->armorCur = this->armorMax;
744    else
745      this->armorCur = tmp;
746  }
747  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
748    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
749    if( tmp > shieldMax)
750      this->shieldCur = this->shieldMax;
751    else
752      this->shieldCur = tmp;
753    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
754
755    updateShieldWidget();
756  }
757
758  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
759
760  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
761    tmp = this->electronicCur + this->electronicRegen * time;
762    if ( tmp > electronicMax)
763      this->electronicCur = this->electronicMax;
764    else
765      this->electronicCur = tmp;
766
767    updateElectronicWidget();
768  }
769
770}
771
772
773/**
774 * Weapon regeneration
775 * does not use any reactor capacity, as it wouldn't work in a consistent way.
776 */
777void SpaceShip::weaponRegen(float time)
778{
779  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
780  Weapon* weapon;
781  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
782  {
783    weapon = this->weaponMan.getWeapon(i);
784    if( weapon != NULL && weapon->isActive())
785    {
786      weapon->increaseEnergy( energy);
787    }
788
789  }
790  // weaponMan.increaseAmmunition( weapon, energy);
791}
792
793
794void SpaceShip::enterPlaymode(Playable::Playmode playmode)
795{
796  switch(playmode)
797  {
798    case Playable::Full3D:
799      /*
800      if (State::getCameraNode != NULL)
801      {
802        Vector absCoor = this->getAbsCoor();
803        this->setParent(PNode::getNullParent());
804        this->setAbsCoor(absCoor);
805        State::getCameraNode()->setParentSoft(&this->cameraNode);
806        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
807        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
808        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
809
810      }
811      */
812      //break;
813
814      break;
815    case Playable::Horizontal:
816      if (State::getCameraNode != NULL)
817      {
818        this->debugNode(1);
819        this->travelNode->debugNode(1);
820
821        this->travelNode->setAbsCoor(this->getAbsCoor());
822        this->travelNode->updateNode(0.01f);
823
824        this->setParent(this->travelNode);
825        this->setRelCoor(0,0,0);
826
827        State::getCameraNode()->setParentSoft(this->travelNode);
828        //State::getCameraNode()->setParentSoft(this);
829        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
830        State::getCameraTargetNode()->setParentSoft(this->travelNode);
831        //State::getCameraTargetNode()->setParentSoft(this);
832        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
833        this->setCameraMode(Camera::ViewTop);
834        State::getCamera()->setEventHandling(false);
835        registerEvent(KeyMapper::PEV_VIEW0);
836        registerEvent(KeyMapper::PEV_VIEW1);
837        registerEvent(KeyMapper::PEV_VIEW2);
838        registerEvent(KeyMapper::PEV_VIEW3);
839        registerEvent(KeyMapper::PEV_VIEW4);
840        registerEvent(KeyMapper::PEV_VIEW5);
841
842        State::getCamera()->setParentMode(PNODE_MOVEMENT);
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}
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}
1086
1087void SpaceShip::setCameraDistance(float dist)
1088{
1089  State::getCamera()->setViewTopDistance(dist);
1090}
1091
1092void SpaceShip::setCameraFovy(float fovy)
1093{
1094  State::getCamera()->setViewTopFovy(fovy);
1095}
Note: See TracBrowser for help on using the repository browser.