Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/helicopter.cc @ 6781

Last change on this file since 6781 was 6724, checked in by bensch, 19 years ago

merged the Spaceshipcontrol back to the trunk

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