Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10755 was 10755, checked in by snellen, 17 years ago

less debug

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