Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Control: SpaceShip is not anymore as strong effected by gravity as before

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