Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6429 was 6426, 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 . -r6224:HEAD
small conflict in space_ship.cc fixed in favor of the control

File size: 13.5 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->getWeaponManager()->addWeapon(wpLeft, 1, 0);
92  this->getWeaponManager()->addWeapon(wpRight,1 ,1);
93  this->getWeaponManager()->addWeapon(cannon, 0, 6);
94
95  //this->getWeaponManager()->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.001;
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(SDLK_w);
137  registerEvent(SDLK_s);
138  registerEvent(SDLK_a);
139  registerEvent(SDLK_d);
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  static_cast<WorldEntity*>(this)->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 * adds a weapon to the weapon list of the spaceship
216 * @param weapon to add
217*/
218void Helicopter::addWeapon(Weapon* weapon)
219{
220  this->getWeaponManager()->addWeapon(weapon);
221}
222
223
224/**
225 *  removes a weapon from the spaceship
226 * @param weapon to remove
227*/
228void Helicopter::removeWeapon(Weapon* weapon)
229{
230  this->getWeaponManager()->removeWeapon(weapon);
231}
232
233/**
234 *  effect that occurs after the Helicopter is spawned
235*/
236void Helicopter::postSpawn ()
237{
238  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
239}
240
241/**
242 *  the action occuring if the spaceship left the game
243*/
244void Helicopter::leftWorld ()
245{}
246
247/**
248 *  this function is called, when two entities collide
249 * @param entity: the world entity with whom it collides
250 *
251 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
252 */
253void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
254{
255  if (entity->isA(CL_TURRET_POWER_UP))
256  {
257    this->ADDWEAPON();
258    }
259//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
260}
261
262
263
264/**
265 *  the function called for each passing timeSnap
266 * @param time The timespan passed since last update
267*/
268void Helicopter::tick (float time)
269{ 
270  tailrotorspeed += xMouse/20;
271  if (tailrotorspeed >= 0.07) tailrotorspeed = 0.07;
272  else if (tailrotorspeed <= -0.07) tailrotorspeed = -0.07;
273 
274  if (tailrotorspeed > 0.0008) tailrotorspeed -= 0.001;
275  else if (tailrotorspeed < -0.0008) tailrotorspeed += 0.001;
276  if (tailrotorspeed <= 0.001 && tailrotorspeed >= -0.001) tailrotorspeed = 0;
277 
278  // spaceship controlled movement
279  this->calculateVelocity(time);
280
281  Vector move = (velocity);
282
283  // this is the air friction (necessary for a smooth control)
284  if(velocity.len() != 0) velocity -= velocity*0.1;
285
286  //physics: Gravity
287  this->shiftCoor(Vector(0,-1,0));
288 
289  this->shiftCoor(getAbsDirY()*rotorspeed);
290
291  //hoover effect
292  cycle += time;
293  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
294
295  //readjust
296 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
297  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
298
299  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
300
301  this->shiftCoor (move);
302  this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
303
304  this->getWeaponManager()->tick(time);
305  // weapon system manipulation
306  this->weaponAction();
307}
308
309/**
310 *  calculate the velocity
311 * @param time the timeslice since the last frame
312*/
313void Helicopter::calculateVelocity (float time)
314{
315  Vector accel(0.0, 0.0, 0.0);
316  Vector rot(0.0, 0.0, 0.0);
317  float rotVal = 0.0;
318  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
319  /* calculate the direction in which the craft is heading  */
320
321  if( this->bUp )
322   {
323     //this->shiftCoor(this->getAbsDirX());
324     //accel -= this->getAbsDirY();
325     rot += Vector (0,0,1);
326     rotVal -= time/5;
327   }
328   else
329   {
330       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/8, Vector(0,0,1)));
331   }
332
333  if( this->bDown )
334   {
335     //this->shiftCoor((this->getAbsDirX())*-1);
336     //accel -= this->getAbsDirY();
337     rot += Vector (0,0,1);
338     rotVal += time/5;
339   }
340   else
341   {
342         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time/8, Vector(0,0,1)));
343   }
344
345  if( this->bLeft /* > -this->getRelCoor().z*2*/)
346  {
347    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
348    //accel -= this->getAbsDirY();
349    //velocityDir.normalize();
350    rot += Vector(1,0,0);
351    rotVal -= time/5;
352  }
353  else
354   {
355         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0)));
356   }
357
358  if( this->bRight /* > this->getRelCoor().z*2*/)
359  {
360    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
361    accel += this->getAbsDirY();
362    //velocityDir.normalize();
363    rot += Vector(1,0,0);
364    rotVal += time/5;
365  }
366  else
367   {
368         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0)));
369   }
370
371  if( this->bRollL /* > -this->getRelCoor().z*2*/)
372  {
373    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
374    //accel -= rightDirection;
375    //velocityDir.normalize();
376    //rot +=Vector(1,0,0);
377    //rotVal -= .4;
378  }
379  if( this->bRollR /* > this->getRelCoor().z*2*/)
380  {
381    this->shiftDir(Quaternion(time, Vector(1,0,0)));
382
383    //accel += rightDirection;
384    //velocityDir.normalize();
385    //rot += Vector(1,0,0);
386    //rotVal += .4;
387  }
388  if (this->bAscend )
389  {
390    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
391
392    rotorspeed += 0.05;
393    if (rotorspeed >= 2) rotorspeed = 2;
394    //velocityDir.normalize();
395    //rot += Vector(0,0,1);
396    //rotVal += .4;
397  }
398  else
399  {
400    if(rotorspeed >= 1.05) rotorspeed -= 0.05;
401  }
402 
403  if (this->bDescend )
404  {
405    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
406
407    rotorspeed -= 0.05;
408    if (rotorspeed <= 0) rotorspeed = 0;
409    //velocityDir.normalize();
410    //rot += Vector(0,0,1);
411    //rotVal -= .4;
412  }
413  else
414  {
415    if(rotorspeed <= 0.05) rotorspeed += 0.05;
416  }
417
418  //velocity += accel;
419  rot.normalize();
420  this->shiftDir(Quaternion(rotVal, rot));
421}
422
423
424void Helicopter::draw() const
425{
426  WorldEntity::draw();
427
428  this->getWeaponManager()->draw();
429}
430
431
432/**
433 * weapon manipulation by the player
434*/
435void Helicopter::weaponAction()
436{
437  if( this->bFire)
438    {
439      this->getWeaponManager()->fire();
440    }
441}
442
443/**
444 * @todo switch statement ??
445 */
446void Helicopter::process(const Event &event)
447{
448
449
450  if( event.type == SDLK_a)
451      this->bLeft = event.bPressed;
452  else if( event.type == SDLK_d)
453      this->bRight = event.bPressed;
454  else if( event.type == KeyMapper::PEV_FIRE1)
455      this->bFire = event.bPressed;
456  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
457    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
458  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
459    this->getWeaponManager()->previousWeaponConfig();
460  else if( event.type == SDLK_e)
461    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
462  else if( event.type == SDLK_c)
463    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
464  else if( event.type == SDLK_w)
465    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
466  else if( event.type == SDLK_s)
467    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
468  else if( event.type == EV_MOUSE_MOTION)
469  {
470    this->xMouse = event.xRel*mouseSensitivity;
471    this->yMouse = event.yRel*mouseSensitivity;
472   
473    //this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
474  }
475}
476
477#include "weapons/aiming_turret.h"
478// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
479void Helicopter::ADDWEAPON()
480{
481  Weapon* turret = NULL;
482
483  if ((float)rand()/RAND_MAX < .1)
484  {
485    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
486    {
487      turret = new Turret();
488      this->getWeaponManager()->addWeapon(turret, 2);
489      this->getWeaponManager()->changeWeaponConfig(2);
490    }
491  }
492  else
493  {
494    //if (this->getWeaponManager()->hasFreeSlot(3))
495    {
496      turret = new AimingTurret();
497      this->getWeaponManager()->addWeapon(turret, 3);
498
499      this->getWeaponManager()->changeWeaponConfig(3);
500    }
501  }
502
503  if(turret != NULL)
504  {
505    turret->setName("Turret");
506    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
507  }
508}
Note: See TracBrowser for help on using the repository browser.