Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6167 was 6162, checked in by bensch, 19 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
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
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     }
290   }
291   else
292   {
293       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/5, Vector(0,0,1)));
294   }
295
296  if( this->bDown )
297   {
298     //this->shiftCoor((this->getAbsDirX())*-1);
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     }
305   }
306   else
307   {
308         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(-time/5, Vector(0,0,1)));
309   }
310
311  if( this->bLeft /* > -this->getRelCoor().z*2*/)
312  {
313    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
314    accel -= Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
315    //velocityDir.normalize();
316    if((this->getAbsDirZ()).y < .2)
317    {
318      rot +=Vector(1,0,0);
319      rotVal -= time/5;
320    }
321  }
322  else
323   {
324         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0)));
325   }
326   
327  if( this->bRight /* > this->getRelCoor().z*2*/)
328  {
329    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
330
331    accel += Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
332    //velocityDir.normalize();
333    if((this->getAbsDirZ()).y > -.2)
334    {
335      rot += Vector(1,0,0);
336      rotVal += time/5;
337    }
338  }
339  else
340   {
341         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0)));
342   }
343
344  if( this->bRollL /* > -this->getRelCoor().z*2*/)
345  {
346    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
347    //accel -= rightDirection;
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
356    //accel += rightDirection;
357    //velocityDir.normalize();
358    //rot += Vector(1,0,0);
359    //rotVal += .4;
360  }
361  if (this->bAscend )
362  {
363    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
364
365    accel += (this->getAbsDirY())*2;
366    //velocityDir.normalize();
367    //rot += Vector(0,0,1);
368    //rotVal += .4;
369  }
370  if (this->bDescend )
371  {
372    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
373
374    accel -= (this->getAbsDirY())*2;
375    //velocityDir.normalize();
376    //rot += Vector(0,0,1);
377    //rotVal -= .4;
378  }
379
380  velocity += accel;
381  rot.normalize();
382  this->shiftDir(Quaternion(rotVal, rot));
383}
384
385
386void Helicopter::draw() const
387{
388  this->drawLODsafe();
389
390  this->getWeaponManager()->draw();
391}
392
393
394/**
395 * weapon manipulation by the player
396*/
397void Helicopter::weaponAction()
398{
399  if( this->bFire)
400    {
401      this->getWeaponManager()->fire();
402    }
403}
404
405/**
406 * @todo switch statement ??
407 */
408void Helicopter::process(const Event &event)
409{
410
411
412  if( event.type == SDLK_a)
413      this->bLeft = event.bPressed;
414  else if( event.type == SDLK_d)
415      this->bRight = event.bPressed;
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();
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);
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;
434    if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0;
435    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
436  }
437}
438
439#include "weapons/aiming_turret.h"
440// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
441void Helicopter::ADDWEAPON()
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.