Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed camera handling for the vertical-scroller playmode. the top-down view in vertical scroller is now a proper camera-viewmode. also added a bool-state in the camera class to override the camera's event-listener and handle the camera-events (view buttons) in other classes. i used this to handle the camera parenting with the space-ship class, so other views as the top-down get useful and nice-looking again…. *taking-a-look-at-the-watch* oops damnit… i hope i won't forget the last tram ;-)

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