Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the presentation back

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->chopperBuffer = NULL;
127
128  this->setClassID(CL_HELICOPTER, "Helicopter");
129  PRINTF(4)("HELICOPTER INIT\n");
130
131  this->loadModel("models/ships/helicopter_#.obj", 1.0);
132
133  //EventHandler::getInstance()->grabEvents(true);
134
135  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
136  bFire = false;
137  xMouse = yMouse = 0;
138  mouseSensitivity = 0.05;
139  controlVelocityX = 100;
140  controlVelocityY = 100;
141
142
143  // initialization of cameraNode
144  this->cameraNode.setParent(this);
145  this->cameraNode.setParentMode(PNODE_ALL);
146  this->cameraNode.setRelCoor(Vector(0,1,0));
147
148  // rotors
149  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
150  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
151
152  this->topRotor.setParent(this);
153  this->tailRotor.setParent(this);
154
155  this->topRotor.setRelCoor(Vector(-0.877,0.627,0));
156  this->tailRotor.setRelCoor(Vector(-4.43,0.297,0.068));
157  this->tailRotor.setAbsDir(Quaternion(M_PI_2,Vector(1,0,0)));
158
159  this->loadModel("models/ships/rotor.obj",1.0,3);
160  this->loadModel("models/ships/rotor.obj",0.2,4);
161
162
163  this->velocity = Vector(0.0,0.0,0.0);
164  this->velocityDir = Vector(1.0,0.0,0.0);
165
166  // very, very old stuff
167  //  GLGuiButton* button = new GLGuiPushButton();
168  //  button->show();
169  //  button->setLabel("orxonox");
170  //  button->setBindNode(this);
171
172  //add events to the eventlist
173  registerEvent(KeyMapper::PEV_FORWARD);
174  registerEvent(KeyMapper::PEV_BACKWARD);
175  registerEvent(KeyMapper::PEV_LEFT);
176  registerEvent(KeyMapper::PEV_RIGHT);
177  registerEvent(SDLK_e);
178  registerEvent(SDLK_c);
179  registerEvent(KeyMapper::PEV_FIRE1);
180  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
181  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
182  registerEvent(EV_MOUSE_MOTION);
183
184  this->getWeaponManager().setSlotCount(7);
185
186  this->getWeaponManager().setSlotPosition(0, Vector(0.0, .1, -1.0));
187  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
188
189  this->getWeaponManager().setSlotPosition(1, Vector(0.0, .1, 1.0));
190  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
191
192  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
193  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
194
195  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
196  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
197
198  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
199  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
200
201  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
202  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
203
204  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
205  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
206
207  this->getWeaponManager().getFixedTarget()->setParent(&(this->cameraNode));
208  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
209
210  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
211
212}
213
214/**
215 * loads the Settings of a Helicopter from an XML-element.
216 * @param root the XML-element to load the Spaceship's properties from
217 */
218void Helicopter::loadParams(const TiXmlElement* root)
219{
220  WorldEntity::loadParams(root);
221}
222
223void Helicopter::enter()
224{
225  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
226  State::getCameraNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
227  State::getCameraTargetNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
228
229  this->soundSource.play(this->chopperBuffer, 0.7f, true);
230}
231
232void Helicopter::leave()
233{
234  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
235  this->detachCamera();
236  this->soundSource.stop();
237}
238
239
240/**
241 *  effect that occurs after the Helicopter is spawned
242*/
243void Helicopter::postSpawn ()
244{
245  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
246}
247
248/**
249 *  the action occuring if the helicopter left the game
250*/
251void Helicopter::leftWorld ()
252{}
253
254/**
255 *  this function is called, when two entities collide
256 * @param entity: the world entity with whom it collides
257 *
258 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
259 */
260void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
261{
262}
263
264
265
266/**
267 *  the function called for each passing timeSnap
268 * @param time The timespan passed since last update
269*/
270void Helicopter::tick (float time)
271{
272  Playable::tick(time);
273
274  if( xMouse != 0 || yMouse != 0)
275   {
276    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
277    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
278    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
279    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
280  }
281
282  // rotorrotation
283  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
284  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
285
286  // spaceship controlled movement
287  this->calculateVelocity(time);
288
289  Vector move = (velocity)*time;
290
291  // this is the air friction (necessary for a smooth control)
292  if(velocity.len() != 0) velocity -= velocity*0.1;
293
294
295  //readjust
296  // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
297  // else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
298
299
300  this->shiftCoor (move);
301}
302
303/**
304 *  calculate the velocity
305 * @param time the timeslice since the last frame
306*/
307void Helicopter::calculateVelocity (float time)
308{
309  Vector accel(0.0, 0.0, 0.0);
310  float rotValX = 0.0;
311  float rotValZ = 0.0;
312  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
313  /* calculate the direction in which the craft is heading  */
314
315  if( this->bUp )
316   {
317     //this->shiftCoor(this->getAbsDirX());
318     //accel -= this->getAbsDirY();
319
320     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z) * 3;
321     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
322   }
323   else
324   {
325       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1))) ;
326   }
327
328  if( this->bDown )
329   {
330     //this->shiftCoor((this->getAbsDirX())*-1);
331     //accel -= this->getAbsDirY();
332
333     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z)*3;
334     rotValZ += time;
335   }
336   else
337   {
338         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
339   }
340
341  if( this->bLeft )
342  {
343    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
344    //accel -= this->getAbsDirY();
345    //velocityDir.normalize();
346
347    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
348    rotValX -= time;
349  }
350  else
351   {
352         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
353   }
354
355  if( this->bRight )
356  {
357    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
358    //accel += this->getAbsDirY();
359    //velocityDir.normalize();
360
361    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
362    rotValX += time;
363  }
364  else
365   {
366         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
367   }
368
369  if( this->bRollL )
370  {
371    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
372  }
373  if( this->bRollR )
374  {
375    this->shiftDir(Quaternion(time, Vector(1,0,0)));
376  }
377  if (this->bAscend )
378  {
379    accel += this->getAbsDirY();
380  }
381
382  if (this->bDescend )
383  {
384    accel -= this->getAbsDirY();
385  }
386
387  velocity += accel*3;
388  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
389  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
390}
391
392
393void Helicopter::draw() const
394{
395  WorldEntity::draw();
396
397  glMatrixMode(GL_MODELVIEW);
398    glPushMatrix();
399
400    /* translate */
401    glTranslatef (this->topRotor.getAbsCoor ().x,
402                  this->topRotor.getAbsCoor ().y,
403                  this->topRotor.getAbsCoor ().z);
404    Vector tmpRot = this->topRotor.getAbsDir().getSpacialAxis();
405    glRotatef (this->topRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
406    this->getModel(3)->draw();
407
408    glPopMatrix ();
409    glPushMatrix();
410
411    /* translate */
412    glTranslatef (this->tailRotor.getAbsCoor ().x,
413                  this->tailRotor.getAbsCoor ().y,
414                  this->tailRotor.getAbsCoor ().z);
415    tmpRot = this->tailRotor.getAbsDir().getSpacialAxis();
416    glRotatef (this->tailRotor.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
417    this->getModel(4)->draw();
418
419    glPopMatrix ();
420}
421
422/**
423 * @todo switch statement ??
424 */
425void Helicopter::process(const Event &event)
426{
427  Playable::process(event);
428
429  if( event.type == KeyMapper::PEV_LEFT)
430      this->bLeft = event.bPressed;
431  else if( event.type == KeyMapper::PEV_RIGHT)
432      this->bRight = event.bPressed;
433  else if( event.type == SDLK_e)
434    this->bAscend = event.bPressed;
435  else if( event.type == SDLK_c)
436    this->bDescend = event.bPressed;
437  else if( event.type == KeyMapper::PEV_FORWARD)
438    this->bUp = event.bPressed;
439  else if( event.type == KeyMapper::PEV_BACKWARD)
440    this->bDown = event.bPressed;
441  else if( event.type == EV_MOUSE_MOTION)
442  {
443    this->xMouse = event.xRel*mouseSensitivity;
444    this->yMouse = event.yRel*mouseSensitivity;
445
446    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
447
448    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
449
450
451    if ((this->cameraNode.getAbsDirY()).y < 0.5)
452    {
453     if((this->cameraNode.getAbsDirX()).y > 0)
454     {
455        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
456     }
457     else
458     {
459         if(yMouse < 0) this->cameraNode.shiftDir(yDir);
460     }
461    }
462    else this->cameraNode.shiftDir(yDir);;
463    }
464}
Note: See TracBrowser for help on using the repository browser.