Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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