Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/space_ships/helicopter.cc @ 9757

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

new_class_id: hups… this was bad naming… confusing too.

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