Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/helicopter.cc @ 6378

Last change on this file since 6378 was 6378, checked in by bknecht, 18 years ago

Control: new helicopter control

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