Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/gui/src/world_entities/space_ships/helicopter.cc @ 8471

Last change on this file since 8471 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
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"
38//CREATE_SCRIPTABLE_CLASS(Helicopter, CL_HELICOPTER, NULL);
39
40
41/**
42 *  creates the controlable Helicopter
43 */
44Helicopter::Helicopter()
45{
46  this->init();
47}
48
49/**
50 *  destructs the helicopter, deletes alocated memory
51 */
52Helicopter::~Helicopter ()
53{
54  this->setPlayer(NULL);
55
56  if (this->chopperBuffer != NULL)
57    ResourceManager::getInstance()->unload(this->chopperBuffer);
58}
59
60/**
61 * loads a Helicopter information from a specified file.
62 * @param fileName the name of the File to load the helicopter from (absolute path)
63 */
64Helicopter::Helicopter(const std::string& fileName)
65{
66  this->init();
67  TiXmlDocument doc(fileName);
68
69  if(!doc.LoadFile())
70  {
71    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName.c_str());
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
103  this->getWeaponManager().changeWeaponConfig(1);
104  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
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
111}
112
113
114/**
115 * initializes a Helicopter
116 */
117void Helicopter::init()
118{
119  this->setClassID(CL_HELICOPTER, "Helicopter");
120
121  PRINTF(4)("HELICOPTER INIT\n");
122
123  this->loadModel("models/ships/helicopter_#.obj", 1.0);
124
125  //EventHandler::getInstance()->grabEvents(true);
126
127  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
128  bFire = false;
129  xMouse = yMouse = 0;
130  mouseSensitivity = 0.05;
131  controlVelocityX = 100;
132  controlVelocityY = 100;
133
134
135  // initialization of cameraNode
136  this->cameraNode.setParent(this);
137  this->cameraNode.setParentMode(PNODE_ALL);
138  this->cameraNode.setRelCoor(Vector(0,1,0));
139
140  // rotors
141  this->topRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
142  this->tailRotor.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
143
144  this->topRotor.setParent(this);
145  this->tailRotor.setParent(this);
146
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)));
150
151  this->loadModel("models/ships/rotor.obj",1.0,3);
152  this->loadModel("models/ships/rotor.obj",0.2,4);
153
154
155  this->velocity = Vector(0.0,0.0,0.0);
156  this->velocityDir = Vector(1.0,0.0,0.0);
157
158  // very, very old stuff
159  //  GLGuiButton* button = new GLGuiPushButton();
160  //  button->show();
161  //  button->setLabel("orxonox");
162  //  button->setBindNode(this);
163
164  //add events to the eventlist
165  registerEvent(KeyMapper::PEV_FORWARD);
166  registerEvent(KeyMapper::PEV_BACKWARD);
167  registerEvent(KeyMapper::PEV_LEFT);
168  registerEvent(KeyMapper::PEV_RIGHT);
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
176  this->getWeaponManager().setSlotCount(7);
177
178  this->getWeaponManager().setSlotPosition(0, Vector(0.0, .1, -1.0));
179  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
180
181  this->getWeaponManager().setSlotPosition(1, Vector(0.0, .1, 1.0));
182  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
183
184  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
185  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
186
187  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
188  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
189
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)));
192
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)));
195
196  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
197  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
198
199  this->getWeaponManager().getFixedTarget()->setParent(&(this->cameraNode));
200  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
201
202  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
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{
212  WorldEntity::loadParams(root);
213}
214
215void Helicopter::enter()
216{
217  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
218  State::getCameraNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
219  State::getCameraTargetNode()->setParentSoft(this->getWeaponManager().getFixedTarget());
220
221  this->soundSource.loop(this->chopperBuffer, 0.8f);
222  // PRINTF(0)( "Playing ChopperSound\n" );
223}
224
225void Helicopter::leave()
226{
227  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
228  this->detachCamera();
229  this->soundSource.stop();
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/**
242 *  the action occuring if the helicopter left the game
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{
265  Playable::tick(time);
266
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  }
274
275  // rotorrotation
276  this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
277  this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
278
279  // spaceship controlled movement
280  this->calculateVelocity(time);
281
282  Vector move = (velocity)*time;
283
284  // this is the air friction (necessary for a smooth control)
285  if(velocity.len() != 0) velocity -= velocity*0.1;
286
287
288  //readjust
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)));
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);
303  float rotValX = 0.0;
304  float rotValZ = 0.0;
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();
312
313     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z) * 3;
314     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
315   }
316   else
317   {
318       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1))) ;
319   }
320
321  if( this->bDown )
322   {
323     //this->shiftCoor((this->getAbsDirX())*-1);
324     //accel -= this->getAbsDirY();
325
326     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z)*3;
327     rotValZ += time;
328   }
329   else
330   {
331         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
332   }
333
334  if( this->bLeft )
335  {
336    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
337    //accel -= this->getAbsDirY();
338    //velocityDir.normalize();
339
340    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
341    rotValX -= time;
342  }
343  else
344   {
345         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
346   }
347
348  if( this->bRight )
349  {
350    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
351    //accel += this->getAbsDirY();
352    //velocityDir.normalize();
353
354    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
355    rotValX += time;
356  }
357  else
358   {
359         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
360   }
361
362  if( this->bRollL )
363  {
364    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
365  }
366  if( this->bRollR )
367  {
368    this->shiftDir(Quaternion(time, Vector(1,0,0)));
369  }
370  if (this->bAscend )
371  {
372    accel += this->getAbsDirY();
373  }
374
375  if (this->bDescend )
376  {
377    accel -= this->getAbsDirY();
378  }
379
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)));
383}
384
385
386void Helicopter::draw() const
387{
388  WorldEntity::draw();
389
390  glMatrixMode(GL_MODELVIEW);
391    glPushMatrix();
392
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 ();
413}
414
415/**
416 * @todo switch statement ??
417 */
418void Helicopter::process(const Event &event)
419{
420  Playable::process(event);
421
422  if( event.type == KeyMapper::PEV_LEFT)
423      this->bLeft = event.bPressed;
424  else if( event.type == KeyMapper::PEV_RIGHT)
425      this->bRight = event.bPressed;
426  else if( event.type == SDLK_e)
427    this->bAscend = event.bPressed;
428  else if( event.type == SDLK_c)
429    this->bDescend = event.bPressed;
430  else if( event.type == KeyMapper::PEV_FORWARD)
431    this->bUp = event.bPressed;
432  else if( event.type == KeyMapper::PEV_BACKWARD)
433    this->bDown = event.bPressed;
434  else if( event.type == EV_MOUSE_MOTION)
435  {
436    this->xMouse = event.xRel*mouseSensitivity;
437    this->yMouse = event.yRel*mouseSensitivity;
438
439    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
440
441    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
442
443
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    }
457}
Note: See TracBrowser for help on using the repository browser.