Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.older/src/world_entities/space_ships/helicopter.cc @ 10640

Last change on this file since 10640 was 8783, checked in by patrick, 19 years ago

merged the script engine branche back to trunk

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