Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8141 was 7868, checked in by bensch, 19 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

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