Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/world_entities/space_ships/helicopter.cc @ 9337

Last change on this file since 9337 was 8495, checked in by bensch, 19 years ago

merged the branche atmos back. no conflicts

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