Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6476 was 6443, checked in by bensch, 19 years ago

huge pipeline

File size: 12.5 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 Grauer
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 "factory.h"
27#include "key_mapper.h"
28#include "event_handler.h"
29#include "state.h"
30
31#include "graphics_engine.h"
32
33using namespace std;
34
35CREATE_FACTORY(Helicopter, CL_HELICOPTER);
36
37/**
38 *  creates the controlable Spaceship
39 */
40Helicopter::Helicopter()
41{
42  this->init();
43}
44
45/**
46 *  destructs the spaceship, deletes alocated memory
47 */
48Helicopter::~Helicopter ()
49{
50}
51
52/**
53 * loads a Spaceships information from a specified file.
54 * @param fileName the name of the File to load the spaceship from (absolute path)
55 */
56Helicopter::Helicopter(const char* fileName)
57{
58  this->init();
59  TiXmlDocument doc(fileName);
60
61  if(!doc.LoadFile())
62  {
63    PRINTF(2)("Loading file %s failed for Helicopter.\n", fileName);
64    return;
65  }
66
67  this->loadParams(doc.RootElement());
68}
69
70/**
71 *  creates a new Spaceship from Xml Data
72 * @param root the xml element containing spaceship data
73
74   @todo add more parameters to load
75*/
76Helicopter::Helicopter(const TiXmlElement* root)
77{
78  this->init();
79  if (root != NULL)
80    this->loadParams(root);
81
82  //weapons:
83  Weapon* wpRight = new TestGun(0);
84  wpRight->setName("testGun Right");
85  Weapon* wpLeft = new TestGun(1);
86  wpLeft->setName("testGun Left");
87  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
88
89  cannon->setName("BFG");
90
91  this->addWeapon(wpLeft, 1, 0);
92  this->addWeapon(wpRight,1 ,1);
93  this->addWeapon(cannon, 0, 6);
94
95  //this->addWeapon(turret, 3, 0);
96
97  this->getWeaponManager()->changeWeaponConfig(1);
98  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
99}
100
101
102/**
103 * initializes a Helicopter
104 */
105void Helicopter::init()
106{
107//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
108  this->setClassID(CL_HELICOPTER, "Helicopter");
109
110  PRINTF(4)("SPACESHIP INIT\n");
111
112  this->loadModel("models/ships/helicopter_#.obj", 1.0);
113
114  EventHandler::getInstance()->grabEvents(true);
115
116  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
117  bFire = false;
118  xMouse = yMouse = 0;
119  mouseSensitivity = 0.001;
120  rotorspeed = 1;
121  tailrotorspeed = 0;
122
123  cycle = 0.0;
124
125
126  travelSpeed = 15.0;
127  this->velocity = Vector(0.0,0.0,0.0);
128  this->velocityDir = Vector(1.0,0.0,0.0);
129
130//   GLGuiButton* button = new GLGuiPushButton();
131//   button->show();
132//   button->setLabel("orxonox");
133//   button->setBindNode(this);
134
135  //add events to the eventlist
136  registerEvent(SDLK_w);
137  registerEvent(SDLK_s);
138  registerEvent(SDLK_a);
139  registerEvent(SDLK_d);
140  registerEvent(SDLK_q);
141  registerEvent(SDLK_e);
142  registerEvent(SDLK_c);
143  registerEvent(KeyMapper::PEV_FIRE1);
144  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
145  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
146  registerEvent(SDLK_PAGEUP);
147  registerEvent(SDLK_PAGEDOWN);
148  registerEvent(EV_MOUSE_MOTION);
149
150  this->getWeaponManager()->setSlotCount(7);
151
152  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
153  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154
155  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
156  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
157
158  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
159  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
160
161  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
162  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
163
164  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
165  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
166
167  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
168  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
169//
170   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
171   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
172   //
173//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
174//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
175//
176//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
177//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
178
179  this->getWeaponManager()->getFixedTarget()->setParent(this);
180  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
181
182  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
183
184}
185
186/**
187 * loads the Settings of a Helicopter from an XML-element.
188 * @param root the XML-element to load the Spaceship's properties from
189 */
190void Helicopter::loadParams(const TiXmlElement* root)
191{
192  static_cast<WorldEntity*>(this)->loadParams(root);
193}
194
195
196
197
198void Helicopter::enter()
199{
200  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
201  this->attachCamera();
202
203
204}
205
206void Helicopter::leave()
207{
208  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
209  this->detachCamera();
210
211}
212
213
214/**
215 *  effect that occurs after the Helicopter is spawned
216*/
217void Helicopter::postSpawn ()
218{
219  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
220}
221
222/**
223 *  the action occuring if the spaceship left the game
224*/
225void Helicopter::leftWorld ()
226{}
227
228/**
229 *  this function is called, when two entities collide
230 * @param entity: the world entity with whom it collides
231 *
232 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
233 */
234void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
235{
236  if (entity->isA(CL_TURRET_POWER_UP))
237  {
238    this->ADDWEAPON();
239    }
240//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
241}
242
243
244
245/**
246 *  the function called for each passing timeSnap
247 * @param time The timespan passed since last update
248*/
249void Helicopter::tick (float time)
250{
251  tailrotorspeed += xMouse/20;
252  if (tailrotorspeed >= 0.07) tailrotorspeed = 0.07;
253  else if (tailrotorspeed <= -0.07) tailrotorspeed = -0.07;
254
255  if (tailrotorspeed > 0.0008) tailrotorspeed -= 0.001;
256  else if (tailrotorspeed < -0.0008) tailrotorspeed += 0.001;
257  if (tailrotorspeed <= 0.001 && tailrotorspeed >= -0.001) tailrotorspeed = 0;
258
259  // spaceship controlled movement
260  this->calculateVelocity(time);
261
262  Vector move = (velocity);
263
264  // this is the air friction (necessary for a smooth control)
265  if(velocity.len() != 0) velocity -= velocity*0.1;
266
267  //physics: Gravity
268  this->shiftCoor(Vector(0,-1,0));
269
270  this->shiftCoor(getAbsDirY()*rotorspeed);
271
272  //hoover effect
273  cycle += time;
274  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
275
276  //readjust
277 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
278  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
279
280  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
281
282  this->shiftCoor (move);
283  this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
284
285  this->getWeaponManager()->tick(time);
286  // weapon system manipulation
287  this->weaponAction();
288}
289
290/**
291 *  calculate the velocity
292 * @param time the timeslice since the last frame
293*/
294void Helicopter::calculateVelocity (float time)
295{
296  Vector accel(0.0, 0.0, 0.0);
297  Vector rot(0.0, 0.0, 0.0);
298  float rotVal = 0.0;
299  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
300  /* calculate the direction in which the craft is heading  */
301
302  if( this->bUp )
303   {
304     //this->shiftCoor(this->getAbsDirX());
305     //accel -= this->getAbsDirY();
306     rot += Vector (0,0,1);
307     rotVal -= time/5;
308   }
309   else
310   {
311       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/8, Vector(0,0,1)));
312   }
313
314  if( this->bDown )
315   {
316     //this->shiftCoor((this->getAbsDirX())*-1);
317     //accel -= this->getAbsDirY();
318     rot += Vector (0,0,1);
319     rotVal += time/5;
320   }
321   else
322   {
323         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time/8, Vector(0,0,1)));
324   }
325
326  if( this->bLeft /* > -this->getRelCoor().z*2*/)
327  {
328    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
329    //accel -= this->getAbsDirY();
330    //velocityDir.normalize();
331    rot += Vector(1,0,0);
332    rotVal -= time/5;
333  }
334  else
335   {
336         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0)));
337   }
338
339  if( this->bRight /* > this->getRelCoor().z*2*/)
340  {
341    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
342    accel += this->getAbsDirY();
343    //velocityDir.normalize();
344    rot += Vector(1,0,0);
345    rotVal += time/5;
346  }
347  else
348   {
349         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0)));
350   }
351
352  if( this->bRollL /* > -this->getRelCoor().z*2*/)
353  {
354    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
355    //accel -= rightDirection;
356    //velocityDir.normalize();
357    //rot +=Vector(1,0,0);
358    //rotVal -= .4;
359  }
360  if( this->bRollR /* > this->getRelCoor().z*2*/)
361  {
362    this->shiftDir(Quaternion(time, Vector(1,0,0)));
363
364    //accel += rightDirection;
365    //velocityDir.normalize();
366    //rot += Vector(1,0,0);
367    //rotVal += .4;
368  }
369  if (this->bAscend )
370  {
371    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
372
373    rotorspeed += 0.05;
374    if (rotorspeed >= 2) rotorspeed = 2;
375    //velocityDir.normalize();
376    //rot += Vector(0,0,1);
377    //rotVal += .4;
378  }
379  else
380  {
381    if(rotorspeed >= 1.05) rotorspeed -= 0.05;
382  }
383
384  if (this->bDescend )
385  {
386    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
387
388    rotorspeed -= 0.05;
389    if (rotorspeed <= 0) rotorspeed = 0;
390    //velocityDir.normalize();
391    //rot += Vector(0,0,1);
392    //rotVal -= .4;
393  }
394  else
395  {
396    if(rotorspeed <= 0.05) rotorspeed += 0.05;
397  }
398
399  //velocity += accel;
400  rot.normalize();
401  this->shiftDir(Quaternion(rotVal, rot));
402}
403
404
405void Helicopter::draw() const
406{
407  WorldEntity::draw();
408
409  this->getWeaponManager()->draw();
410}
411
412
413/**
414 * weapon manipulation by the player
415*/
416void Helicopter::weaponAction()
417{
418  if( this->bFire)
419    {
420      this->getWeaponManager()->fire();
421    }
422}
423
424/**
425 * @todo switch statement ??
426 */
427void Helicopter::process(const Event &event)
428{
429
430
431  if( event.type == SDLK_a)
432      this->bLeft = event.bPressed;
433  else if( event.type == SDLK_d)
434      this->bRight = event.bPressed;
435  else if( event.type == KeyMapper::PEV_FIRE1)
436      this->bFire = event.bPressed;
437  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
438    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
439  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
440    this->getWeaponManager()->previousWeaponConfig();
441  else if( event.type == SDLK_e)
442    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
443  else if( event.type == SDLK_c)
444    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
445  else if( event.type == SDLK_w)
446    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
447  else if( event.type == SDLK_s)
448    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
449  else if( event.type == EV_MOUSE_MOTION)
450  {
451    this->xMouse = event.xRel*mouseSensitivity;
452    this->yMouse = event.yRel*mouseSensitivity;
453
454    //this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
455  }
456}
457
458#include "weapons/aiming_turret.h"
459// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
460void Helicopter::ADDWEAPON()
461{
462  Weapon* turret = NULL;
463
464  if ((float)rand()/RAND_MAX < .1)
465  {
466    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
467    {
468      turret = new Turret();
469      this->addWeapon(turret, 2);
470      this->getWeaponManager()->changeWeaponConfig(2);
471    }
472  }
473  else
474  {
475    //if (this->getWeaponManager()->hasFreeSlot(3))
476    {
477      turret = new AimingTurret();
478      this->addWeapon(turret, 3);
479
480      this->getWeaponManager()->changeWeaponConfig(3);
481    }
482  }
483
484  if(turret != NULL)
485  {
486    turret->setName("Turret");
487    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
488  }
489}
Note: See TracBrowser for help on using the repository browser.