Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6584 was 6584, checked in by snellen, 18 years ago

space_ship.cc updated

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