Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/christmas_branche/src/world_entities/space_ships/helicopter.cc @ 6213

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

cristmas: crosshair hide and Helicopter added

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(KeyMapper::PEV_FIRE1);
140  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
141  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
142  registerEvent(SDLK_PAGEUP);
143  registerEvent(SDLK_PAGEDOWN);
144  registerEvent(EV_MOUSE_MOTION);
145
146  this->getWeaponManager()->setSlotCount(7);
147
148  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
149  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
150
151  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
152  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
153
154  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
155  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
156
157  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
158  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
159
160  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
161  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
162
163  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
164  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
165//
166   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
167   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168   //
169//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
170//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
171//
172//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
173//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
174
175}
176
177/**
178 * loads the Settings of a Helicopter from an XML-element.
179 * @param root the XML-element to load the Spaceship's properties from
180 */
181void Helicopter::loadParams(const TiXmlElement* root)
182{
183  static_cast<WorldEntity*>(this)->loadParams(root);
184}
185
186
187/**
188 * adds a weapon to the weapon list of the spaceship
189 * @param weapon to add
190*/
191void Helicopter::addWeapon(Weapon* weapon)
192{
193  this->getWeaponManager()->addWeapon(weapon);
194}
195
196
197/**
198 *  removes a weapon from the spaceship
199 * @param weapon to remove
200*/
201void Helicopter::removeWeapon(Weapon* weapon)
202{
203  this->getWeaponManager()->removeWeapon(weapon);
204}
205
206/**
207 *  effect that occurs after the Helicopter is spawned
208*/
209void Helicopter::postSpawn ()
210{
211  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
212}
213
214/**
215 *  the action occuring if the spaceship left the game
216*/
217void Helicopter::leftWorld ()
218{}
219
220/**
221 *  this function is called, when two entities collide
222 * @param entity: the world entity with whom it collides
223 *
224 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
225 */
226void Helicopter::collidesWith(WorldEntity* entity, const Vector& location)
227{
228  if (entity->isA(CL_TURRET_POWER_UP))
229  {
230    this->ADDWEAPON();
231    }
232//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
233}
234
235
236
237/**
238 *  the function called for each passing timeSnap
239 * @param time The timespan passed since last update
240*/
241void Helicopter::tick (float time)
242{
243  // spaceship controlled movement
244  this->calculateVelocity(time);
245
246  Vector move = (velocity)*time;
247
248  // this is the air friction (necessary for a smooth control)
249  if(velocity.len() != 0) velocity -= velocity*0.1;
250
251
252  //hoover effect
253  cycle += time;
254  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
255
256  //readjust
257 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
258  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
259
260  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
261
262  this->shiftCoor (move);
263
264  this->getWeaponManager()->tick(time);
265  // weapon system manipulation
266  this->weaponAction();
267}
268
269/**
270 *  calculate the velocity
271 * @param time the timeslice since the last frame
272*/
273void Helicopter::calculateVelocity (float time)
274{
275  Vector accel(0.0, 0.0, 0.0);
276  Vector rot(0.0, 0.0, 0.0);
277  float rotVal = 0.0;
278  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
279  /* calculate the direction in which the craft is heading  */
280
281  if( this->bUp )
282   {
283     //this->shiftCoor(this->getAbsDirX());
284
285     accel += Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2;
286     if((this->getAbsDirX()).y > -.2)
287     {
288       rot += Vector (0,0,1);
289       rotVal -= time/5;
290     }
291   }
292   else
293   {
294       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time/5, Vector(0,0,1)));
295   }
296
297  if( this->bDown )
298   {
299     //this->shiftCoor((this->getAbsDirX())*-1);
300     accel -= Vector(this->getAbsDirX().x,0,this->getAbsDirX().z).getNormalized()*2;
301     if((this->getAbsDirX()).y < .2)
302     {
303       rot += Vector (0,0,1);
304       rotVal += time/5;
305     }
306   }
307   else
308   {
309         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(-time/5, Vector(0,0,1)));
310   }
311
312  if( this->bLeft /* > -this->getRelCoor().z*2*/)
313  {
314    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
315    accel -= Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
316    //velocityDir.normalize();
317    if((this->getAbsDirZ()).y < .2)
318    {
319      rot +=Vector(1,0,0);
320      rotVal -= time/5;
321    }
322  }
323  else
324   {
325         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time/5, Vector(1,0,0)));
326   }
327
328  if( this->bRight /* > this->getRelCoor().z*2*/)
329  {
330    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
331
332    accel += Vector(this->getAbsDirZ().x,0,this->getAbsDirZ().z).getNormalized()*2;
333    //velocityDir.normalize();
334    if((this->getAbsDirZ()).y > -.2)
335    {
336      rot += Vector(1,0,0);
337      rotVal += time/5;
338    }
339  }
340  else
341   {
342         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time/5, Vector(1,0,0)));
343   }
344
345  if( this->bRollL /* > -this->getRelCoor().z*2*/)
346  {
347    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
348    //accel -= rightDirection;
349    //velocityDir.normalize();
350    //rot +=Vector(1,0,0);
351    //rotVal -= .4;
352  }
353  if( this->bRollR /* > this->getRelCoor().z*2*/)
354  {
355    this->shiftDir(Quaternion(time, Vector(1,0,0)));
356
357    //accel += rightDirection;
358    //velocityDir.normalize();
359    //rot += Vector(1,0,0);
360    //rotVal += .4;
361  }
362  if (this->bAscend )
363  {
364    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
365
366    accel += (this->getAbsDirY())*2;
367    //velocityDir.normalize();
368    //rot += Vector(0,0,1);
369    //rotVal += .4;
370  }
371  if (this->bDescend )
372  {
373    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
374
375    accel -= (this->getAbsDirY())*2;
376    //velocityDir.normalize();
377    //rot += Vector(0,0,1);
378    //rotVal -= .4;
379  }
380
381  velocity += accel;
382  rot.normalize();
383  this->shiftDir(Quaternion(rotVal, rot));
384}
385
386
387void Helicopter::draw() const
388{
389  this->drawLODsafe();
390
391  this->getWeaponManager()->draw();
392}
393
394
395/**
396 * weapon manipulation by the player
397*/
398void Helicopter::weaponAction()
399{
400  if( this->bFire)
401    {
402      this->getWeaponManager()->fire();
403    }
404}
405
406/**
407 * @todo switch statement ??
408 */
409void Helicopter::process(const Event &event)
410{
411
412
413  if( event.type == SDLK_a)
414      this->bLeft = event.bPressed;
415  else if( event.type == SDLK_d)
416      this->bRight = event.bPressed;
417  else if( event.type == KeyMapper::PEV_FIRE1)
418      this->bFire = event.bPressed;
419  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
420    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
421  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
422    this->getWeaponManager()->previousWeaponConfig();
423  else if( event.type == SDLK_e)
424    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
425  else if( event.type == SDLK_c)
426    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
427  else if( event.type == SDLK_w)
428    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
429  else if( event.type == SDLK_s)
430    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
431  else if( event.type == EV_MOUSE_MOTION)
432  {
433    this->xMouse = event.xRel;
434    this->yMouse = event.yRel;
435    if(((this->getAbsDirX().y) <= .2 && yMouse > 0) || ((this->getAbsDirX().y) >= -.2 && yMouse < 0)) yMouse = 0;
436    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
437  }
438}
439
440#include "weapons/aiming_turret.h"
441// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
442void Helicopter::ADDWEAPON()
443{
444  Weapon* turret = NULL;
445
446  if ((float)rand()/RAND_MAX < .1)
447  {
448    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
449    {
450      turret = new Turret();
451      this->getWeaponManager()->addWeapon(turret, 2);
452      this->getWeaponManager()->changeWeaponConfig(2);
453    }
454  }
455  else
456  {
457    //if (this->getWeaponManager()->hasFreeSlot(3))
458    {
459      turret = new AimingTurret();
460      this->getWeaponManager()->addWeapon(turret, 3);
461
462      this->getWeaponManager()->changeWeaponConfig(3);
463    }
464  }
465
466  if(turret != NULL)
467  {
468    turret->setName("Turret");
469    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
470  }
471}
Note: See TracBrowser for help on using the repository browser.