Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: merged the script_engine branche back here
merged with command
svn merge https://svn.orxonox.net/orxonox/branches/script_engine . -r8284:HEAD
no conflicts

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