Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/space_ship.cc @ 6506

Last change on this file since 6506 was 6506, checked in by bknecht, 18 years ago

Control: Some keys for SpaceShip and Helicopter can now be altered in orxonox.conf

File size: 14.4 KB
RevLine 
[5907]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
[6034]13   co-programmer: Silvan Nellen
[5907]14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
[6034]22#include "objModel.h"
[5907]23#include "resource_manager.h"
24
25#include "weapons/weapon_manager.h"
26#include "weapons/test_gun.h"
27#include "weapons/turret.h"
28#include "weapons/cannon.h"
29
30#include "factory.h"
31#include "key_mapper.h"
[5978]32#include "event_handler.h"
[5907]33
[6424]34#include "network_game_manager.h"
35
[6243]36#include "power_ups/weapon_power_up.h"
37#include "power_ups/param_power_up.h"
38
[5978]39#include "graphics_engine.h"
40
[6287]41
42
[6306]43// #include "lib/gui/gl_gui/glgui_bar.h"
44// #include "lib/gui/gl_gui/glgui_pushbutton.h"
[6287]45
46
[5907]47using namespace std;
48
49CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
50
51/**
52 *  creates the controlable Spaceship
53 */
54SpaceShip::SpaceShip()
55{
56  this->init();
57}
58
59/**
60 *  destructs the spaceship, deletes alocated memory
61 */
62SpaceShip::~SpaceShip ()
63{
64}
65
66/**
67 * loads a Spaceships information from a specified file.
68 * @param fileName the name of the File to load the spaceship from (absolute path)
69 */
70SpaceShip::SpaceShip(const char* fileName)
71{
72  this->init();
73  TiXmlDocument doc(fileName);
74
75  if(!doc.LoadFile())
76  {
77    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName);
78    return;
79  }
80
81  this->loadParams(doc.RootElement());
82}
83
84/**
85 *  creates a new Spaceship from Xml Data
86 * @param root the xml element containing spaceship data
87
88   @todo add more parameters to load
89*/
90SpaceShip::SpaceShip(const TiXmlElement* root)
91{
92  this->init();
93  if (root != NULL)
94    this->loadParams(root);
95
96  //weapons:
97  Weapon* wpRight = new TestGun(0);
98  wpRight->setName("testGun Right");
99  Weapon* wpLeft = new TestGun(1);
100  wpLeft->setName("testGun Left");
[5982]101  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
[5907]102
103  cannon->setName("BFG");
104
[6443]105  this->addWeapon(wpLeft, 1, 0);
106  this->addWeapon(wpRight,1 ,1);
107  this->addWeapon(cannon, 0, 6);
[5907]108
[6443]109  //this->addWeapon(turret, 3, 0);
[5907]110
111  this->getWeaponManager()->changeWeaponConfig(1);
112}
113
114
115/**
116 * initializes a Spaceship
117 */
118void SpaceShip::init()
119{
120//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
121  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
122
123  PRINTF(4)("SPACESHIP INIT\n");
124
[6426]125  EventHandler::getInstance()->grabEvents(true);
[5907]126
[5978]127  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
[5907]128  bFire = false;
[5978]129  xMouse = yMouse = 0;
130  mouseSensitivity = 0.001;
[6034]131  airViscosity = 1.0;
[5978]132  cycle = 0.0;
[5907]133
[6430]134  this->setMaxEnergy(100);
135  this->setEnergy(80);
[5982]136
[5907]137  travelSpeed = 15.0;
[6505]138  acceleration = 5.0;
139  this->velocity = this->getAbsDirX()*travelSpeed;
[6162]140  this->mouseDir = this->getAbsDir();
[5907]141
[6306]142//   GLGuiButton* button = new GLGuiPushButton();
143//    button->show();
144//    button->setLabel("orxonox");
145//    button->setBindNode(this);
146//     GLGuiBar* bar = new GLGuiBar();
147//     bar->show();
148//     bar->setValue(7.0);
149//     bar->setMaximum(10);
150//     bar->setSize2D( 20, 100);
151//     bar->setAbsCoor2D( 10, 200);
[6300]152
[5907]153  //add events to the eventlist
[6506]154  registerEvent(KeyMapper::PEV_UP);
155  registerEvent(KeyMapper::PEV_DOWN);
156  registerEvent(KeyMapper::PEV_LEFT);
157  registerEvent(KeyMapper::PEV_RIGHT);
158  //registerEvent(SDLK_q);
159  //registerEvent(SDLK_e);
[5907]160  registerEvent(KeyMapper::PEV_FIRE1);
161  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
162  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
[6506]163  //registerEvent(SDLK_PAGEUP);
164  //registerEvent(SDLK_PAGEDOWN);
[5978]165  registerEvent(EV_MOUSE_MOTION);
[5907]166
167  this->getWeaponManager()->setSlotCount(7);
168
169  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
170  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
171
172  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
173  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
174
175  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
176  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
177
178  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
179  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
180
181  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
182  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
183
184  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
185  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
186//
187   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
188   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
189   //
190//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
191//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
192//
193//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
194//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
195
[6162]196  this->getWeaponManager()->getFixedTarget()->setParent(this);
197  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
198
[6222]199  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[5907]200}
201
202/**
203 * loads the Settings of a SpaceShip from an XML-element.
204 * @param root the XML-element to load the Spaceship's properties from
205 */
206void SpaceShip::loadParams(const TiXmlElement* root)
207{
208  static_cast<WorldEntity*>(this)->loadParams(root);
209}
210
211
[6222]212void SpaceShip::enter()
213{
214  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
[6241]215  this->attachCamera();
[6222]216}
217
218void SpaceShip::leave()
219{
220  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[6241]221  this->detachCamera();
[6222]222}
223
[5907]224
225/**
226 *  effect that occurs after the SpaceShip is spawned
227*/
228void SpaceShip::postSpawn ()
229{
230  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
231}
232
233/**
234 *  the action occuring if the spaceship left the game
235*/
236void SpaceShip::leftWorld ()
237{}
238
239WorldEntity* ref = NULL;
240/**
241 *  this function is called, when two entities collide
242 * @param entity: the world entity with whom it collides
243 *
244 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
245 */
246void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
247{
[6436]248  Playable::collidesWith(entity, location);
[5907]249  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
250  {
251    this->ADDWEAPON();
252    ref = entity;
253    }
254//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
255}
256
257/**
258 *  draws the spaceship after transforming it.
259*/
260void SpaceShip::draw () const
261{
[6281]262  WorldEntity::draw();
[5907]263  this->getWeaponManager()->draw();
264
265  //this->debug(0);
266}
267
268/**
269 *  the function called for each passing timeSnap
270 * @param time The timespan passed since last update
271*/
272void SpaceShip::tick (float time)
273{
[6034]274
[5907]275  // spaceship controlled movement
276  this->calculateVelocity(time);
277
[6505]278  Vector move = velocity.getNormalized()*travelSpeed;
[5907]279
[6034]280  //orient the velocity in the direction of the spaceship.
281  travelSpeed = velocity.len();
282  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
283  velocity = (velocity.getNormalized())*travelSpeed;
[6222]284
[6162]285  //orient the spaceship in direction of the mouse
[6241]286   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
287   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6162]288    this->setAbsDir( rotQuat);
289   //this->setAbsDirSoft(mouseDir,5);
[6222]290
[5978]291  // this is the air friction (necessary for a smooth control)
292  if(velocity.len() != 0) velocity -= velocity*0.01;
[5982]293
[6034]294  //hoover effect
[6497]295  //cycle += time;
296  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
[5982]297
[6034]298  //readjust
[6426]299  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
[6034]300  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
301
[5978]302  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
[5907]303
304  this->shiftCoor (move);
305
306  this->getWeaponManager()->tick(time);
307  // weapon system manipulation
308  this->weaponAction();
309}
310
311/**
312 *  calculate the velocity
313 * @param time the timeslice since the last frame
314*/
315void SpaceShip::calculateVelocity (float time)
316{
[6505]317  //Vector accel(0.0, 0.0, 0.0);
[6162]318  /*
319  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
320  */
[5907]321  //float rotVal = 0.0;
[5912]322  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
[5907]323  /* calculate the direction in which the craft is heading  */
324
[6222]325  Plane plane(Vector(0,1,0), Vector(0,0,0));
326
[5907]327  if( this->bUp )
328   {
[5978]329     //this->shiftCoor(this->getAbsDirX());
[6505]330      //accel += (this->getAbsDirX())*2;
331      if(travelSpeed < 30) travelSpeed += acceleration*time;
[6222]332
[6162]333      /* Heli-Steuerung
334         accel += (this->getAbsDirX()*2;
335         if(
336      */
[5907]337   }
338
339  if( this->bDown )
[5908]340   {
[5978]341     //this->shiftCoor((this->getAbsDirX())*-1);
[6505]342     //accel -= (this->getAbsDirX())*2;
343     if(travelSpeed >= 0) travelSpeed -= acceleration*time;
[5907]344   }
345
[6034]346  if( this->bLeft/* > -this->getRelCoor().z*2*/)
[5907]347  {
[5912]348    this->shiftDir(Quaternion(time, Vector(0,1,0)));
349//    accel -= rightDirection;
[5908]350    //velocityDir.normalize();
[5907]351    //rot +=Vector(1,0,0);
352    //rotVal -= .4;
353  }
354  if( this->bRight /* > this->getRelCoor().z*2*/)
355  {
[5912]356    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
357
358    //    accel += rightDirection;
[5908]359    //velocityDir.normalize();
[5907]360    //rot += Vector(1,0,0);
361    //rotVal += .4;
362  }
[5982]363
[6034]364
[5978]365  if( this->bRollL /* > -this->getRelCoor().z*2*/)
366  {
[6162]367    mouseDir *= Quaternion(-time, Vector(1,0,0));
[5978]368//    accel -= rightDirection;
369    //velocityDir.normalize();
370    //rot +=Vector(1,0,0);
371    //rotVal -= .4;
372  }
373  if( this->bRollR /* > this->getRelCoor().z*2*/)
374  {
[6162]375    mouseDir *= Quaternion(time, Vector(1,0,0));
[5978]376
377    //    accel += rightDirection;
378    //velocityDir.normalize();
379    //rot += Vector(1,0,0);
380    //rotVal += .4;
381  }
[5907]382  if (this->bAscend )
383  {
[5912]384    this->shiftDir(Quaternion(time, Vector(0,0,1)));
385
386//    accel += upDirection;
[5908]387    //velocityDir.normalize();
[5907]388    //rot += Vector(0,0,1);
389    //rotVal += .4;
390  }
391  if (this->bDescend )
392  {
[5912]393    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
394
395    //    accel -= upDirection;
[5908]396    //velocityDir.normalize();
[5907]397    //rot += Vector(0,0,1);
398    //rotVal -= .4;
399  }
400
[6505]401  //velocity += accel;
[5907]402  //rot.normalize();
403  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
404}
405
406/**
407 * weapon manipulation by the player
408*/
409void SpaceShip::weaponAction()
410{
411  if( this->bFire)
412    {
413      this->getWeaponManager()->fire();
414    }
415}
416
417/**
418 * @todo switch statement ??
419 */
420void SpaceShip::process(const Event &event)
421{
[5982]422
423
[6506]424  if( event.type == KeyMapper::PEV_LEFT)
[5978]425      this->bRollL = event.bPressed;
[6506]426  else if( event.type == KeyMapper::PEV_RIGHT)
[5978]427      this->bRollR = event.bPressed;
[5907]428  else if( event.type == KeyMapper::PEV_FIRE1)
429      this->bFire = event.bPressed;
430  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
[6443]431  {
[6444]432    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
[6443]433  }
[5907]434  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
[6444]435    this->previousWeaponConfig();
[6506]436  else if( event.type == KeyMapper::PEV_UP)
[5978]437    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
[6506]438  else if( event.type == KeyMapper::PEV_DOWN)
[5978]439    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
440  else if( event.type == EV_MOUSE_MOTION)
441  {
442    this->xMouse = event.xRel;
443    this->yMouse = event.yRel;
[6162]444    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
[6241]445   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
446     //this->setAbsDir(mouseDir);
[5978]447  }
[5907]448}
449
[6243]450/**
451 *
452 */
453bool SpaceShip::pickup(PowerUp* powerUp)
454{
455  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
456    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
457    WeaponManager* manager = this->getWeaponManager();
458    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
459    if(slot >= 0) {
460      manager->addWeapon(weapon, 0, slot);
461      return true;
462    }
463  }
464  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
465    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
466    switch(ppu->getType()) {
467      case PARAM_SHIELD:
468        break;
469    }
470  }
471  return false;
472}
473
[5907]474#include "weapons/aiming_turret.h"
475// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
476void SpaceShip::ADDWEAPON()
477{
478  Weapon* turret = NULL;
479
[6306]480  if ((float)rand()/RAND_MAX < .9)
[5907]481  {
482    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
483    {
484      turret = new Turret();
[6443]485      this->addWeapon(turret, 2);
[5907]486      this->getWeaponManager()->changeWeaponConfig(2);
487    }
488  }
489  else
490  {
491    //if (this->getWeaponManager()->hasFreeSlot(3))
492    {
[6426]493      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
494      if (turret != NULL)
[6443]495      this->addWeapon(turret, 3);
[5907]496
497      this->getWeaponManager()->changeWeaponConfig(3);
498    }
499  }
500
501  if(turret != NULL)
502  {
503    turret->setName("Turret");
504    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
505  }
506}
[6424]507
508
509int SpaceShip::writeBytes( const byte * data, int length, int sender )
510{
511  setRequestedSync( false );
512  setIsOutOfSync( false );
513
514  SYNCHELP_READ_BEGIN();
515
516  SYNCHELP_READ_FKT( WorldEntity::writeState );
517
518  return SYNCHELP_READ_N;
519}
520
521int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
522{
523  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
524  {
525    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
526    setRequestedSync( true );
527  }
528
529  int rec = this->getRequestSync();
530  if ( rec > 0 )
531  {
532    *reciever = rec;
533
534    SYNCHELP_WRITE_BEGIN();
535
536    SYNCHELP_WRITE_FKT( WorldEntity::readState );
537
538    return SYNCHELP_WRITE_N;
539  }
540
541  *reciever = 0;
542  return 0;
543}
Note: See TracBrowser for help on using the repository browser.