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
RevLine 
[6443]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:
[6947]12   main-programmer: Benjamin Knecht
[6443]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
[7193]26#include "util/loading/factory.h"
[9869]27#include "sound/resource_sound_buffer.h"
[7810]28
[6443]29#include "key_mapper.h"
30#include "state.h"
31
32#include "graphics_engine.h"
33
[8362]34#include "debug.h"
[6443]35
[9869]36#include "class_id_DEPRECATED.h"
37ObjectListDefinitionID(Helicopter, CL_HELICOPTER);
38CREATE_FACTORY(Helicopter);
[8408]39#include "script_class.h"
[9869]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))
[9235]47
[8783]48                       );
[6443]49
[9869]50
[6443]51/**
[6724]52 *  creates the controlable Helicopter
[6443]53 */
54Helicopter::Helicopter()
55{
56  this->init();
57}
58
59/**
[6724]60 *  destructs the helicopter, deletes alocated memory
[6443]61 */
62Helicopter::~Helicopter ()
63{
[6986]64  this->setPlayer(NULL);
[6443]65}
66
67/**
[6724]68 * loads a Helicopter information from a specified file.
69 * @param fileName the name of the File to load the helicopter from (absolute path)
[6443]70 */
[7221]71Helicopter::Helicopter(const std::string& fileName)
[6443]72{
73  this->init();
74  TiXmlDocument doc(fileName);
75
76  if(!doc.LoadFile())
77  {
[7221]78    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName.c_str());
[6443]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");
[9869]102  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate("Cannon"));
[6443]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
[7337]110  this->getWeaponManager().changeWeaponConfig(1);
111  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[7810]112
113  //load sound
[9869]114  this->chopperBuffer = OrxSound::ResourceSoundBuffer("sound/engine/chopper.wav");
[7810]115
[6443]116}
117
118
119/**
120 * initializes a Helicopter
121 */
122void Helicopter::init()
123{
[9869]124  this->registerObject(this, Helicopter::_objectList);
[6724]125  PRINTF(4)("HELICOPTER INIT\n");
[6443]126
127  this->loadModel("models/ships/helicopter_#.obj", 1.0);
128
[7221]129  //EventHandler::getInstance()->grabEvents(true);
[6443]130
131  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
132  bFire = false;
133  xMouse = yMouse = 0;
[6637]134  mouseSensitivity = 0.05;
[6724]135  controlVelocityX = 100;
136  controlVelocityY = 100;
[6443]137
[6804]138
[6947]139  // initialization of cameraNode
[6724]140  this->cameraNode.setParent(this);
141  this->cameraNode.setParentMode(PNODE_ALL);
[6947]142  this->cameraNode.setRelCoor(Vector(0,1,0));
[6986]143
[6724]144  // rotors
145  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
146  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6804]147
[6724]148  this->topRotor.setParent(this);
149  this->tailRotor.setParent(this);
[6804]150
[6724]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)));
[6804]154
[6724]155  this->loadModel("models/ships/rotor.obj",1.0,3);
156  this->loadModel("models/ships/rotor.obj",0.2,4);
[6443]157
158
159  this->velocity = Vector(0.0,0.0,0.0);
160  this->velocityDir = Vector(1.0,0.0,0.0);
[6986]161
[6947]162  // very, very old stuff
163  //  GLGuiButton* button = new GLGuiPushButton();
164  //  button->show();
165  //  button->setLabel("orxonox");
166  //  button->setBindNode(this);
[6443]167
168  //add events to the eventlist
[6997]169  registerEvent(KeyMapper::PEV_FORWARD);
170  registerEvent(KeyMapper::PEV_BACKWARD);
[6637]171  registerEvent(KeyMapper::PEV_LEFT);
172  registerEvent(KeyMapper::PEV_RIGHT);
[6443]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
[7337]180  this->getWeaponManager().setSlotCount(7);
[6443]181
[7337]182  this->getWeaponManager().setSlotPosition(0, Vector(0.0, .1, -1.0));
183  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6443]184
[7337]185  this->getWeaponManager().setSlotPosition(1, Vector(0.0, .1, 1.0));
186  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6443]187
[7337]188  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
189  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[6443]190
[7337]191  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
192  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[6443]193
[7337]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)));
[6443]196
[7337]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)));
[6443]199
[7337]200  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
201  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6947]202
[7337]203  this->getWeaponManager().getFixedTarget()->setParent(&(this->cameraNode));
204  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
[6443]205
[7337]206  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[6443]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{
[6512]216  WorldEntity::loadParams(root);
[6443]217}
218
[6871]219void Helicopter::enter()
[6724]220{
[7337]221  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
222  State::getCameraNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
223  State::getCameraTargetNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
[7810]224
[8495]225  this->soundSource.play(this->chopperBuffer, 0.7f, true);
[6724]226}
[6443]227
228void Helicopter::leave()
229{
[7337]230  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[6443]231  this->detachCamera();
[7810]232  this->soundSource.stop();
[6443]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/**
[6724]245 *  the action occuring if the helicopter left the game
[6443]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{
[6804]268  Playable::tick(time);
269
[6724]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  }
[6804]277
[6724]278  // rotorrotation
279  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
280  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
[6804]281
[6443]282  // spaceship controlled movement
283  this->calculateVelocity(time);
284
[6637]285  Vector move = (velocity)*time;
[6443]286
287  // this is the air friction (necessary for a smooth control)
288  if(velocity.len() != 0) velocity -= velocity*0.1;
[6804]289
[6443]290
291  //readjust
[6947]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)));
[6443]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);
[6637]306  float rotValX = 0.0;
307  float rotValZ = 0.0;
[6443]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();
[6804]315
[7096]316     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z) * 3;
[6637]317     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
[6443]318   }
319   else
320   {
[7096]321       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1))) ;
[6443]322   }
323
324  if( this->bDown )
325   {
326     //this->shiftCoor((this->getAbsDirX())*-1);
327     //accel -= this->getAbsDirY();
[6804]328
[7096]329     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z)*3;
[6637]330     rotValZ += time;
[6443]331   }
332   else
333   {
[6637]334         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
[6443]335   }
336
[6947]337  if( this->bLeft )
[6443]338  {
339    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
340    //accel -= this->getAbsDirY();
341    //velocityDir.normalize();
[6804]342
[6637]343    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
344    rotValX -= time;
[6443]345  }
346  else
347   {
[6637]348         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
[6443]349   }
350
[6947]351  if( this->bRight )
[6443]352  {
353    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
[6637]354    //accel += this->getAbsDirY();
[6443]355    //velocityDir.normalize();
[6804]356
[6637]357    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
358    rotValX += time;
[6443]359  }
360  else
361   {
[6637]362         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
[6443]363   }
364
[6947]365  if( this->bRollL )
[6443]366  {
367    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
368  }
[6947]369  if( this->bRollR )
[6443]370  {
371    this->shiftDir(Quaternion(time, Vector(1,0,0)));
372  }
373  if (this->bAscend )
374  {
[6637]375    accel += this->getAbsDirY();
[6443]376  }
377
378  if (this->bDescend )
379  {
[6637]380    accel -= this->getAbsDirY();
[6443]381  }
382
[6637]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)));
[6443]386}
387
388
389void Helicopter::draw() const
390{
391  WorldEntity::draw();
[6804]392
[6724]393  glMatrixMode(GL_MODELVIEW);
394    glPushMatrix();
[6443]395
[6724]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 ();
[6443]416}
417
418/**
419 * @todo switch statement ??
420 */
421void Helicopter::process(const Event &event)
422{
[6804]423  Playable::process(event);
[6443]424
[6637]425  if( event.type == KeyMapper::PEV_LEFT)
[6443]426      this->bLeft = event.bPressed;
[6637]427  else if( event.type == KeyMapper::PEV_RIGHT)
[6443]428      this->bRight = event.bPressed;
429  else if( event.type == SDLK_e)
[6947]430    this->bAscend = event.bPressed;
[6443]431  else if( event.type == SDLK_c)
[6947]432    this->bDescend = event.bPressed;
[6997]433  else if( event.type == KeyMapper::PEV_FORWARD)
[6947]434    this->bUp = event.bPressed;
[6997]435  else if( event.type == KeyMapper::PEV_BACKWARD)
[6947]436    this->bDown = event.bPressed;
[6443]437  else if( event.type == EV_MOUSE_MOTION)
438  {
439    this->xMouse = event.xRel*mouseSensitivity;
440    this->yMouse = event.yRel*mouseSensitivity;
441
[6724]442    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
[6804]443
[6724]444    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
445
[6804]446
[6724]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    }
[6443]460}
Note: See TracBrowser for help on using the repository browser.