Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6699 was 6637, checked in by bensch, 19 years ago

orxonox/trunk: merged the SpaceShipControl branche back to the trunk

merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/spaceshipcontrol . -r6482:HEAD

minor conflicts, and added file hover.cc/h to the Makefile.

File size: 13.2 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.05;
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(KeyMapper::PEV_UP);
137  registerEvent(KeyMapper::PEV_DOWN);
138  registerEvent(KeyMapper::PEV_LEFT);
139  registerEvent(KeyMapper::PEV_RIGHT);
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  WorldEntity::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  /*
252  tailrotorspeed += xMouse/20;
253  if (tailrotorspeed >= 0.07) tailrotorspeed = 0.07;
254  else if (tailrotorspeed <= -0.07) tailrotorspeed = -0.07;
255
256  if (tailrotorspeed > 0.0008) tailrotorspeed -= 0.001;
257  else if (tailrotorspeed < -0.0008) tailrotorspeed += 0.001;
258  if (tailrotorspeed <= 0.001 && tailrotorspeed >= -0.001) tailrotorspeed = 0;
259  */
260 
261  // spaceship controlled movement
262  this->calculateVelocity(time);
263
264  Vector move = (velocity)*time;
265
266  // this is the air friction (necessary for a smooth control)
267  if(velocity.len() != 0) velocity -= velocity*0.1;
268 
269  //travelSpeed = velocity.len();
270
271  //physics: Gravity
272  /*this->shiftCoor(Vector(0,-1,0));
273
274  this->shiftCoor(getAbsDirY()*rotorspeed);
275  */
276 
277  /*
278  //hoover effect
279  cycle += time;
280  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
281  */
282
283  //readjust
284 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
285  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
286
287  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
288
289  this->shiftCoor (move);
290  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
291
292  this->getWeaponManager()->tick(time);
293  // weapon system manipulation
294  this->weaponAction();
295}
296
297/**
298 *  calculate the velocity
299 * @param time the timeslice since the last frame
300*/
301void Helicopter::calculateVelocity (float time)
302{
303  Vector accel(0.0, 0.0, 0.0);
304  float rotValX = 0.0;
305  float rotValZ = 0.0;
306  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
307  /* calculate the direction in which the craft is heading  */
308
309  if( this->bUp )
310   {
311     //this->shiftCoor(this->getAbsDirX());
312     //accel -= this->getAbsDirY();
313     
314     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
315     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
316   }
317   else
318   {
319       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
320   }
321
322  if( this->bDown )
323   {
324     //this->shiftCoor((this->getAbsDirX())*-1);
325     //accel -= this->getAbsDirY();
326     
327     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
328     rotValZ += time;
329   }
330   else
331   {
332         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
333   }
334
335  if( this->bLeft /* > -this->getRelCoor().z*2*/)
336  {
337    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
338    //accel -= this->getAbsDirY();
339    //velocityDir.normalize();
340   
341    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
342    rotValX -= time;
343  }
344  else
345   {
346         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
347   }
348
349  if( this->bRight /* > this->getRelCoor().z*2*/)
350  {
351    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
352    //accel += this->getAbsDirY();
353    //velocityDir.normalize();
354   
355    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
356    rotValX += time;
357  }
358  else
359   {
360         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
361   }
362
363  if( this->bRollL /* > -this->getRelCoor().z*2*/)
364  {
365    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
366    //accel -= rightDirection;
367    //velocityDir.normalize();
368    //rot +=Vector(1,0,0);
369    //rotVal -= .4;
370  }
371  if( this->bRollR /* > this->getRelCoor().z*2*/)
372  {
373    this->shiftDir(Quaternion(time, Vector(1,0,0)));
374
375    //accel += rightDirection;
376    //velocityDir.normalize();
377    //rot += Vector(1,0,0);
378    //rotVal += .4;
379  }
380  if (this->bAscend )
381  {
382    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
383
384    accel += this->getAbsDirY();
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    accel -= this->getAbsDirY();
401    //rotorspeed -= 0.05;
402    //if (rotorspeed <= 0) rotorspeed = 0;
403    //velocityDir.normalize();
404    //rot += Vector(0,0,1);
405    //rotVal -= .4;
406  }
407  else
408  {
409    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
410  }
411
412  velocity += accel*3;
413  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
414  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
415}
416
417
418void Helicopter::draw() const
419{
420  WorldEntity::draw();
421
422  this->getWeaponManager()->draw();
423}
424
425
426/**
427 * weapon manipulation by the player
428*/
429void Helicopter::weaponAction()
430{
431  if( this->bFire)
432    {
433      this->getWeaponManager()->fire();
434    }
435}
436
437/**
438 * @todo switch statement ??
439 */
440void Helicopter::process(const Event &event)
441{
442
443
444  if( event.type == KeyMapper::PEV_LEFT)
445      this->bLeft = event.bPressed;
446  else if( event.type == KeyMapper::PEV_RIGHT)
447      this->bRight = event.bPressed;
448  else if( event.type == KeyMapper::PEV_FIRE1)
449      this->bFire = event.bPressed;
450  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
451    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
452  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
453    this->getWeaponManager()->previousWeaponConfig();
454  else if( event.type == SDLK_e)
455    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
456  else if( event.type == SDLK_c)
457    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
458  else if( event.type == KeyMapper::PEV_UP)
459    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
460  else if( event.type == KeyMapper::PEV_DOWN)
461    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
462  else if( event.type == EV_MOUSE_MOTION)
463  {
464    this->xMouse = event.xRel*mouseSensitivity;
465    this->yMouse = event.yRel*mouseSensitivity;
466
467    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))/*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1))*/);
468  }
469}
470
471#include "weapons/aiming_turret.h"
472// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
473void Helicopter::ADDWEAPON()
474{
475  Weapon* turret = NULL;
476
477  if ((float)rand()/RAND_MAX < .1)
478  {
479    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
480    {
481      turret = new Turret();
482      this->addWeapon(turret, 2);
483      this->getWeaponManager()->changeWeaponConfig(2);
484    }
485  }
486  else
487  {
488    //if (this->getWeaponManager()->hasFreeSlot(3))
489    {
490      turret = new AimingTurret();
491      this->addWeapon(turret, 3);
492
493      this->getWeaponManager()->changeWeaponConfig(3);
494    }
495  }
496
497  if(turret != NULL)
498  {
499    turret->setName("Turret");
500    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
501  }
502}
Note: See TracBrowser for help on using the repository browser.