Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6275 was 6242, checked in by bknecht, 19 years ago

Control: Helicopter updated

File size: 12.7 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
120  cycle = 0.0;
121
122
123  travelSpeed = 15.0;
124  this->velocity = Vector(0.0,0.0,0.0);
125  this->velocityDir = Vector(1.0,0.0,0.0);
126
127//   GLGuiButton* button = new GLGuiPushButton();
128//   button->show();
129//   button->setLabel("orxonox");
130//   button->setBindNode(this);
131
132  //add events to the eventlist
133  registerEvent(SDLK_w);
134  registerEvent(SDLK_s);
135  registerEvent(SDLK_a);
136  registerEvent(SDLK_d);
137  registerEvent(SDLK_q);
138  registerEvent(SDLK_e);
139  registerEvent(SDLK_c);
140  registerEvent(KeyMapper::PEV_FIRE1);
141  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
142  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
143  registerEvent(SDLK_PAGEUP);
144  registerEvent(SDLK_PAGEDOWN);
145  registerEvent(EV_MOUSE_MOTION);
146
147  this->getWeaponManager()->setSlotCount(7);
148
149  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
150  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
151
152  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
153  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
154
155  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
156  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
157
158  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
159  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
160
161  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
162  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
163
164  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
165  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
166//
167   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
168   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169   //
170//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
171//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
172//
173//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
174//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
175
176}
177
178/**
179 * loads the Settings of a Helicopter from an XML-element.
180 * @param root the XML-element to load the Spaceship's properties from
181 */
182void Helicopter::loadParams(const TiXmlElement* root)
183{
184  static_cast<WorldEntity*>(this)->loadParams(root);
185}
186
187
188
189
190void Helicopter::enter()
191{
192  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
193  this->attachCamera();
194
195}
196
197void Helicopter::leave()
198{
199  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
200  this->detachCamera();
201
202}
203
204
205/**
206 * adds a weapon to the weapon list of the spaceship
207 * @param weapon to add
208*/
209void Helicopter::addWeapon(Weapon* weapon)
210{
211  this->getWeaponManager()->addWeapon(weapon);
212}
213
214
215/**
216 *  removes a weapon from the spaceship
217 * @param weapon to remove
218*/
219void Helicopter::removeWeapon(Weapon* weapon)
220{
221  this->getWeaponManager()->removeWeapon(weapon);
222}
223
224/**
225 *  effect that occurs after the Helicopter is spawned
226*/
227void Helicopter::postSpawn ()
228{
229  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
230}
231
232/**
233 *  the action occuring if the spaceship left the game
234*/
235void Helicopter::leftWorld ()
236{}
237
238/**
239 *  this function is called, when two entities collide
240 * @param entity: the world entity with whom it collides
241 *
242 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
243 */
244void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
245{
246  if (entity->isA(CL_TURRET_POWER_UP))
247  {
248    this->ADDWEAPON();
249    }
250//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
251}
252
253
254
255/**
256 *  the function called for each passing timeSnap
257 * @param time The timespan passed since last update
258*/
259void Helicopter::tick (float time)
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  //physics: Gravity
270  this->shiftCoor(Vector(0,-0.05,0));
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
284  this->getWeaponManager()->tick(time);
285  // weapon system manipulation
286  this->weaponAction();
287}
288
289/**
290 *  calculate the velocity
291 * @param time the timeslice since the last frame
292*/
293void Helicopter::calculateVelocity (float time)
294{
295  Vector accel(0.0, 0.0, 0.0);
296  Vector rot(0.0, 0.0, 0.0);
297  float rotVal = 0.0;
298  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
299  /* calculate the direction in which the craft is heading  */
300
301  if( this->bUp )
302   {
303     //this->shiftCoor(this->getAbsDirX());
304
305     accel += this->getAbsDirX()*2;
306     rot += Vector (0,0,1);
307     rotVal -= time/8;
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->getAbsDirX()*2;
318     rot += Vector (0,0,1);
319     rotVal += time/8;
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->getAbsDirZ()*2;
330    //velocityDir.normalize();
331    rot += Vector(1,0,0);
332    rotVal -= time/8;
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->getAbsDirZ()*2;
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    accel += (this->getAbsDirY())*3;
374    //velocityDir.normalize();
375    //rot += Vector(0,0,1);
376    //rotVal += .4;
377  }
378  if (this->bDescend )
379  {
380    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
381
382    accel -= (this->getAbsDirY())*3;
383    //velocityDir.normalize();
384    //rot += Vector(0,0,1);
385    //rotVal -= .4;
386  }
387
388  velocity += accel;
389  rot.normalize();
390  this->shiftDir(Quaternion(rotVal, rot));
391}
392
393
394void Helicopter::draw() const
395{
396  this->drawLODsafe();
397
398  this->getWeaponManager()->draw();
399}
400
401
402/**
403 * weapon manipulation by the player
404*/
405void Helicopter::weaponAction()
406{
407  if( this->bFire)
408    {
409      this->getWeaponManager()->fire();
410    }
411}
412
413/**
414 * @todo switch statement ??
415 */
416void Helicopter::process(const Event &event)
417{
418
419
420  if( event.type == SDLK_a)
421      this->bLeft = event.bPressed;
422  else if( event.type == SDLK_d)
423      this->bRight = event.bPressed;
424  else if( event.type == KeyMapper::PEV_FIRE1)
425      this->bFire = event.bPressed;
426  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
427    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
428  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
429    this->getWeaponManager()->previousWeaponConfig();
430  else if( event.type == SDLK_e)
431    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
432  else if( event.type == SDLK_c)
433    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
434  else if( event.type == SDLK_w)
435    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
436  else if( event.type == SDLK_s)
437    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
438  else if( event.type == EV_MOUSE_MOTION)
439  {
440    this->xMouse = event.xRel;
441    this->yMouse = event.yRel;
442    if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0;
443    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
444  }
445}
446
447#include "weapons/aiming_turret.h"
448// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
449void Helicopter::ADDWEAPON()
450{
451  Weapon* turret = NULL;
452
453  if ((float)rand()/RAND_MAX < .1)
454  {
455    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
456    {
457      turret = new Turret();
458      this->getWeaponManager()->addWeapon(turret, 2);
459      this->getWeaponManager()->changeWeaponConfig(2);
460    }
461  }
462  else
463  {
464    //if (this->getWeaponManager()->hasFreeSlot(3))
465    {
466      turret = new AimingTurret();
467      this->getWeaponManager()->addWeapon(turret, 3);
468
469      this->getWeaponManager()->changeWeaponConfig(3);
470    }
471  }
472
473  if(turret != NULL)
474  {
475    turret->setName("Turret");
476    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
477  }
478}
Note: See TracBrowser for help on using the repository browser.