Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/helicopter.cc @ 6232

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

Control: more realistic helicopter-control

File size: 12.6 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:
[6002]12   main-programmer: Benjamin Grauer
[5907]13   co-programmer: ...
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
[6002]19#include "helicopter.h"
[5907]20
21#include "weapons/weapon_manager.h"
22#include "weapons/test_gun.h"
23#include "weapons/turret.h"
24#include "weapons/cannon.h"
25
26#include "factory.h"
27#include "key_mapper.h"
[5978]28#include "event_handler.h"
[5907]29
[5978]30#include "graphics_engine.h"
31
[5907]32using namespace std;
33
[6002]34CREATE_FACTORY(Helicopter, CL_HELICOPTER);
[5907]35
36/**
37 *  creates the controlable Spaceship
38 */
[6002]39Helicopter::Helicopter()
[5907]40{
41  this->init();
42}
43
44/**
45 *  destructs the spaceship, deletes alocated memory
46 */
[6002]47Helicopter::~Helicopter ()
[5907]48{
49}
50
51/**
52 * loads a Spaceships information from a specified file.
53 * @param fileName the name of the File to load the spaceship from (absolute path)
54 */
[6002]55Helicopter::Helicopter(const char* fileName)
[5907]56{
57  this->init();
58  TiXmlDocument doc(fileName);
59
60  if(!doc.LoadFile())
61  {
[6002]62    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName);
[5907]63    return;
64  }
65
66  this->loadParams(doc.RootElement());
67}
68
69/**
70 *  creates a new Spaceship from Xml Data
71 * @param root the xml element containing spaceship data
72
73   @todo add more parameters to load
74*/
[6002]75Helicopter::Helicopter(const TiXmlElement* root)
[5907]76{
77  this->init();
78  if (root != NULL)
79    this->loadParams(root);
80
81  //weapons:
82  Weapon* wpRight = new TestGun(0);
83  wpRight->setName("testGun Right");
84  Weapon* wpLeft = new TestGun(1);
85  wpLeft->setName("testGun Left");
[5982]86  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
[5907]87
88  cannon->setName("BFG");
89
90  this->getWeaponManager()->addWeapon(wpLeft, 1, 0);
91  this->getWeaponManager()->addWeapon(wpRight,1 ,1);
92  this->getWeaponManager()->addWeapon(cannon, 0, 6);
93
94  //this->getWeaponManager()->addWeapon(turret, 3, 0);
95
96  this->getWeaponManager()->changeWeaponConfig(1);
[6222]97  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
[5907]98}
99
100
101/**
[6162]102 * initializes a Helicopter
[5907]103 */
[6002]104void Helicopter::init()
[5907]105{
106//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[6002]107  this->setClassID(CL_HELICOPTER, "Helicopter");
[5907]108
109  PRINTF(4)("SPACESHIP INIT\n");
110
[6005]111  this->loadModel("models/ships/helicopter_#.obj", 1.0);
[6002]112
[5978]113  EventHandler::getInstance()->grabEvents(true);
[5907]114
[5978]115  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
[5907]116  bFire = false;
[5978]117  xMouse = yMouse = 0;
118  mouseSensitivity = 0.001;
[5982]119
[5978]120  cycle = 0.0;
[5907]121
[5982]122
[5907]123  travelSpeed = 15.0;
124  this->velocity = Vector(0.0,0.0,0.0);
125  this->velocityDir = Vector(1.0,0.0,0.0);
126
127//   GLGuiButton* button = new GLGuiPushButton();
128//   button->show();
129//   button->setLabel("orxonox");
130//   button->setBindNode(this);
131
132  //add events to the eventlist
[5978]133  registerEvent(SDLK_w);
134  registerEvent(SDLK_s);
135  registerEvent(SDLK_a);
136  registerEvent(SDLK_d);
137  registerEvent(SDLK_q);
138  registerEvent(SDLK_e);
[6232]139  registerEvent(SDLK_c);
[5907]140  registerEvent(KeyMapper::PEV_FIRE1);
141  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
142  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
143  registerEvent(SDLK_PAGEUP);
144  registerEvent(SDLK_PAGEDOWN);
[5978]145  registerEvent(EV_MOUSE_MOTION);
[5907]146
147  this->getWeaponManager()->setSlotCount(7);
148
149  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
150  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
151
152  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
153  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154
155  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
156  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
157
158  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
159  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
160
161  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
162  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
163
164  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
165  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
166//
167   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
168   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169   //
170//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
171//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
172//
173//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
174//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
175
176}
177
178/**
[6002]179 * loads the Settings of a Helicopter from an XML-element.
[5907]180 * @param root the XML-element to load the Spaceship's properties from
181 */
[6002]182void Helicopter::loadParams(const TiXmlElement* root)
[5907]183{
184  static_cast<WorldEntity*>(this)->loadParams(root);
185}
186
187
[6222]188
189
190void Helicopter::enter()
191{
192  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
193
194
195}
196
197void Helicopter::leave()
198{
199  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
200
201
202}
203
204
[5907]205/**
206 * adds a weapon to the weapon list of the spaceship
207 * @param weapon to add
208*/
[6002]209void Helicopter::addWeapon(Weapon* weapon)
[5907]210{
211  this->getWeaponManager()->addWeapon(weapon);
212}
213
214
215/**
216 *  removes a weapon from the spaceship
217 * @param weapon to remove
218*/
[6002]219void Helicopter::removeWeapon(Weapon* weapon)
[5907]220{
221  this->getWeaponManager()->removeWeapon(weapon);
222}
223
224/**
[6002]225 *  effect that occurs after the Helicopter is spawned
[5907]226*/
[6002]227void Helicopter::postSpawn ()
[5907]228{
229  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
230}
231
232/**
233 *  the action occuring if the spaceship left the game
234*/
[6002]235void Helicopter::leftWorld ()
[5907]236{}
237
238/**
239 *  this function is called, when two entities collide
240 * @param entity: the world entity with whom it collides
241 *
242 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
243 */
[6002]244void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
[5907]245{
[6002]246  if (entity->isA(CL_TURRET_POWER_UP))
[5907]247  {
248    this->ADDWEAPON();
249    }
250//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
251}
252
253
254
255/**
256 *  the function called for each passing timeSnap
257 * @param time The timespan passed since last update
258*/
[6002]259void Helicopter::tick (float time)
[5907]260{
261  // spaceship controlled movement
262  this->calculateVelocity(time);
263
264  Vector move = (velocity)*time;
265
[5978]266  // this is the air friction (necessary for a smooth control)
[6162]267  if(velocity.len() != 0) velocity -= velocity*0.1;
[5982]268
[6232]269  //physics: Gravity
270  this->shiftCoor(Vector(0,-0.05,0));
[6162]271
272  //hoover effect
273  cycle += time;
[5978]274  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
[5982]275
[6162]276  //readjust
277 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
278  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
279
[5978]280  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
[5907]281
282  this->shiftCoor (move);
283
284  this->getWeaponManager()->tick(time);
285  // weapon system manipulation
286  this->weaponAction();
287}
288
289/**
290 *  calculate the velocity
291 * @param time the timeslice since the last frame
292*/
[6002]293void Helicopter::calculateVelocity (float time)
[5907]294{
295  Vector accel(0.0, 0.0, 0.0);
[6162]296  Vector rot(0.0, 0.0, 0.0);
297  float rotVal = 0.0;
[5912]298  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
[5907]299  /* calculate the direction in which the craft is heading  */
300
301  if( this->bUp )
302   {
[5978]303     //this->shiftCoor(this->getAbsDirX());
[6162]304
[6232]305     accel += this->getAbsDirX()*2;
306     rot += Vector (0,0,1);
307     rotVal -= time/8;
[5907]308   }
[6162]309   else
310   {
[6232]311       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/8, Vector(0,0,1)));
[6162]312   }
[5907]313
314  if( this->bDown )
[5908]315   {
[5978]316     //this->shiftCoor((this->getAbsDirX())*-1);
[6232]317     accel -= this->getAbsDirX()*2;
318     rot += Vector (0,0,1);
319     rotVal += time/8;
[5907]320   }
[6162]321   else
322   {
[6232]323         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time/8, Vector(0,0,1)));
[6162]324   }
[5907]325
326  if( this->bLeft /* > -this->getRelCoor().z*2*/)
327  {
[6162]328    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
[6232]329    accel -= this->getAbsDirZ()*2;
[5908]330    //velocityDir.normalize();
[6232]331    rot += Vector(1,0,0);
332    rotVal -= time/8;
[5907]333  }
[6162]334  else
335   {
336         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0)));
337   }
[6222]338
[5907]339  if( this->bRight /* > this->getRelCoor().z*2*/)
340  {
[6162]341    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
[6232]342    accel += this->getAbsDirZ()*2;
[5908]343    //velocityDir.normalize();
[6232]344    rot += Vector(1,0,0);
345    rotVal += time/5;
[5907]346  }
[6162]347  else
348   {
349         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0)));
350   }
[5982]351
[5978]352  if( this->bRollL /* > -this->getRelCoor().z*2*/)
353  {
354    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
[6162]355    //accel -= rightDirection;
[5978]356    //velocityDir.normalize();
357    //rot +=Vector(1,0,0);
358    //rotVal -= .4;
359  }
360  if( this->bRollR /* > this->getRelCoor().z*2*/)
361  {
362    this->shiftDir(Quaternion(time, Vector(1,0,0)));
363
[6162]364    //accel += rightDirection;
[5978]365    //velocityDir.normalize();
366    //rot += Vector(1,0,0);
367    //rotVal += .4;
368  }
[5907]369  if (this->bAscend )
370  {
[6162]371    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
[5912]372
[6232]373    accel += (this->getAbsDirY())*3;
[5908]374    //velocityDir.normalize();
[5907]375    //rot += Vector(0,0,1);
376    //rotVal += .4;
377  }
378  if (this->bDescend )
379  {
[6162]380    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
[5912]381
[6232]382    accel -= (this->getAbsDirY())*3;
[5908]383    //velocityDir.normalize();
[5907]384    //rot += Vector(0,0,1);
385    //rotVal -= .4;
386  }
387
[5978]388  velocity += accel;
[6162]389  rot.normalize();
390  this->shiftDir(Quaternion(rotVal, rot));
[5907]391}
392
[6005]393
394void Helicopter::draw() const
395{
396  this->drawLODsafe();
397
398  this->getWeaponManager()->draw();
399}
400
401
[5907]402/**
403 * weapon manipulation by the player
404*/
[6002]405void Helicopter::weaponAction()
[5907]406{
407  if( this->bFire)
408    {
409      this->getWeaponManager()->fire();
410    }
411}
412
413/**
414 * @todo switch statement ??
415 */
[6002]416void Helicopter::process(const Event &event)
[5907]417{
[5982]418
419
[5978]420  if( event.type == SDLK_a)
[6162]421      this->bLeft = event.bPressed;
[5978]422  else if( event.type == SDLK_d)
[6162]423      this->bRight = event.bPressed;
[5907]424  else if( event.type == KeyMapper::PEV_FIRE1)
425      this->bFire = event.bPressed;
426  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
427    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
428  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
429    this->getWeaponManager()->previousWeaponConfig();
[6162]430  else if( event.type == SDLK_e)
431    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
432  else if( event.type == SDLK_c)
433    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
[5978]434  else if( event.type == SDLK_w)
435    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
436  else if( event.type == SDLK_s)
437    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
438  else if( event.type == EV_MOUSE_MOTION)
439  {
440    this->xMouse = event.xRel;
441    this->yMouse = event.yRel;
[6162]442    if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0;
[5978]443    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
444  }
[5907]445}
446
447#include "weapons/aiming_turret.h"
448// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
[6002]449void Helicopter::ADDWEAPON()
[5907]450{
451  Weapon* turret = NULL;
452
453  if ((float)rand()/RAND_MAX < .1)
454  {
455    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
456    {
457      turret = new Turret();
458      this->getWeaponManager()->addWeapon(turret, 2);
459      this->getWeaponManager()->changeWeaponConfig(2);
460    }
461  }
462  else
463  {
464    //if (this->getWeaponManager()->hasFreeSlot(3))
465    {
466      turret = new AimingTurret();
467      this->getWeaponManager()->addWeapon(turret, 3);
468
469      this->getWeaponManager()->changeWeaponConfig(3);
470    }
471  }
472
473  if(turret != NULL)
474  {
475    turret->setName("Turret");
476    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
477  }
478}
Note: See TracBrowser for help on using the repository browser.