Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the spaceshipcontroll branche into the trunk
merged with command
svn merge -r6036:HEAD spaceshipcontrol/ ../trunk/
no conflicts

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