Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/space_ships/space_ship.cc @ 10030

Last change on this file since 10030 was 10030, checked in by marcscha, 18 years ago

Second Weapon Ammo Widget Support

File size: 27.8 KB
RevLine 
[6868]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
[7193]22#include "util/loading/resource_manager.h"
[6868]23
24#include "weapons/test_gun.h"
[9970]25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
[10023]28#include "weapons/swarm_launcher.h"
[9970]29#include "weapons/boomerang_gun.h"
[6868]30#include "weapons/turret.h"
31#include "weapons/cannon.h"
32
[9869]33#include "particles/dot_emitter.h"
34#include "particles/sprite_particles.h"
[6868]35
[7193]36#include "util/loading/factory.h"
[6868]37#include "key_mapper.h"
38
39#include "network_game_manager.h"
[8708]40#include "shared_network_data.h"
[6868]41
42#include "power_ups/weapon_power_up.h"
43#include "power_ups/param_power_up.h"
44
45#include "graphics_engine.h"
46
47#include "plane.h"
48
49#include "state.h"
50#include "player.h"
51
[7193]52#include "util/loading/load_param.h"
[10020]53#include "time.h"
[6868]54
[7056]55
[6868]56// #include "lib/gui/gl_gui/glgui_bar.h"
57// #include "lib/gui/gl_gui/glgui_pushbutton.h"
58
59
[9869]60#include "class_id_DEPRECATED.h"
61ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP);
62CREATE_FACTORY(SpaceShip);
[9406]63
[9061]64#include "script_class.h"
[9869]65CREATE_SCRIPTABLE_CLASS(SpaceShip,
66                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
67                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
68                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
69                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
70                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
71                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
72                        //Coordinates
73                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
74                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
75                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
76                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
[10017]77                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
[9061]78                       );
[6868]79
80/**
81 *  destructs the spaceship, deletes alocated memory
82 */
83SpaceShip::~SpaceShip ()
84{
[6986]85  this->setPlayer(NULL);
[6868]86}
87
88/**
89 * loads a Spaceships information from a specified file.
90 * @param fileName the name of the File to load the spaceship from (absolute path)
91 */
[7221]92SpaceShip::SpaceShip(const std::string& fileName)
[10019]93    : secWeaponMan(this) //,
94    //supportedPlaymodes(Playable::Vertical) ,
95    //playmode(Playable::Vertical)
[6868]96{
97  this->init();
98  TiXmlDocument doc(fileName);
99
100  if(!doc.LoadFile())
101  {
[7221]102    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
[6868]103    return;
104  }
105
106  this->loadParams(doc.RootElement());
107}
108
109/**
110 *  creates a new Spaceship from Xml Data
111 * @param root the xml element containing spaceship data
112
113   @todo add more parameters to load
114*/
115SpaceShip::SpaceShip(const TiXmlElement* root)
[10019]116    : secWeaponMan(this) //,
117    //supportedPlaymodes(Playable::Vertical) ,
118    //playmode(Playable::Vertical)
[6868]119{
120  this->init();
121  if (root != NULL)
122    this->loadParams(root);
123
124}
125
126
127/**
128 * initializes a Spaceship
129 */
130void SpaceShip::init()
131{
[9998]132
[10022]133  srand(time(0));   //initialize Random Nomber Generator
134
[10019]135  this->setSupportedPlaymodes(Playable::Vertical);
136
[9869]137  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
138  this->registerObject(this, SpaceShip::_objectList);
[6868]139
140  PRINTF(4)("SPACESHIP INIT\n");
141
[9998]142  secWeaponMan.showCrosshair();
143
[6868]144  //weapons:
[9998]145
[9996]146  Weapon* wpRight1 = new LightBlaster ();
[10030]147  wpRight1->setName("LightBlaster");
[9996]148  Weapon* wpLeft1 = new LightBlaster ();
[10030]149  wpLeft1->setName("LightBlaster");
[6868]150
[9996]151  Weapon* wpRight2 = new MediumBlaster ();
[10030]152  wpRight2->setName("MediumBlaster");
[9996]153  Weapon* wpLeft2 = new MediumBlaster ();
[10030]154  wpLeft2->setName("MediumBlaster");
[9996]155
[10030]156  Weapon* wpRight3 = new HeavyBlaster ();
157  wpRight3->setName("HeavyBlaster");
[9996]158  Weapon* wpLeft3 = new HeavyBlaster ();
[10030]159  wpLeft3->setName("HeavyBlaster");
[9996]160
[10023]161  Weapon* cannon = new SwarmLauncher();
162  cannon->setName("SwarmLauncher");
[10004]163 
[6868]164
[9961]165
[10016]166  this->weaponMan.addWeapon( wpLeft1, 0, 0);
167  this->weaponMan.addWeapon( wpRight1, 0, 1);
168  this->weaponMan.addWeapon( wpLeft2, 0, 2);
169  this->weaponMan.addWeapon( wpRight2, 0, 3);
[10030]170  this->weaponMan.addWeapon( wpLeft3, 0, 4);
171  this->weaponMan.addWeapon( wpRight3, 0, 5);
[9998]172
[10023]173  this->secWeaponMan.addWeapon( cannon, 1, 0);
[9961]174
[9965]175  this->weaponMan.changeWeaponConfig(0);
[10023]176  this->secWeaponMan.changeWeaponConfig(1);
[9961]177
[9996]178  wpRight1->requestAction(WA_ACTIVATE);
179  wpLeft1->requestAction(WA_ACTIVATE);
180  wpRight2->requestAction(WA_ACTIVATE);
181  wpLeft2->requestAction(WA_ACTIVATE);
[10030]182  wpRight3->requestAction(WA_ACTIVATE);
183  wpLeft3->requestAction(WA_ACTIVATE);
[9998]184
[9961]185  cannon->requestAction(WA_ACTIVATE);
[9965]186
[9970]187  curWeaponPrimary    = 0;
188  curWeaponSecondary  = 0;
[9965]189
190  Playable::weaponConfigChanged();
191
[9970]192  reactorOutput     = 10;
[9965]193
[9970]194  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
195  engineSpeedBase   = 5;
196  shieldRegen       = 2;
197
198  shieldEnergyShare = 0.3;
199  weaponEnergyShare = 0.3;
200  engineEnergyShare = 0.4;
201
202  shieldCur         = 20;
203  shieldMax         = 100;
[10020]204  shieldTH          = .2* shieldMax;   // shield power must be 20% before shield kicks in again
[9970]205
[10024]206  electronicCur = 50;
[10022]207  electronicMax = 50;
[10020]208  electronicRegen   = 3;
209  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
210
[9961]211  /*
[6868]212  this->addWeapon(wpLeft, 1, 0);
213  this->addWeapon(wpRight,1 ,1);
[7087]214  //this->addWeapon(cannon, 0, 6);
[6868]215
[7337]216  this->getWeaponManager().changeWeaponConfig(1);
[9961]217  */
[6868]218
[9970]219  this->loadModel("models/ships/mantawing.obj");
[10017]220  //this->setVisibiliy(false);
[6868]221
[10017]222  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
223 
224  /*
[6868]225  xMouse = yMouse = 0;
226  yInvert = 1;
227  mouseSensitivity = 0.001;
[7122]228  airViscosity = 0.9;
[6868]229  controlVelocityX = 25;
230  controlVelocityY = 150;
231  shipInertia = 1.5;
[10017]232  */
[9869]233  //  cycle = 0.0;
[6868]234
[9970]235  this->setHealthMax(shieldMax);
236  this->setHealth(shieldCur);
[6868]237
[10017]238  this->travelNode = new PNode();
239
240  // camera - issue
241  //this->cameraNode = State::getCameraNode();
242  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
243  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
244
245  /*
[6868]246  this->velocity = this->getAbsDirX()*travelSpeed;
247  this->mouseDir = this->getAbsDir();
248  this->pitchDir = this->getAbsDir();
[10017]249  */
[6868]250
[9965]251
[9869]252  //   GLGuiButton* button = new GLGuiPushButton();
253  //    button->show();
254  //    button->setLabel("orxonox");
255  //    button->setBindNode(this);
256  //     GLGuiBar* bar = new GLGuiBar();
257  //     bar->show();
258  //     bar->setValue(7.0);
259  //     bar->setMaximum(10);
260  //     bar->setSize2D( 20, 100);
261  //     bar->setAbsCoor2D( 10, 200);
[6868]262
263  //add events to the eventlist
[6997]264  registerEvent(KeyMapper::PEV_FORWARD);
265  registerEvent(KeyMapper::PEV_BACKWARD);
[6868]266  registerEvent(KeyMapper::PEV_LEFT);
267  registerEvent(KeyMapper::PEV_RIGHT);
268  //registerEvent(SDLK_q);
269  //registerEvent(SDLK_e);
270  registerEvent(KeyMapper::PEV_FIRE1);
[9958]271  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
[6868]272  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
273  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
274  //registerEvent(SDLK_PAGEUP);
275  //registerEvent(SDLK_PAGEDOWN);
276  registerEvent(EV_MOUSE_MOTION);
277
[9965]278  this->weaponMan.setSlotCount(6);
[6868]279
[9965]280  this->weaponMan.setSlotPosition(0, Vector(-2.6, .1, -3.0));
281  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]282
[9965]283  this->weaponMan.setSlotPosition(1, Vector(-2.6, .1, 3.0));
284  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]285
[9965]286  this->weaponMan.setSlotPosition(2, Vector(-1.5, .5, -.5));
287  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[6868]288
[9965]289  this->weaponMan.setSlotPosition(3, Vector(-1.5, .5, .5));
290  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[6868]291
[9965]292  this->weaponMan.setSlotPosition(4, Vector(-1.5, -.5, .5));
293  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
[6868]294
[9965]295  this->weaponMan.setSlotPosition(5, Vector(-1.5, -.5, -.5));
296  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9961]297
298  this->secWeaponMan.setSlotCount(6);
299
[10004]300  this->secWeaponMan.setSlotPosition(0, Vector(1.5, -1, 0));
[9961]301  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
302
303  this->secWeaponMan.setSlotPosition(1, Vector(2.6, .1, 3.0));
304  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
305
306  this->secWeaponMan.setSlotPosition(2, Vector(1.5, .5, -.5));
307  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
308
309  this->secWeaponMan.setSlotPosition(3, Vector(1.5, .5, .5));
310  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
311
312  this->secWeaponMan.setSlotPosition(4, Vector(1.5, -.5, .5));
313  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
314
315  this->secWeaponMan.setSlotPosition(5, Vector(1.5, -.5, -.5));
316  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9965]317
[9869]318  //
319  //   this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
320  //   this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
321  //
322  //   this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
323  //   this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
[6868]324
[9965]325  this->weaponMan.getFixedTarget()->setParent(this);
326  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]327
[9998]328 
[9961]329  this->secWeaponMan.getFixedTarget()->setParent(this);
330  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]331  this->secWeaponMan.setRotationSpeed(0);
[6868]332
[9965]333  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
[9961]334
[6868]335  this->burstEmitter = new DotEmitter(200, 0.0, .01);
336  this->burstEmitter->setParent(this);
337  this->burstEmitter->setRelCoor(-1, .5, 0);
338  this->burstEmitter->setName("SpaceShip_Burst_emitter");
339
340  this->burstSystem = new SpriteParticles(1000);
341  this->burstSystem->addEmitter(this->burstEmitter);
342  this->burstSystem->setName("SpaceShip_Burst_System");
343  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
344  this->burstSystem->setLifeSpan(1.0, .3);
345  this->burstSystem->setRadius(0.0, 1.0);
346  this->burstSystem->setRadius(0.05, 1.0);
347  this->burstSystem->setRadius(.5, .8);
348  this->burstSystem->setRadius(1.0, 0);
349  this->burstSystem->setColor(0.0, .7,.7,1,.7);
350  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
351  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
352  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
[9008]353
[10017]354  /*
[7954]355  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) );
356  registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) );
357  registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) );
358  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
359  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
360  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
361  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
362  registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) );
363  registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) );
[10017]364  */
365
366  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
367  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
368  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
369  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
370  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
371  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
[10023]372  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
[10017]373
374  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
375
376  //this->airFriction = 0.5f;
[10019]377  this->travelDistancePlus = Vector2D(38.0, 43.0);
378  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
379  this->travelSpeed = 80;
[10017]380  this->cameraSpeed = 40;
[10019]381  this->cameraLook = 0.0f;
382  this->acceleration = 120.0f;
383  //this->airFriction = 0.0f;
[10017]384
[10020]385  srand(time(0));  //initaialize RNG
386
[10017]387  this->travelNode->debugDraw();
[6868]388}
389
390
391/**
392 * loads the Settings of a SpaceShip from an XML-element.
393 * @param root the XML-element to load the Spaceship's properties from
394 */
395void SpaceShip::loadParams(const TiXmlElement* root)
396{
397  Playable::loadParams(root);
398}
399
[10017]400void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
[7056]401{
[10017]402  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
[7056]403}
[6868]404
[10017]405void SpaceShip::setTravelSpeed(float travelSpeed)
406{
407  this->travelSpeed = travelSpeed;
408}
[7056]409
[10017]410/*
411void SpaceShip::setTravelHeight(float travelHeight)
412{
413  if (this->toTravelHeight == NULL)
414    this->toTravelHeight = new float;
415  *this->toTravelHeight = travelHeight;
416}
417*/
418
[10019]419/*
[10017]420void SpaceShip::setTravelDistance(const Vector2D& distance)
421{
422  this->travelDistance = distance;
423}
424
425void SpaceShip::setTravelDistance(float x, float y)
426{
427  this->setTravelDistance(Vector2D(x, y));
428}
[10019]429*/
[10017]430
431
432
[7085]433void SpaceShip::reset()
434{
[10017]435  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
[7056]436
[10017]437  //xMouse = yMouse = 0;
[7085]438
439  this->setHealth(80);
440  this->velocity = Vector(0.0, 0.0, 0.0);
441}
442
443
[6868]444void SpaceShip::enter()
445{
[10004]446  this->secWeaponMan.showCrosshair();
447  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
[10019]448  if (!this->setPlaymode(Playable::Vertical))
449    std::cout << "playmode not supported";
450  //this->attachCamera();
[6868]451}
452
453void SpaceShip::leave()
454{
[10004]455  this->secWeaponMan.hideCrosshair();
456  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
[10019]457  //this->detachCamera();
[6868]458}
459
460
461/**
462 *  effect that occurs after the SpaceShip is spawned
463*/
464void SpaceShip::postSpawn ()
465{
466  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
467}
468
469/**
470 *  the action occuring if the spaceship left the game
471*/
472void SpaceShip::leftWorld ()
473{}
474
475WorldEntity* ref = NULL;
476/**
477 *  this function is called, when two entities collide
478 * @param entity: the world entity with whom it collides
479 *
480 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
481 */
482void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
483{
484}
485
486/**
487 *  draws the spaceship after transforming it.
488*/
489void SpaceShip::draw () const
490{
491  WorldEntity::draw();
492
493  //this->debug(0);
494}
495
496/**
497 *  the function called for each passing timeSnap
498 * @param time The timespan passed since last update
499*/
500void SpaceShip::tick (float time)
501{
[9961]502  // Playable::tick(time);$
[6868]503
[9961]504  // Own Tick Setup, as a different fire routine is used on the weapon manager
[9965]505  this->weaponMan.tick(time);
[9961]506  this->secWeaponMan.tick(time);
507
[10020]508  if( this->systemFailure() )
509    bFire = bSecFire = false;
510
[9965]511  if( this->bFire)
[9961]512  {
[9965]513    this->weaponMan.fire();
[9961]514  }
515  if( this->bSecFire)
516  {
517    this->secWeaponMan.fire();
[9965]518    this->bSecFire = !this->bSecFire;   // FIXME This currently is needed to prevent "echo fires" of a second rocket after its cooldown has passed
[9961]519  }
520
[9970]521
522  // Shield Regeneration and other regular calculations on the ship
523  this->regen(time);
524
525  // Weapon Regeneration and other regular calculations on the ship
[9965]526  this->weaponRegen(time);
527
[9970]528  // current engine speed output
529  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
530
531
[10017]532  /*
[8708]533  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
[9869]534  {
[6868]535    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
536    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
537    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
538    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
539
540    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
541
542    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
543    xMouse = yMouse = 0;
[9869]544  }
[10017]545*/
[6868]546
[9235]547  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
[7954]548  //if (this->getOwner() == this->getHostID())
[6868]549
[10017]550  //is->calculateVelocity(time);
[6959]551
[6868]552
[10017]553  //vector move = velocity*time;
554
555  /*
[6868]556  //orient the velocity in the direction of the spaceship.
557  travelSpeed = velocity.len();
558  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
559  velocity = (velocity.getNormalized())*travelSpeed;
[10017]560  */
561  this->movement(time);
562
563   // TRYING TO FIX PNode.
564  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
565  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
566
[6868]567  this->burstEmitter->setEmissionRate(travelSpeed);
568  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
569
570  //orient the spaceship in direction of the mouse
[10017]571  /*
[9869]572  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
573  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6868]574    this->setAbsDir( rotQuat);
[9869]575  //this->setAbsDirSoft(mouseDir,5);
[10017]576  */
[6868]577
578  // this is the air friction (necessary for a smooth control)
[10017]579  /*
[6868]580  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
581  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
[10017]582  */
[6868]583
584  //other physics (gravity)
585  //if(travelSpeed < 120)
586  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
587
588  //hoover effect
589  //cycle += time;
590  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
591
592  //readjust
593  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
594  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
595
596  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
597
[10017]598  /*
[6868]599  this->shiftCoor(move);
[10017]600  */
[6868]601
[9869]602  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
[6868]603
604}
605
[10017]606/*
[6868]607void SpaceShip::calculateVelocity (float time)
608{
609  Vector accel(0.0, 0.0, 0.0);
[10017]610  /* calculate the direction in which the craft is heading  */
[6868]611  /*
612  if( this->bUp )
[9869]613  {
614    accel += (this->getAbsDirX())*acceleration;
[6868]615
[9869]616  }
[6868]617
618  if( this->bDown )
[9869]619  {
620    accel -= (this->getAbsDirX())*0.5*acceleration;
[6868]621
[9869]622  }
[6868]623
[10017]624  if( this->bLeft)
[6868]625  {
626    this->shiftDir(Quaternion(time, Vector(0,1,0)));
627  }
[10017]628  if( this->bRight)
[6868]629  {
630    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
631  }
632
633
[10017]634  if( this->bRollL)
[6868]635  {
636    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
637  }
[10017]638  if( this->bRollR)
[6868]639  {
640    mouseDir *= Quaternion(time*2, Vector(1,0,0));
641
642  }
643  if (this->bAscend )
644  {
645    this->shiftDir(Quaternion(time, Vector(0,0,1)));
646  }
647  if (this->bDescend )
648  {
649    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
650  }
651
[6959]652  velocity += accel*time*10;
[9961]653
[6868]654}
[10017]655*/
[6868]656
657/**
658 * @todo switch statement ??
659 */
660void SpaceShip::process(const Event &event)
661{
[9961]662  //Playable::process(event);
[6868]663
664  if( event.type == KeyMapper::PEV_LEFT)
[10017]665    this->bLeft = event.bPressed;
[6868]666  else if( event.type == KeyMapper::PEV_RIGHT)
[10017]667    this->bRight = event.bPressed;
[6997]668  else if( event.type == KeyMapper::PEV_FORWARD)
[10017]669    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6997]670  else if( event.type == KeyMapper::PEV_BACKWARD)
[10017]671    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[9961]672  else if( event.type == KeyMapper::PEV_FIRE2)
673    this->bSecFire = event.bPressed;
[9958]674  else if( event.type == KeyMapper::PEV_FIRE1)
[9965]675    this->bFire = event.bPressed;
[10017]676
677
678  /*
[6868]679  else if( event.type == EV_MOUSE_MOTION)
680  {
[9961]681
[6868]682    this->xMouse += event.xRel;
683    this->yMouse += event.yRel;
684  }
[10017]685  */
[6868]686}
687
[9235]688void SpaceShip::destroy( WorldEntity* killer )
[9008]689{
690  PRINTF(0)("spaceship destroy\n");
691}
[6868]692
[9008]693void SpaceShip::respawn( )
694{
695  toList( OM_PLAYERS );
696}
[6868]697
698
[9957]699void SpaceShip::damage(float pDamage, float eDamage){
[9950]700if( this->shieldActive) {
701    if( this->shieldCur > pDamage) {
702      this->shieldCur = this->shieldCur - pDamage;
703    }
704    else { // shield <= pDamage
[9987]705      this->shieldCur -=pDamage;
706      this->shieldActive = false; //shield collapses
[9950]707      pDamage = pDamage - this->shieldCur;
708      if( !this->shieldActive) {
[9957]709        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
[9953]710        this->electronicCur -= eDamage;
[9950]711      }
712    }
713  }
714  else {
715    this->armorCur = this->armorCur - pDamage;
716    this->electronicCur = this->electronicCur - eDamage;
717  }
718  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
719}
720
[9987]721
[9957]722void SpaceShip::regen(float time){
723  float tmp;
[9963]724  if (this->armorCur != this->armorMax || this->armorRegen != 0){
725    tmp = this->armorCur + this->armorRegen * time;
726    if ( tmp > electronicMax)
727      this->armorCur = this->armorMax;
728    else
729      this->armorCur = tmp;
730  }
731  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
[9970]732    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
[9957]733    if( tmp > shieldMax)
[9953]734      this->shieldCur = this->shieldMax;
735    else
[9957]736      this->shieldCur = tmp;
737    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
[9953]738  }
[9970]739
740  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
741
[9963]742  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
[9957]743    tmp = this->electronicCur + this->electronicRegen * time;
744    if ( tmp > electronicMax)
[9953]745      this->electronicCur = this->electronicMax;
746    else
[9957]747      this->electronicCur = tmp;
[9953]748  }
[9950]749}
[9965]750
[9987]751
[9965]752/**
753 * Weapon regeneration
754 * does not use any reactor capacity, as it wouldn't work in a consistent way.
755 */
756void SpaceShip::weaponRegen(float time)
757{
758  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
759  Weapon* weapon;
[9987]760  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
[9965]761  {
762    weapon = this->weaponMan.getWeapon(i);
763    if( weapon != NULL && weapon->isActive())
764    {
765      weapon->increaseEnergy( energy);
766    }
767
768  }
769  // weaponMan.increaseAmmunition( weapon, energy);
770}
[9975]771
[9987]772
[9975]773void SpaceShip::enterPlaymode(Playable::Playmode playmode)
774{
[10017]775  switch(playmode)
776  {
777    case Playable::Full3D:
778      /*
779      if (State::getCameraNode != NULL)
780      {
781        Vector absCoor = this->getAbsCoor();
782        this->setParent(PNode::getNullParent());
783        this->setAbsCoor(absCoor);
784        State::getCameraNode()->setParentSoft(&this->cameraNode);
785        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
786        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
787        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
788
789      }
790      */
791      //break;
792
793
794    case Playable::Vertical:
795      if (State::getCameraNode != NULL)
796      {
797        this->debugNode(1);
798        this->travelNode->debugNode(1);
799
800        this->travelNode->setAbsCoor(this->getAbsCoor());
801        this->travelNode->updateNode(0.01f);
802
803        this->setParent(this->travelNode);
804        this->setRelCoor(0,0,0);
805
806        State::getCameraNode()->setParentSoft(this->travelNode);
[10019]807        State::getCameraNode()->setRelCoorSoft(-0.01, 40,0);
[10017]808        State::getCameraTargetNode()->setParentSoft(this->travelNode);
[10019]809        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
[10017]810
811        this->debugNode(1);
812        this->travelNode->debugNode(1);
813      }
814      break;
815
816    default:
817      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
818  }
[10019]819  std::cout << Playable::playmodeToString(this->getPlaymode());
[9975]820}
821
[10017]822/**
823 * @brief calculate the velocity
824 * @param time the timeslice since the last frame
825*/
[9987]826
[9975]827void SpaceShip::movement (float dt)
828{
[10019]829  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
830  //the ship doesn't slide too much.
831  float airCoeff = 2.5;
832
833  float pi = 3.14;
834
835  // these routines will change the travel movement into zero in a short amout of time, if the player
836  // doesn't press any buttons.
837  if (this->velocity.x >= 0)
838  {
839    if (this->velocity.x > airCoeff*this->acceleration * dt)
840      this->velocity.x -= airCoeff* this->acceleration * dt;
[10017]841    else
[10019]842      this->velocity.x = 0;
843  }
844  else
845  {
846    if (this->velocity.x < -airCoeff*this->acceleration * dt)
847      this->velocity.x += airCoeff* this->acceleration * dt;
848    else
849      this->velocity.x = 0;
850  }
851  if (this->velocity.z >= 0)
852  {
853    if (this->velocity.z > airCoeff*this->acceleration * dt)
854      this->velocity.z -= airCoeff* this->acceleration * dt;
855    else
856      this->velocity.z = 0;
857  }
858  else
859  {
860    if (this->velocity.z < -airCoeff*this->acceleration * dt)
861      this->velocity.z += airCoeff* this->acceleration * dt;
862    else
863      this->velocity.z = 0;
864  }
[10017]865
[10019]866  // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
867  // and its continuing to slide a bit.
868  Vector oldCoor = this->getRelCoor();
869  if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
870  if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
871  if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
872  if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
873
[10020]874  if( this->systemFailure() )
875    bForward = bBackward = bLeft = bRight = false;
876
877
[10017]878  if( this->bForward )
879  {
[10019]880    if(this->getRelCoor().x < this->travelDistancePlus.x)
[10017]881    {
882      if (this->velocity.x < this->travelSpeed)
883      {
[10019]884        this->velocity.x += (airCoeff + 1.0)*this->acceleration*dt;
[10017]885      }
886      else
887      {
888        this->velocity.x = this->travelSpeed;
889      }
890    }
891    else
892    {
893      this->velocity.x = 0.0f;
894    }
895  }
896
897  if( this->bBackward )
898  {
[10019]899    if(this->getRelCoor().x > this->travelDistanceMinus.x)
[10017]900    {
901      if (this->velocity.x > -this->travelSpeed)
902      {
[10019]903        this->velocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
[10017]904      }
905      else
906      {
907        this->velocity.x = -this->travelSpeed;
908      }
909    }
910    else
911    {
912      this->velocity.x = 0.0f;
913    }
914  }
[10019]915
[10017]916  if( this->bLeft)
917  {
[10019]918    if(this->getRelCoor().z > this->travelDistanceMinus.y)
919    {
920      if (this->velocity.z > -this->travelSpeed)
921      {
922        this->velocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
923      }
924      else
925      {
926        this->velocity.z = -this->travelSpeed;
927      }
928    }
929    else
930    {
931      this->velocity.z = 0.0f;
932    }
[10017]933  }
934
935  if( this->bRight)
936  {
[10019]937    if(this->getRelCoor().z < this->travelDistancePlus.y)
938    {
939      if (this->velocity.z < this->travelSpeed)
940      {
941        this->velocity.z += (airCoeff + 1.0)*this->acceleration*dt;
942      }
943      else
944      {
945        this->velocity.z = this->travelSpeed;
946      }
947    }
948    else
949    {
950      this->velocity.z = 0.0f;
951    }
[10017]952  }
[10019]953 
954  this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
955  this->shiftCoor (this->velocity * dt);
956 
957  float angle = this->velocity.z / travelSpeed * pi / 3;
958  if (angle > pi/3) angle = pi/3;
959  if (angle < -pi/3) angle = -pi/3;
960  this->setRelDirSoft(Quaternion(angle, Vector(1,0,0)), 5.0f);
[10017]961
[10019]962  /*
[10017]963  switch(this->getPlaymode())
964  {
965    case Playable::Vertical:
966      break;
967    default:
968      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
969  }
[10019]970  */
[9975]971}
[10017]972
Note: See TracBrowser for help on using the repository browser.