Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/world_entities/space_ships/space_ship.cc @ 10669

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

some modularisaztion hacking
hardlinked armor to WE Health

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