Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added shield functionality, reverted paeddae's MP patch - does not work

File size: 35.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Knecht
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
22#include "util/loading/resource_manager.h"
23
24#include "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 
431  printf("Ship Status: %f, %f\n", this->getHealth(), this->getShield());
432 
433  if(caminit)
434  {
435  State::getCamera()->setViewMode(Camera::ViewNormal);
436  State::getCameraTargetNode()->setParent(this);
437  State::getCamera()->setParent(this);
438  caminit = false;
439  }
440 
441  if ( deadBox != NULL )
442  {
443    OrxGui::GLGuiHandler::getInstance()->tick( time );
444  }
445 // printf("SSGROUP: %d\n", this->getOMListNumber());
446
447  if( !this->bInit)
448  {
449    // now get slots from the mount points
450    std::map<int, MountPoint*>::iterator it = this->mountPointMap.begin();
451    for( ;it != this->mountPointMap.end(); it++)
452    {
453      WeaponSlot* ws = dynamic_cast<WeaponSlot*>((*it).second->getMount());
454      if( ws != NULL && ws->isA(WeaponSlot::staticClassID()))
455      {
456        int slot = ws->getWeaponSlot();
457//         int side = ws->getWeaponSide(); // HACK needed for some weapons (left/right)
458        this->getWeaponManager().setSlotPosition(slot, (*it).second->getCenter());
459        this->getWeaponManager().setSlotDirection(slot, ws->getRelDir());
460//         PRINTF(0)("setting slot %i\n", slot);
461//         (*it).second->getCenter().debug();
462      }
463    }
464  this->bInit = true;
465  }
466
467
468  // Own Tick Setup, as a different fire routine is used on the weapon manager
469  this->weaponMan.tick(time);
470  this->secWeaponMan.tick(time);
471
472  if( this->systemFailure() )
473    bFire = bSecFire = false;
474
475  // fire reqeust/release for primary weapons
476  if( this->bFire)
477    this->weaponMan.fire();
478  else
479    this->weaponMan.releaseFire();
480
481  // fire reqeust/release for secondary weapons
482  if( this->bSecFire)
483    this->secWeaponMan.fire();
484  else
485    this->secWeaponMan.releaseFire();
486
487  // Tracktick
488  if(this->entityTrack)
489    this->entityTrack->tick(time);
490
491
492  // Shield Regeneration and other regular calculations on the ship
493  this->regen(time);
494
495  // Weapon Regeneration and other regular calculations on the ship
496  this->weaponRegen(time);
497
498  // current engine speed output
499  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
500
501  // calculation of maxSpeed and acceleration:
502  this->travelSpeed = this->engineSpeedCur * 5;
503  this->acceleration = this->travelSpeed * 2;
504
505  this->movement(time);
506
507   // TRYING TO FIX PNode.
508  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
509  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
510
511
512  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
513  this->oldPos    = this->getAbsCoor();
514
515  if (!this->isTravelDistanceInit)
516  {
517    this->updateTravelDistance();
518    //this->isTravelDistanceInit = true;
519  }
520}
521
522/**
523 * @todo switch statement ??
524 */
525void SpaceShip::process(const Event &event)
526{
527//   Playable::process(event);
528
529  if( event.type == KeyMapper::PEV_LEFT)
530    this->bLeft = event.bPressed;
531  else if( event.type == KeyMapper::PEV_RIGHT)
532  {
533    this->bRight = event.bPressed;
534//     printf("ShipCoors: %f , %f, %f \n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
535  }
536  else if( event.type == KeyMapper::PEV_FORWARD)
537  {
538    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
539
540  }
541  else if( event.type == KeyMapper::PEV_BACKWARD)
542    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
543  else if( event.type == KeyMapper::PEV_FIRE2)
544    this->bSecFire = event.bPressed;
545  else if( event.type == KeyMapper::PEV_FIRE1)
546  {
547    printf(" %f, %f, %f \n",this->getAbsCoor().x,this->getAbsCoor().y,this->getAbsCoor().z );
548    this->bFire = event.bPressed;
549  }
550  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
551  {
552    this->nextWeaponConfig();
553  }
554  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
555    this->previousWeaponConfig();
556  else if( event.type == EV_MOUSE_MOTION)
557  {
558//     printf("Spaceship Mouse Motion Event\n");
559//     dynamic_cast<Crosshair*>(this->weaponMan.getFixedTarget())->process(event);
560//     this->weaponMan.process(event);
561    this->xMouse += event.xRel;
562    this->yMouse += event.yRel;
563  //  printf("Mouse Coord: %f, %f\n", this->xMouse, this->yMouse);
564   
565    float maxMouseDist = 75.1;
566    #define sqr(a) ((a)*(a))
567    float mouseDist = sqrt( sqr(0.6*xMouse) + sqr(yMouse) );
568
569    if ( mouseDist > maxMouseDist )
570    {
571        xMouse /= mouseDist/maxMouseDist;
572        yMouse /= mouseDist/maxMouseDist;
573    }
574
575    this->weaponMan.getFixedTarget()->setRelCoor(100000, -100 * this->mouseSensitivity * yMouse, 100 * this->mouseSensitivity * xMouse);
576    this->secWeaponMan.getFixedTarget()->setRelCoor(50000, -50 * this->mouseSensitivity * yMouse, 50 * this->mouseSensitivity * xMouse);
577  }
578  else if (!(State::getCamera()->getEventHandling()))
579  {
580    //PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
581    if( event.type == KeyMapper::PEV_VIEW0)
582    {
583
584    }
585    else if( event.type == KeyMapper::PEV_VIEW1)
586    {
587      State::getCamera()->setViewMode(Camera::ViewBehind);
588      State::getCameraTargetNode()->setParent(this);
589      State::getCamera()->setParent(this);
590    }
591    else if( event.type == KeyMapper::PEV_VIEW2)
592    {
593      State::getCamera()->setViewMode(Camera::ViewFront);
594      State::getCameraTargetNode()->setParent(this);
595      State::getCamera()->setParent(this);
596    }
597    else if( event.type == KeyMapper::PEV_VIEW3)
598    {
599      State::getCamera()->setViewMode(Camera::ViewLeft);
600      State::getCameraTargetNode()->setParent(this);
601      State::getCamera()->setParent(this);
602    }
603    else if( event.type == KeyMapper::PEV_VIEW4)
604    {
605      State::getCamera()->setViewMode(Camera::ViewRight);
606      State::getCameraTargetNode()->setParent(this);
607      State::getCamera()->setParent(this);
608    }
609    else if( event.type == KeyMapper::PEV_VIEW5)
610    {
611      State::getCamera()->setViewMode(Camera::ViewTop);
612      State::getCameraTargetNode()->setParent(this->travelNode);
613      State::getCamera()->setParent(this->travelNode);
614    }
615  }
616
617}
618
619void SpaceShip::respawn( )
620{
621  Playable::respawn();
622}
623
624
625/**
626 * Weapon regeneration
627 * does not use any reactor capacity, as it wouldn't work in a consistent way.
628 */
629void SpaceShip::weaponRegen(float time)
630{
631  float energy = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time ;
632  Weapon* weapon;
633  int weaponCount = 0;
634  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
635    weapon = this->weaponMan.getWeapon(i);
636    if( weapon != NULL && weapon->isActive()) {
637      weaponCount++;
638    }
639  }
640
641  if (weaponCount == 0)
642    return;
643  energy *= ( this->weaponMan.getSlotCount() / (float)weaponCount );
644
645  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++) {
646    weapon = this->weaponMan.getWeapon(i);
647    if( weapon != NULL && weapon->isActive()) {
648      weapon->increaseEnergy( energy);
649    }
650  }
651  // weaponMan.increaseAmmunition( weapon, energy);
652}
653
654
655void SpaceShip::enterPlaymode(Playable::Playmode playmode){
656  switch(playmode)
657  {
658    case Playable::Full3D:
659      /*
660      if (State::getCameraNode != NULL)
661      {
662        Vector absCoor = this->getAbsCoor();
663        this->setParent(PNode::getNullParent());
664        this->setAbsCoor(absCoor);
665        State::getCameraNode()->setParentSoft(&this->cameraNode);
666        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
667        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
668        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
669
670      }
671      */
672      //break;
673
674      break;
675    case Playable::Horizontal:
676      if (State::getCameraNode != NULL)
677      {
678        this->debugNode(1);
679        this->travelNode->debugNode(1);
680
681        this->travelNode->setAbsCoor(this->getAbsCoor());
682        this->travelNode->updateNode(0.01f);
683
684        this->isTravelDistanceInit = false;
685
686        if(this->entityTrack)
687           this->travelNode->setParent(this->entityTrack->getTrackNode());
688
689        this->setParent(this->travelNode);
690        this->setRelCoor(0,0,0);
691
692        State::getCameraNode()->setParentSoft(this->travelNode);
693        //State::getCameraNode()->setParentSoft(this);
694        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
695        State::getCameraTargetNode()->setParentSoft(this->travelNode);
696        //State::getCameraTargetNode()->setParentSoft(this);
697        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
698        this->setCameraMode(Camera::ViewTop);
699        State::getCamera()->setEventHandling(false);
700        registerEvent(KeyMapper::PEV_VIEW0);
701        registerEvent(KeyMapper::PEV_VIEW1);
702        registerEvent(KeyMapper::PEV_VIEW2);
703        registerEvent(KeyMapper::PEV_VIEW3);
704        registerEvent(KeyMapper::PEV_VIEW4);
705        registerEvent(KeyMapper::PEV_VIEW5);
706
707        State::getCamera()->setParentMode(PNODE_ALL);
708
709        //this->updateTravelDistance();
710
711        this->debugNode(1);
712        this->travelNode->debugNode(1);
713      }
714      break;
715
716
717    case Playable::Vertical:
718    {
719        this->travelNode->setAbsCoor(this->getAbsCoor());
720        this->travelNode->updateNode(0.01f);
721
722        this->isTravelDistanceInit = false;
723
724        if(this->entityTrack)
725           this->travelNode->setParent(this->entityTrack->getTrackNode());
726
727        this->setParent(this->travelNode);
728        this->setRelCoor(0,0,0);
729
730        State::getCameraNode()->setParentSoft(this->travelNode);
731        //State::getCameraNode()->setParentSoft(this);
732        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
733        State::getCameraTargetNode()->setParentSoft(this->travelNode);
734        //State::getCameraTargetNode()->setParentSoft(this);
735        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
736        //this->setCameraMode(Camera::ViewNormal);
737        State::getCamera()->setEventHandling(false);
738
739        PRINTF(0)("SETCAMERA %x\n", State::getCamera());
740        State::getCamera()->setViewMode(Camera::ViewNormal);
741        State::getCameraTargetNode()->setParent(this);
742        State::getCamera()->setParent(this);
743
744
745        registerEvent(KeyMapper::PEV_VIEW0);
746        registerEvent(KeyMapper::PEV_VIEW1);
747        registerEvent(KeyMapper::PEV_VIEW2);
748        registerEvent(KeyMapper::PEV_VIEW3);
749        registerEvent(KeyMapper::PEV_VIEW4);
750        registerEvent(KeyMapper::PEV_VIEW5);
751
752        State::getCamera()->setParentMode(PNODE_ALL);
753
754      break;
755    }
756
757    default:
758      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
759  }
760}
761
762/**
763 * @brief calculate the velocity
764 * @param time the timeslice since the last frame
765*/
766
767void SpaceShip::movement (float dt)
768{
769  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
770  //the ship doesn't slide too much.
771  float airCoeff = 2.5;
772  float pi = 3.14;
773
774  switch(this->getPlaymode())
775  {
776    case Playable::Horizontal:
777    {
778      // these routines will change the travel movement into zero in a short amout of time, if the player
779      // doesn't press any buttons.
780      if (this->travelVelocity.x >= 0)
781      {
782        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
783          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
784        else
785          this->travelVelocity.x = 0;
786      }
787      else
788      {
789        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
790          this->travelVelocity.x += airCoeff* this->acceleration * dt;
791        else
792          this->travelVelocity.x = 0;
793      }
794      if (this->travelVelocity.z >= 0)
795      {
796        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
797          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
798        else
799          this->travelVelocity.z = 0;
800      }
801      else
802      {
803        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
804          this->travelVelocity.z += airCoeff* this->acceleration * dt;
805        else
806          this->travelVelocity.z = 0;
807      }
808
809      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
810      // and its continuing to slide a bit.
811      Vector oldCoor = this->getRelCoor();
812      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
813      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
814      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
815      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
816
817      if( this->systemFailure() )
818        bForward = bBackward = bLeft = bRight = false;
819
820      if( this->bForward )
821      {
822        //printf("ShipCoorX: %f \n", this->getRelCoor().x);
823        if(this->getRelCoor().x < this->travelDistancePlus.x)
824        {
825          if (this->travelVelocity.x < this->travelSpeed)
826          {
827            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
828          }
829          else
830          {
831            this->travelVelocity.x = this->travelSpeed;
832          }
833        }
834        else
835        {
836          this->travelVelocity.x = 0.0f;
837        }
838      }
839
840      if( this->bBackward )
841      {
842        if(this->getRelCoor().x > this->travelDistanceMinus.x)
843        {
844          if (this->travelVelocity.x > -this->travelSpeed)
845          {
846            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
847          }
848          else
849          {
850            this->travelVelocity.x = -this->travelSpeed;
851          }
852        }
853        else
854        {
855          this->travelVelocity.x = 0.0f;
856        }
857      }
858
859      if( this->bLeft)
860      {
861        if(this->getRelCoor().z > this->travelDistanceMinus.y)
862        {
863          if (this->travelVelocity.z > -this->travelSpeed)
864          {
865            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
866          }
867          else
868          {
869            this->travelVelocity.z = -this->travelSpeed;
870          }
871        }
872        else
873        {
874          this->travelVelocity.z = 0.0f;
875        }
876        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
877      }
878
879      if( this->bRight)
880      {
881        //printf("ShipCoorZ: %f \n", this->getRelCoor().z);
882        if(this->getRelCoor().z < this->travelDistancePlus.y)
883        {
884          if (this->travelVelocity.z < this->travelSpeed)
885          {
886            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
887          }
888          else
889          {
890            this->travelVelocity.z = this->travelSpeed;
891          }
892        }
893        else
894        {
895          this->travelVelocity.z = 0.0f;
896        }
897        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
898      }
899      if (!this->bRight && !this->bLeft)
900      {
901        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
902      }
903
904    //normalisation of the vectors (vector sum must be <= travelspeed)
905    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
906    if (xzNorm > this->travelSpeed)
907    {
908      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
909      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
910    }
911
912    //this moves camera and ship along the travel path.
913    if(!this->entityTrack)
914       this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
915
916    break;
917    }
918    case Playable::Vertical:
919    {
920      if ( !entityTrack || !entityTrack->getActionBox() )
921      {
922        break;
923      }
924      ActionBox * box = entityTrack->getActionBox();
925
926      this->travelVelocity = Vector(0, 0, 0);
927      float ssss = 50;
928      if ( this->bForward )
929      {
930        this->travelVelocity += Vector( 0, ssss, 0 );
931      }
932      if ( this->bBackward )
933      {
934        this->travelVelocity += Vector( 0, -ssss, 0 );
935      }
936      if ( this->bLeft )
937      {
938        this->travelVelocity += Vector( 0, 0, -ssss );
939      }
940      if ( this->bRight )
941      {
942        this->travelVelocity += Vector( 0, 0, ssss );
943      }
944
945      Vector ds = this->travelVelocity*dt;
946      Vector newPos = this->getRelCoor() + ds;
947      if ( newPos.y < -(box->getHeight_2()) || newPos.y > box->getHeight_2() )
948      {
949        this->travelVelocity.y = 0;
950      }
951      if ( newPos.z > box->getWidth_2() || newPos.z < -(box->getWidth_2()) )
952      {
953        this->travelVelocity.z = 0;
954      }
955    }
956      break;
957    default:
958      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
959  }
960   //set new coordinates calculated through key- events.
961  this->shiftCoor (this->travelVelocity * dt);
962}
963
964void SpaceShip::setPlaymodeXML(const std::string& playmode)
965{
966  this->setPlaymode(Playable::stringToPlaymode(playmode));
967}
968
969/**
970 * @brief jumps to the next WeaponConfiguration
971 */
972void SpaceShip::nextWeaponConfig()
973{
974  PRINTF(0)("Requested next weapon config!\n");
975  this->weaponMan.nextWeaponConfig();
976  Playable::weaponConfigChanged();
977}
978
979/**
980 * @brief moves to the last WeaponConfiguration
981 */
982void SpaceShip::previousWeaponConfig()
983{
984  /*
985  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
986  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
987  */
988  this->weaponMan.previousWeaponConfig();
989  Playable::weaponConfigChanged();
990}
991
992// void SpaceShip::updateElectronicWidget()
993// {
994//   if (this->electronicWidget != NULL)
995//   { //if it exists already: update it
996//      this->electronicWidget->setMaximum(this->electronicMax);
997//      this->electronicWidget->setValue(this->electronicCur);
998//   }
999//   else
1000//   { //create the widget
1001//     this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1002//     this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1003//     //this->electronicWidget->setDisplayedName("Electronics:");
1004//     //this->electronicWidget->setSize2D(100,20);
1005//     //this->electronicWidget->setAbsCoor2D(150,200);
1006//     this->updateElectronicWidget();
1007//     if (this->hasPlayer())
1008//       State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1009//   }
1010// }
1011//
1012// void SpaceShip::updateShieldWidget()
1013// {
1014//   if (this->shieldWidget != NULL)
1015//   {
1016//     this->shieldWidget->setMaximum(this->shieldMax);
1017//     this->shieldWidget->setValue(this->shieldCur);;
1018//   }
1019//   else
1020//   {
1021//     this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1022//     this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1023//     //this->shieldWidget->setDisplayedName("Shield:");
1024//     //his->shieldWidget->setSize2D(100,20);
1025//     //this->shieldWidget->setAbsCoor2D(200,200);
1026//     this->updateShieldWidget();
1027//     if (this->hasPlayer())
1028//       State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1029//   }
1030// }
1031
1032
1033void SpaceShip::setCameraDistance(float dist)
1034{
1035  Camera* c = State::getCamera();
1036  c->setViewTopDistance(dist);
1037
1038  if (this->hasPlayer())
1039    this->isTravelDistanceInit = false;
1040}
1041
1042void SpaceShip::setCameraFovy(float fovy)
1043{
1044
1045  Camera* c = State::getCamera();
1046  c->setViewTopFovy(fovy);
1047
1048  if (this->hasPlayer())
1049    this->isTravelDistanceInit = false;
1050}
1051
1052void SpaceShip::updateTravelDistance()
1053{
1054
1055  Camera* c = State::getCamera();
1056
1057  float x = 1.25 * this->actionWidthPercentage * fabsf(c->getAbsCoor().y) * tan(c->getFovy()*M_PI /360.0);
1058  float y = x / c->getAspectRatio() / this->actionWidthPercentage;
1059  //State::getCamera()->setAbsCoor(-5, 1000, 0);
1060
1061
1062  //State::getCamera()->getAbsCoor().print();
1063  //printf("CameraRelCoorY: %f \n", State::getCamera()->getRelCoor().y);
1064
1065  //printf("x: %f, y: %f \n", x, y);
1066  this->travelDistancePlus = Vector2D(y, x);
1067  this->travelDistanceMinus = Vector2D(-y, -x);
1068
1069  State::getPlayer()->hud().setOverlayPercentage(100-int(100*this->actionWidthPercentage));
1070//   PRINTF(0)("TravelDistance has been updated\n");
1071  this->isTravelDistanceInit = true;
1072}
1073
1074void SpaceShip::setActionWidthPercentage(int i)
1075{
1076  if (i>100) i=100;
1077  if (i<0) i=0;
1078  this->actionWidthPercentage = i/100.0;
1079
1080  if (this->hasPlayer())
1081    this->isTravelDistanceInit = false;
1082};
1083
1084void SpaceShip::hit( float damage, WorldEntity * killer )
1085{
1086  printf("SS HIT: %f, %f \n", this->getHealth() , this->getShield());
1087  Playable::hit( damage, killer );
1088  PRINTF(4)("SS HIT: %f\n", this->getHealth());
1089}
1090
1091void SpaceShip::destroy( WorldEntity * killer )
1092{
1093  Playable::destroy( killer );
1094  this->showDeadScreen();
1095  myList = this->getOMListNumber();
1096  toList( OM_DEAD );
1097}
1098
1099void SpaceShip::onButtonContinue( )
1100{
1101  OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true );
1102  EventHandler::getInstance()->popState();
1103  WorldEntity::reset();
1104  toList( myList );
1105  deadBox->hideAll();
1106}
1107
1108void SpaceShip::onButtonExit( )
1109{
1110  State::getCurrentStoryEntity()->setNextStoryID( 0 );
1111  State::getCurrentStoryEntity()->stop();
1112}
1113
1114void SpaceShip::showDeadScreen( )
1115{
1116  EventHandler::getInstance()->pushState( ES_MENU );
1117
1118  OrxGui::GLGuiHandler::getInstance()->activateCursor();
1119 
1120  if ( deadBox )
1121    delete deadBox;
1122  deadBox = new OrxGui::GLGuiBox();
1123  deadBox->setAbsCoor2D( 100, 100 );
1124
1125  OrxGui::GLGuiText* text = new OrxGui::GLGuiText();
1126  text->setText( "Game Over! - You died!" );
1127  OrxGui::GLGuiPushButton* exitButton = new OrxGui::GLGuiPushButton( "exit" );
1128  exitButton->released.connect(this, &SpaceShip::onButtonExit);
1129  OrxGui::GLGuiPushButton* continueButton = new OrxGui::GLGuiPushButton( "continue" );
1130  continueButton->released.connect(this, &SpaceShip::onButtonContinue);
1131
1132  deadBox->pack( text );
1133  deadBox->pack( continueButton );
1134  deadBox->pack( exitButton );
1135
1136  deadBox->showAll();
1137}
1138
1139
1140
Note: See TracBrowser for help on using the repository browser.