Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

xfer

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