Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Control: Helicopterrotors set

File size: 14.6 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 
128  // rotors
129  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
130  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
131 
132  this->topRotor.setParent(this);
133  this->tailRotor.setParent(this);
134 
135  this->topRotor.setRelCoor(Vector(-0.877,0.627,0));
136  this->tailRotor.setRelCoor(Vector(-4.43,0.297,0.068));
137  this->tailRotor.setAbsDir(Quaternion(M_PI_2,Vector(1,0,0)));
138 
139  this->loadModel("models/ships/rotor.obj",1.0,3);
140  this->loadModel("models/ships/rotor.obj",0.15,4);
141
142
143  //travelSpeed = 15.0;
144  this->velocity = Vector(0.0,0.0,0.0);
145  this->velocityDir = Vector(1.0,0.0,0.0);
146//   GLGuiButton* button = new GLGuiPushButton();
147//   button->show();
148//   button->setLabel("orxonox");
149//   button->setBindNode(this);
150
151  //add events to the eventlist
152  registerEvent(KeyMapper::PEV_UP);
153  registerEvent(KeyMapper::PEV_DOWN);
154  registerEvent(KeyMapper::PEV_LEFT);
155  registerEvent(KeyMapper::PEV_RIGHT);
156  //registerEvent(SDLK_q);
157  registerEvent(SDLK_e);
158  registerEvent(SDLK_c);
159  registerEvent(KeyMapper::PEV_FIRE1);
160  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
161  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
162  //registerEvent(SDLK_PAGEUP);
163  //registerEvent(SDLK_PAGEDOWN);
164  registerEvent(EV_MOUSE_MOTION);
165
166  this->getWeaponManager()->setSlotCount(7);
167
168  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
169  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
170
171  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
172  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
173
174  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
175  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
176
177  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
178  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
179
180  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
181  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
182
183  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
184  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
185//
186   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
187   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
188   //
189//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
190//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
191//
192//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
193//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
194
195  this->getWeaponManager()->getFixedTarget()->setParent(this);
196  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
197
198  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
199
200}
201
202/**
203 * loads the Settings of a Helicopter from an XML-element.
204 * @param root the XML-element to load the Spaceship's properties from
205 */
206void Helicopter::loadParams(const TiXmlElement* root)
207{
208  WorldEntity::loadParams(root);
209}
210
211
212
213
214void Helicopter::enter()
215{
216  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
217  this->attachCamera();
218
219
220}
221
222void Helicopter::leave()
223{
224  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
225  this->detachCamera();
226
227}
228
229
230/**
231 *  effect that occurs after the Helicopter is spawned
232*/
233void Helicopter::postSpawn ()
234{
235  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
236}
237
238/**
239 *  the action occuring if the helicopter left the game
240*/
241void Helicopter::leftWorld ()
242{}
243
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 Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
251{
252  if (entity->isA(CL_TURRET_POWER_UP))
253  {
254    this->ADDWEAPON();
255    }
256//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
257}
258
259
260
261/**
262 *  the function called for each passing timeSnap
263 * @param time The timespan passed since last update
264*/
265void Helicopter::tick (float time)
266{
267  if( xMouse != 0 || yMouse != 0)
268   {
269    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
270    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
271    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
272    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
273  }
274  // rotorrotation
275  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
276  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
277 
278  // spaceship controlled movement
279  this->calculateVelocity(time);
280
281  Vector move = (velocity)*time;
282
283  // this is the air friction (necessary for a smooth control)
284  if(velocity.len() != 0) velocity -= velocity*0.1;
285 
286  //travelSpeed = velocity.len();
287
288  //physics: Gravity
289  /*this->shiftCoor(Vector(0,-1,0));
290
291  this->shiftCoor(getAbsDirY()*rotorspeed);
292  */
293 
294  /*
295  //hoover effect
296  cycle += time;
297  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
298  */
299
300  //readjust
301 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
302  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
303
304  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
305
306  this->shiftCoor (move);
307  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
308
309  this->getWeaponManager()->tick(time);
310  // weapon system manipulation
311  this->weaponAction();
312}
313
314/**
315 *  calculate the velocity
316 * @param time the timeslice since the last frame
317*/
318void Helicopter::calculateVelocity (float time)
319{
320  Vector accel(0.0, 0.0, 0.0);
321  float rotValX = 0.0;
322  float rotValZ = 0.0;
323  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
324  /* calculate the direction in which the craft is heading  */
325
326  if( this->bUp )
327   {
328     //this->shiftCoor(this->getAbsDirX());
329     //accel -= this->getAbsDirY();
330     
331     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
332     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
333   }
334   else
335   {
336       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
337   }
338
339  if( this->bDown )
340   {
341     //this->shiftCoor((this->getAbsDirX())*-1);
342     //accel -= this->getAbsDirY();
343     
344     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
345     rotValZ += time;
346   }
347   else
348   {
349         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
350   }
351
352  if( this->bLeft /* > -this->getRelCoor().z*2*/)
353  {
354    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
355    //accel -= this->getAbsDirY();
356    //velocityDir.normalize();
357   
358    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
359    rotValX -= time;
360  }
361  else
362   {
363         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
364   }
365
366  if( this->bRight /* > this->getRelCoor().z*2*/)
367  {
368    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
369    //accel += this->getAbsDirY();
370    //velocityDir.normalize();
371   
372    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
373    rotValX += time;
374  }
375  else
376   {
377         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
378   }
379
380  if( this->bRollL /* > -this->getRelCoor().z*2*/)
381  {
382    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
383    //accel -= rightDirection;
384    //velocityDir.normalize();
385    //rot +=Vector(1,0,0);
386    //rotVal -= .4;
387  }
388  if( this->bRollR /* > this->getRelCoor().z*2*/)
389  {
390    this->shiftDir(Quaternion(time, Vector(1,0,0)));
391
392    //accel += rightDirection;
393    //velocityDir.normalize();
394    //rot += Vector(1,0,0);
395    //rotVal += .4;
396  }
397  if (this->bAscend )
398  {
399    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
400
401    accel += this->getAbsDirY();
402    //rotorspeed += 0.05;
403    //if (rotorspeed >= 2) rotorspeed = 2;
404    //velocityDir.normalize();
405    //rot += Vector(0,0,1);
406    //rotVal += .4;
407  }
408  else
409  {
410    //if(rotorspeed >= 1.05) rotorspeed -= 0.05;
411  }
412
413  if (this->bDescend )
414  {
415    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
416
417    accel -= this->getAbsDirY();
418    //rotorspeed -= 0.05;
419    //if (rotorspeed <= 0) rotorspeed = 0;
420    //velocityDir.normalize();
421    //rot += Vector(0,0,1);
422    //rotVal -= .4;
423  }
424  else
425  {
426    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
427  }
428
429  velocity += accel*3;
430  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
431  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
432}
433
434
435void Helicopter::draw() const
436{
437  WorldEntity::draw();
438 
439  glMatrixMode(GL_MODELVIEW);
440    glPushMatrix();
441
442    /* translate */
443    glTranslatef (this->topRotor.getAbsCoor ().x,
444                  this->topRotor.getAbsCoor ().y,
445                  this->topRotor.getAbsCoor ().z);
446    Vector tmpRot = this->topRotor.getAbsDir().getSpacialAxis();
447    glRotatef (this->topRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
448    this->getModel(3)->draw();
449
450    glPopMatrix ();
451    glPushMatrix();
452
453    /* translate */
454    glTranslatef (this->tailRotor.getAbsCoor ().x,
455                  this->tailRotor.getAbsCoor ().y,
456                  this->tailRotor.getAbsCoor ().z);
457    tmpRot = this->tailRotor.getAbsDir().getSpacialAxis();
458    glRotatef (this->tailRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
459    this->getModel(4)->draw();
460
461    glPopMatrix ();
462
463  this->getWeaponManager()->draw();
464}
465
466
467/**
468 * weapon manipulation by the player
469*/
470void Helicopter::weaponAction()
471{
472  if( this->bFire)
473    {
474      this->getWeaponManager()->fire();
475    }
476}
477
478/**
479 * @todo switch statement ??
480 */
481void Helicopter::process(const Event &event)
482{
483
484
485  if( event.type == KeyMapper::PEV_LEFT)
486      this->bLeft = event.bPressed;
487  else if( event.type == KeyMapper::PEV_RIGHT)
488      this->bRight = event.bPressed;
489  else if( event.type == KeyMapper::PEV_FIRE1)
490      this->bFire = event.bPressed;
491  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
492    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
493  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
494    this->getWeaponManager()->previousWeaponConfig();
495  else if( event.type == SDLK_e)
496    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
497  else if( event.type == SDLK_c)
498    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
499  else if( event.type == KeyMapper::PEV_UP)
500    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
501  else if( event.type == KeyMapper::PEV_DOWN)
502    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
503  else if( event.type == EV_MOUSE_MOTION)
504  {
505    this->xMouse = event.xRel*mouseSensitivity;
506    this->yMouse = event.yRel*mouseSensitivity;
507
508    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))/*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1))*/);
509  }
510}
511
512#include "weapons/aiming_turret.h"
513// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
514void Helicopter::ADDWEAPON()
515{
516  Weapon* turret = NULL;
517
518  if ((float)rand()/RAND_MAX < .1)
519  {
520    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
521    {
522      turret = new Turret();
523      this->addWeapon(turret, 2);
524      this->getWeaponManager()->changeWeaponConfig(2);
525    }
526  }
527  else
528  {
529    //if (this->getWeaponManager()->hasFreeSlot(3))
530    {
531      turret = new AimingTurret();
532      this->addWeapon(turret, 3);
533
534      this->getWeaponManager()->changeWeaponConfig(3);
535    }
536  }
537
538  if(turret != NULL)
539  {
540    turret->setName("Turret");
541    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
542  }
543}
Note: See TracBrowser for help on using the repository browser.