Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/powerups/src/world_entities/space_ships/helicopter.cc @ 6933

Last change on this file since 6933 was 6871, checked in by bensch, 19 years ago

orxonox/trunk: small fixes (cleanup)

File size: 13.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  // 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::enter()
216{
217  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
218  State::getCamera()->setParentSoft(this->getWeaponManager()->getFixedTarget());
219  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
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}
253
254
255
256/**
257 *  the function called for each passing timeSnap
258 * @param time The timespan passed since last update
259*/
260void Helicopter::tick (float time)
261{
262  Playable::tick(time);
263
264  if( xMouse != 0 || yMouse != 0)
265   {
266    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
267    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
268    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
269    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
270  }
271
272  // rotorrotation
273  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
274  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
275
276  // spaceship controlled movement
277  this->calculateVelocity(time);
278
279  Vector move = (velocity)*time;
280
281  // this is the air friction (necessary for a smooth control)
282  if(velocity.len() != 0) velocity -= velocity*0.1;
283
284  //travelSpeed = velocity.len();
285
286  //physics: Gravity
287  /*this->shiftCoor(Vector(0,-1,0));
288
289  this->shiftCoor(getAbsDirY()*rotorspeed);
290  */
291
292  /*
293  //hoover effect
294  cycle += time;
295  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
296  */
297
298  //readjust
299 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
300  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
301
302  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
303
304  this->shiftCoor (move);
305  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
306}
307
308/**
309 *  calculate the velocity
310 * @param time the timeslice since the last frame
311*/
312void Helicopter::calculateVelocity (float time)
313{
314  Vector accel(0.0, 0.0, 0.0);
315  float rotValX = 0.0;
316  float rotValZ = 0.0;
317  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
318  /* calculate the direction in which the craft is heading  */
319
320  if( this->bUp )
321   {
322     //this->shiftCoor(this->getAbsDirX());
323     //accel -= this->getAbsDirY();
324
325     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
326     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
327   }
328   else
329   {
330       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
331   }
332
333  if( this->bDown )
334   {
335     //this->shiftCoor((this->getAbsDirX())*-1);
336     //accel -= this->getAbsDirY();
337
338     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
339     rotValZ += time;
340   }
341   else
342   {
343         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
344   }
345
346  if( this->bLeft /* > -this->getRelCoor().z*2*/)
347  {
348    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
349    //accel -= this->getAbsDirY();
350    //velocityDir.normalize();
351
352    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
353    rotValX -= time;
354  }
355  else
356   {
357         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
358   }
359
360  if( this->bRight /* > this->getRelCoor().z*2*/)
361  {
362    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
363    //accel += this->getAbsDirY();
364    //velocityDir.normalize();
365
366    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
367    rotValX += time;
368  }
369  else
370   {
371         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
372   }
373
374  if( this->bRollL /* > -this->getRelCoor().z*2*/)
375  {
376    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
377    //accel -= rightDirection;
378    //velocityDir.normalize();
379    //rot +=Vector(1,0,0);
380    //rotVal -= .4;
381  }
382  if( this->bRollR /* > this->getRelCoor().z*2*/)
383  {
384    this->shiftDir(Quaternion(time, Vector(1,0,0)));
385
386    //accel += rightDirection;
387    //velocityDir.normalize();
388    //rot += Vector(1,0,0);
389    //rotVal += .4;
390  }
391  if (this->bAscend )
392  {
393    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
394
395    accel += this->getAbsDirY();
396    //rotorspeed += 0.05;
397    //if (rotorspeed >= 2) rotorspeed = 2;
398    //velocityDir.normalize();
399    //rot += Vector(0,0,1);
400    //rotVal += .4;
401  }
402  else
403  {
404    //if(rotorspeed >= 1.05) rotorspeed -= 0.05;
405  }
406
407  if (this->bDescend )
408  {
409    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
410
411    accel -= this->getAbsDirY();
412    //rotorspeed -= 0.05;
413    //if (rotorspeed <= 0) rotorspeed = 0;
414    //velocityDir.normalize();
415    //rot += Vector(0,0,1);
416    //rotVal -= .4;
417  }
418  else
419  {
420    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
421  }
422
423  velocity += accel*3;
424  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
425  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
426}
427
428
429void Helicopter::draw() const
430{
431  WorldEntity::draw();
432
433  glMatrixMode(GL_MODELVIEW);
434    glPushMatrix();
435
436    /* translate */
437    glTranslatef (this->topRotor.getAbsCoor ().x,
438                  this->topRotor.getAbsCoor ().y,
439                  this->topRotor.getAbsCoor ().z);
440    Vector tmpRot = this->topRotor.getAbsDir().getSpacialAxis();
441    glRotatef (this->topRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
442    this->getModel(3)->draw();
443
444    glPopMatrix ();
445    glPushMatrix();
446
447    /* translate */
448    glTranslatef (this->tailRotor.getAbsCoor ().x,
449                  this->tailRotor.getAbsCoor ().y,
450                  this->tailRotor.getAbsCoor ().z);
451    tmpRot = this->tailRotor.getAbsDir().getSpacialAxis();
452    glRotatef (this->tailRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
453    this->getModel(4)->draw();
454
455    glPopMatrix ();
456}
457
458/**
459 * @todo switch statement ??
460 */
461void Helicopter::process(const Event &event)
462{
463  Playable::process(event);
464
465  if( event.type == KeyMapper::PEV_LEFT)
466      this->bLeft = event.bPressed;
467  else if( event.type == KeyMapper::PEV_RIGHT)
468      this->bRight = event.bPressed;
469  else if( event.type == SDLK_e)
470    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
471  else if( event.type == SDLK_c)
472    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
473  else if( event.type == KeyMapper::PEV_UP)
474    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
475  else if( event.type == KeyMapper::PEV_DOWN)
476    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
477  else if( event.type == EV_MOUSE_MOTION)
478  {
479    this->xMouse = event.xRel*mouseSensitivity;
480    this->yMouse = event.yRel*mouseSensitivity;
481
482    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
483
484    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
485
486
487    if ((this->cameraNode.getAbsDirY()).y < 0.5)
488    {
489     if((this->cameraNode.getAbsDirX()).y > 0)
490     {
491        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
492     }
493     else
494     {
495         if(yMouse < 0) this->cameraNode.shiftDir(yDir);
496     }
497    }
498    else this->cameraNode.shiftDir(yDir);;
499    }
500}
Note: See TracBrowser for help on using the repository browser.