Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7810 was 7810, checked in by bensch, 18 years ago

orxonox/trunk: merged the Weather effects back here

File size: 12.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 Knecht
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 "util/loading/factory.h"
27#include "util/loading/resource_manager.h"
28
29#include "key_mapper.h"
30#include "event_handler.h"
31#include "state.h"
32
33#include "graphics_engine.h"
34
35using namespace std;
36
37CREATE_FACTORY(Helicopter, CL_HELICOPTER);
38
39/**
40 *  creates the controlable Helicopter
41 */
42Helicopter::Helicopter()
43{
44  this->init();
45}
46
47/**
48 *  destructs the helicopter, deletes alocated memory
49 */
50Helicopter::~Helicopter ()
51{
52  this->setPlayer(NULL);
53
54  if (this->chopperBuffer != NULL)
55    ResourceManager::getInstance()->unload(this->chopperBuffer);
56}
57
58/**
59 * loads a Helicopter information from a specified file.
60 * @param fileName the name of the File to load the helicopter from (absolute path)
61 */
62Helicopter::Helicopter(const std::string& fileName)
63{
64  this->init();
65  TiXmlDocument doc(fileName);
66
67  if(!doc.LoadFile())
68  {
69    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName.c_str());
70    return;
71  }
72
73  this->loadParams(doc.RootElement());
74}
75
76/**
77 *  creates a new Spaceship from Xml Data
78 * @param root the xml element containing spaceship data
79
80   @todo add more parameters to load
81*/
82Helicopter::Helicopter(const TiXmlElement* root)
83{
84  this->init();
85  if (root != NULL)
86    this->loadParams(root);
87
88  //weapons:
89  Weapon* wpRight = new TestGun(0);
90  wpRight->setName("testGun Right");
91  Weapon* wpLeft = new TestGun(1);
92  wpLeft->setName("testGun Left");
93  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
94
95  cannon->setName("BFG");
96
97  this->addWeapon(wpLeft, 1, 0);
98  this->addWeapon(wpRight,1 ,1);
99  this->addWeapon(cannon, 0, 6);
100
101  this->getWeaponManager().changeWeaponConfig(1);
102  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
103
104  //load sound
105  if (this->chopperBuffer != NULL)
106    ResourceManager::getInstance()->unload(this->chopperBuffer);
107  this->chopperBuffer = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load("sound/chopper.wav", WAV);
108
109}
110
111
112/**
113 * initializes a Helicopter
114 */
115void Helicopter::init()
116{
117  this->setClassID(CL_HELICOPTER, "Helicopter");
118
119  PRINTF(4)("HELICOPTER INIT\n");
120
121  this->loadModel("models/ships/helicopter_#.obj", 1.0);
122
123  //EventHandler::getInstance()->grabEvents(true);
124
125  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
126  bFire = false;
127  xMouse = yMouse = 0;
128  mouseSensitivity = 0.05;
129  controlVelocityX = 100;
130  controlVelocityY = 100;
131
132
133  // initialization of cameraNode
134  this->cameraNode.setParent(this);
135  this->cameraNode.setParentMode(PNODE_ALL);
136  this->cameraNode.setRelCoor(Vector(0,1,0));
137
138  // rotors
139  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
140  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
141
142  this->topRotor.setParent(this);
143  this->tailRotor.setParent(this);
144
145  this->topRotor.setRelCoor(Vector(-0.877,0.627,0));
146  this->tailRotor.setRelCoor(Vector(-4.43,0.297,0.068));
147  this->tailRotor.setAbsDir(Quaternion(M_PI_2,Vector(1,0,0)));
148
149  this->loadModel("models/ships/rotor.obj",1.0,3);
150  this->loadModel("models/ships/rotor.obj",0.2,4);
151
152
153  this->velocity = Vector(0.0,0.0,0.0);
154  this->velocityDir = Vector(1.0,0.0,0.0);
155
156  // very, very old stuff
157  //  GLGuiButton* button = new GLGuiPushButton();
158  //  button->show();
159  //  button->setLabel("orxonox");
160  //  button->setBindNode(this);
161
162  //add events to the eventlist
163  registerEvent(KeyMapper::PEV_FORWARD);
164  registerEvent(KeyMapper::PEV_BACKWARD);
165  registerEvent(KeyMapper::PEV_LEFT);
166  registerEvent(KeyMapper::PEV_RIGHT);
167  registerEvent(SDLK_e);
168  registerEvent(SDLK_c);
169  registerEvent(KeyMapper::PEV_FIRE1);
170  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
171  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
172  registerEvent(EV_MOUSE_MOTION);
173
174  this->getWeaponManager().setSlotCount(7);
175
176  this->getWeaponManager().setSlotPosition(0, Vector(0.0, .1, -1.0));
177  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
178
179  this->getWeaponManager().setSlotPosition(1, Vector(0.0, .1, 1.0));
180  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
181
182  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
183  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
184
185  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
186  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
187
188  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
189  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
190
191  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
192  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
193
194  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
195  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
196
197  this->getWeaponManager().getFixedTarget()->setParent(&(this->cameraNode));
198  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
199
200  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
201
202}
203
204/**
205 * loads the Settings of a Helicopter from an XML-element.
206 * @param root the XML-element to load the Spaceship's properties from
207 */
208void Helicopter::loadParams(const TiXmlElement* root)
209{
210  WorldEntity::loadParams(root);
211}
212
213void Helicopter::enter()
214{
215  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
216  State::getCameraNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
217  State::getCameraTargetNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
218
219  this->soundSource.loop(this->chopperBuffer);
220  // PRINTF(0)( "Playing ChopperSound\n" );
221}
222
223void Helicopter::leave()
224{
225  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
226  this->detachCamera();
227  this->soundSource.stop();
228}
229
230
231/**
232 *  effect that occurs after the Helicopter is spawned
233*/
234void Helicopter::postSpawn ()
235{
236  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
237}
238
239/**
240 *  the action occuring if the helicopter left the game
241*/
242void Helicopter::leftWorld ()
243{}
244
245/**
246 *  this function is called, when two entities collide
247 * @param entity: the world entity with whom it collides
248 *
249 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
250 */
251void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
252{
253}
254
255
256
257/**
258 *  the function called for each passing timeSnap
259 * @param time The timespan passed since last update
260*/
261void Helicopter::tick (float time)
262{
263  Playable::tick(time);
264
265  if( xMouse != 0 || yMouse != 0)
266   {
267    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
268    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
269    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
270    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
271  }
272
273  // rotorrotation
274  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
275  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
276
277  // spaceship controlled movement
278  this->calculateVelocity(time);
279
280  Vector move = (velocity)*time;
281
282  // this is the air friction (necessary for a smooth control)
283  if(velocity.len() != 0) velocity -= velocity*0.1;
284
285
286  //readjust
287  // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
288  // else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
289
290
291  this->shiftCoor (move);
292}
293
294/**
295 *  calculate the velocity
296 * @param time the timeslice since the last frame
297*/
298void Helicopter::calculateVelocity (float time)
299{
300  Vector accel(0.0, 0.0, 0.0);
301  float rotValX = 0.0;
302  float rotValZ = 0.0;
303  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
304  /* calculate the direction in which the craft is heading  */
305
306  if( this->bUp )
307   {
308     //this->shiftCoor(this->getAbsDirX());
309     //accel -= this->getAbsDirY();
310
311     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z) * 3;
312     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
313   }
314   else
315   {
316       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1))) ;
317   }
318
319  if( this->bDown )
320   {
321     //this->shiftCoor((this->getAbsDirX())*-1);
322     //accel -= this->getAbsDirY();
323
324     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z)*3;
325     rotValZ += time;
326   }
327   else
328   {
329         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
330   }
331
332  if( this->bLeft )
333  {
334    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
335    //accel -= this->getAbsDirY();
336    //velocityDir.normalize();
337
338    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
339    rotValX -= time;
340  }
341  else
342   {
343         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
344   }
345
346  if( this->bRight )
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->bRollL )
361  {
362    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
363  }
364  if( this->bRollR )
365  {
366    this->shiftDir(Quaternion(time, Vector(1,0,0)));
367  }
368  if (this->bAscend )
369  {
370    accel += this->getAbsDirY();
371  }
372
373  if (this->bDescend )
374  {
375    accel -= this->getAbsDirY();
376  }
377
378  velocity += accel*3;
379  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
380  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
381}
382
383
384void Helicopter::draw() const
385{
386  WorldEntity::draw();
387
388  glMatrixMode(GL_MODELVIEW);
389    glPushMatrix();
390
391    /* translate */
392    glTranslatef (this->topRotor.getAbsCoor ().x,
393                  this->topRotor.getAbsCoor ().y,
394                  this->topRotor.getAbsCoor ().z);
395    Vector tmpRot = this->topRotor.getAbsDir().getSpacialAxis();
396    glRotatef (this->topRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
397    this->getModel(3)->draw();
398
399    glPopMatrix ();
400    glPushMatrix();
401
402    /* translate */
403    glTranslatef (this->tailRotor.getAbsCoor ().x,
404                  this->tailRotor.getAbsCoor ().y,
405                  this->tailRotor.getAbsCoor ().z);
406    tmpRot = this->tailRotor.getAbsDir().getSpacialAxis();
407    glRotatef (this->tailRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
408    this->getModel(4)->draw();
409
410    glPopMatrix ();
411}
412
413/**
414 * @todo switch statement ??
415 */
416void Helicopter::process(const Event &event)
417{
418  Playable::process(event);
419
420  if( event.type == KeyMapper::PEV_LEFT)
421      this->bLeft = event.bPressed;
422  else if( event.type == KeyMapper::PEV_RIGHT)
423      this->bRight = event.bPressed;
424  else if( event.type == SDLK_e)
425    this->bAscend = event.bPressed;
426  else if( event.type == SDLK_c)
427    this->bDescend = event.bPressed;
428  else if( event.type == KeyMapper::PEV_FORWARD)
429    this->bUp = event.bPressed;
430  else if( event.type == KeyMapper::PEV_BACKWARD)
431    this->bDown = event.bPressed;
432  else if( event.type == EV_MOUSE_MOTION)
433  {
434    this->xMouse = event.xRel*mouseSensitivity;
435    this->yMouse = event.yRel*mouseSensitivity;
436
437    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
438
439    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
440
441
442    if ((this->cameraNode.getAbsDirY()).y < 0.5)
443    {
444     if((this->cameraNode.getAbsDirX()).y > 0)
445     {
446        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
447     }
448     else
449     {
450         if(yMouse < 0) this->cameraNode.shiftDir(yDir);
451     }
452    }
453    else this->cameraNode.shiftDir(yDir);;
454    }
455}
Note: See TracBrowser for help on using the repository browser.