Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/space_ship.cc @ 10742

Last change on this file since 10742 was 10742, checked in by nicolasc, 17 years ago

moved loadHealth to WE

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