Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

moved loadShield (XML), some minor cleanup

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