Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new helicopter control

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 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}
98
99
100/**
101 * initializes a Helicopter
102 */
103void Helicopter::init()
104{
105//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
106  this->setClassID(CL_HELICOPTER, "Helicopter");
107
108  PRINTF(4)("SPACESHIP INIT\n");
109
110  this->loadModel("models/ships/helicopter_#.obj", 1.0);
111
112  EventHandler::getInstance()->grabEvents(true);
113
114  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
115  bFire = false;
116  xMouse = yMouse = 0;
117  mouseSensitivity = 0.001;
118
119  cycle = 0.0;
120
121
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
132  registerEvent(SDLK_w);
133  registerEvent(SDLK_s);
134  registerEvent(SDLK_a);
135  registerEvent(SDLK_d);
136  registerEvent(SDLK_q);
137  registerEvent(SDLK_e);
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);
143  registerEvent(EV_MOUSE_MOTION);
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/**
177 * loads the Settings of a Helicopter from an XML-element.
178 * @param root the XML-element to load the Spaceship's properties from
179 */
180void Helicopter::loadParams(const TiXmlElement* root)
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*/
190void Helicopter::addWeapon(Weapon* weapon)
191{
192  this->getWeaponManager()->addWeapon(weapon);
193}
194
195
196/**
197 *  removes a weapon from the spaceship
198 * @param weapon to remove
199*/
200void Helicopter::removeWeapon(Weapon* weapon)
201{
202  this->getWeaponManager()->removeWeapon(weapon);
203}
204
205/**
206 *  effect that occurs after the Helicopter is spawned
207*/
208void Helicopter::postSpawn ()
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*/
216void Helicopter::leftWorld ()
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 */
225void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
226{
227  if (entity->isA(CL_TURRET_POWER_UP))
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*/
240void Helicopter::tick (float time)
241{
242  // spaceship controlled movement
243  this->calculateVelocity(time);
244
245  Vector move = (velocity)*time;
246
247  // this is the air friction (necessary for a smooth control)
248  if(velocity.len() != 0) velocity -= velocity*0.1;
249 
250
251  //hoover effect
252  cycle += time;
253  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
254
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
259  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
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*/
272void Helicopter::calculateVelocity (float time)
273{
274  Vector accel(0.0, 0.0, 0.0);
275  Vector rot(0.0, 0.0, 0.0);
276  float rotVal = 0.0;
277  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
278  /* calculate the direction in which the craft is heading  */
279
280  if( this->bUp )
281   {
282     //this->shiftCoor(this->getAbsDirX());
283     accel += Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2;
284     if((this->getAbsDirX()).y > -.2)
285     {
286       rot += Vector (0,0,1);
287       rotVal -= .4;
288     }
289   }
290   else
291   {
292       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/5, Vector(0,0,1)));
293   }
294
295  if( this->bDown )
296   {
297     //this->shiftCoor((this->getAbsDirX())*-1);
298     accel -= Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2;
299     if((this->getAbsDirX()).y < .2)
300     {
301       rot += Vector (0,0,1);
302       rotVal += .4;
303     }
304   }
305   else
306   {
307         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time/5, Vector(0,0,1)));
308   }
309
310  if( this->bLeft /* > -this->getRelCoor().z*2*/)
311  {
312    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
313    accel -= Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
314    //velocityDir.normalize();
315    if((this->getAbsDirZ()).y < .2)
316    {
317      rot +=Vector(1,0,0);
318      rotVal -= .06;
319    }
320  }
321  if( this->bRight /* > this->getRelCoor().z*2*/)
322  {
323    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
324
325    accel += Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
326    //velocityDir.normalize();
327    if((this->getAbsDirZ()).y > -.2)
328    {
329      rot += Vector(1,0,0);
330      rotVal += .06;
331    }
332  }
333
334  if( this->bRollL /* > -this->getRelCoor().z*2*/)
335  {
336    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
337    //accel -= rightDirection;
338    //velocityDir.normalize();
339    //rot +=Vector(1,0,0);
340    //rotVal -= .4;
341  }
342  if( this->bRollR /* > this->getRelCoor().z*2*/)
343  {
344    this->shiftDir(Quaternion(time, Vector(1,0,0)));
345
346    //accel += rightDirection;
347    //velocityDir.normalize();
348    //rot += Vector(1,0,0);
349    //rotVal += .4;
350  }
351  if (this->bAscend )
352  {
353    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
354
355    accel += (this->getAbsDirY())*2;
356    //velocityDir.normalize();
357    //rot += Vector(0,0,1);
358    //rotVal += .4;
359  }
360  if (this->bDescend )
361  {
362    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
363
364    accel -= (this->getAbsDirY())*2;
365    //velocityDir.normalize();
366    //rot += Vector(0,0,1);
367    //rotVal -= .4;
368  }
369
370  velocity += accel;
371  rot.normalize();
372  this->shiftDir(Quaternion(rotVal, rot));
373}
374
375
376void Helicopter::draw() const
377{
378  this->drawLODsafe();
379
380  this->getWeaponManager()->draw();
381}
382
383
384/**
385 * weapon manipulation by the player
386*/
387void Helicopter::weaponAction()
388{
389  if( this->bFire)
390    {
391      this->getWeaponManager()->fire();
392    }
393}
394
395/**
396 * @todo switch statement ??
397 */
398void Helicopter::process(const Event &event)
399{
400
401
402  if( event.type == SDLK_a)
403      this->bLeft = event.bPressed;
404  else if( event.type == SDLK_d)
405      this->bRight = event.bPressed;
406  else if( event.type == KeyMapper::PEV_FIRE1)
407      this->bFire = event.bPressed;
408  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
409    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
410  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
411    this->getWeaponManager()->previousWeaponConfig();
412  else if( event.type == SDLK_e)
413    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
414  else if( event.type == SDLK_c)
415    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
416  else if( event.type == SDLK_w)
417    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
418  else if( event.type == SDLK_s)
419    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
420  else if( event.type == EV_MOUSE_MOTION)
421  {
422    this->xMouse = event.xRel;
423    this->yMouse = event.yRel;
424    if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0;
425    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
426  }
427}
428
429#include "weapons/aiming_turret.h"
430// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
431void Helicopter::ADDWEAPON()
432{
433  Weapon* turret = NULL;
434
435  if ((float)rand()/RAND_MAX < .1)
436  {
437    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
438    {
439      turret = new Turret();
440      this->getWeaponManager()->addWeapon(turret, 2);
441      this->getWeaponManager()->changeWeaponConfig(2);
442    }
443  }
444  else
445  {
446    //if (this->getWeaponManager()->hasFreeSlot(3))
447    {
448      turret = new AimingTurret();
449      this->getWeaponManager()->addWeapon(turret, 3);
450
451      this->getWeaponManager()->changeWeaponConfig(3);
452    }
453  }
454
455  if(turret != NULL)
456  {
457    turret->setName("Turret");
458    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
459  }
460}
Note: See TracBrowser for help on using the repository browser.