Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Control: Minimumspeed for SpaceShip

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