Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

first try of a helicopter control

File size: 11.8 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  //orient the velocity in the direction of the spaceship.
248  travelSpeed = velocity.len();
249  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
250  velocity = (velocity.getNormalized())*travelSpeed;
251
252  // this is the air friction (necessary for a smooth control)
253  if(velocity.len() != 0) velocity -= velocity*0.01;
254
255  //hoover effect
256  cycle += time;
257  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
258
259  //readjust
260 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
261  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
262
263  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
264
265  this->shiftCoor (move);
266
267  this->getWeaponManager()->tick(time);
268  // weapon system manipulation
269  this->weaponAction();
270}
271
272/**
273 *  calculate the velocity
274 * @param time the timeslice since the last frame
275*/
276void Helicopter::calculateVelocity (float time)
277{
278  Vector accel(0.0, 0.0, 0.0);
279  Vector rot(0.0, 0.0, 0.0);
280  float rotVal = 0.0;
281  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
282  /* calculate the direction in which the craft is heading  */
283
284  if( this->bUp )
285   {
286     //this->shiftCoor(this->getAbsDirX());
287      accel += (this->getAbsDirX())*2;
288      rot += Vector (0,0,1);
289      rotVal -= .4;
290   }
291
292  if( this->bDown )
293   {
294     //this->shiftCoor((this->getAbsDirX())*-1);
295     accel -= (this->getAbsDirX())*2;
296     rot += Vector (0,0,1);
297     rotVal += .4;
298   }
299
300  if( this->bLeft /* > -this->getRelCoor().z*2*/)
301  {
302    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
303    accel -= (this->getAbsDirZ());
304    //velocityDir.normalize();
305    rot +=Vector(1,0,0);
306    rotVal -= .2;
307  }
308  if( this->bRight /* > this->getRelCoor().z*2*/)
309  {
310    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
311
312    accel += (this->getAbsDirZ());
313    //velocityDir.normalize();
314    rot += Vector(1,0,0);
315    rotVal += .2;
316  }
317
318  if( this->bRollL /* > -this->getRelCoor().z*2*/)
319  {
320    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
321    //accel -= rightDirection;
322    //velocityDir.normalize();
323    //rot +=Vector(1,0,0);
324    //rotVal -= .4;
325  }
326  if( this->bRollR /* > this->getRelCoor().z*2*/)
327  {
328    this->shiftDir(Quaternion(time, Vector(1,0,0)));
329
330    //accel += rightDirection;
331    //velocityDir.normalize();
332    //rot += Vector(1,0,0);
333    //rotVal += .4;
334  }
335  if (this->bAscend )
336  {
337    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
338
339    accel += (this->getAbsDirY())*2;
340    //velocityDir.normalize();
341    //rot += Vector(0,0,1);
342    //rotVal += .4;
343  }
344  if (this->bDescend )
345  {
346    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
347
348    accel -= (this->getAbsDirY())*2;
349    //velocityDir.normalize();
350    //rot += Vector(0,0,1);
351    //rotVal -= .4;
352  }
353
354  velocity += accel;
355  rot.normalize();
356  this->setRelDirSoft(Quaternion(rotVal, rot), 5);
357}
358
359
360void Helicopter::draw() const
361{
362  this->drawLODsafe();
363
364  this->getWeaponManager()->draw();
365}
366
367
368/**
369 * weapon manipulation by the player
370*/
371void Helicopter::weaponAction()
372{
373  if( this->bFire)
374    {
375      this->getWeaponManager()->fire();
376    }
377}
378
379/**
380 * @todo switch statement ??
381 */
382void Helicopter::process(const Event &event)
383{
384
385
386  if( event.type == SDLK_a)
387      this->bLeft = event.bPressed;
388  else if( event.type == SDLK_d)
389      this->bRight = event.bPressed;
390  else if( event.type == KeyMapper::PEV_FIRE1)
391      this->bFire = event.bPressed;
392  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
393    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
394  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
395    this->getWeaponManager()->previousWeaponConfig();
396  else if( event.type == SDLK_e)
397    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
398  else if( event.type == SDLK_c)
399    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
400  else if( event.type == SDLK_w)
401    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
402  else if( event.type == SDLK_s)
403    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
404  else if( event.type == EV_MOUSE_MOTION)
405  {
406    this->xMouse = event.xRel;
407    this->yMouse = event.yRel;
408    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
409  }
410}
411
412#include "weapons/aiming_turret.h"
413// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
414void Helicopter::ADDWEAPON()
415{
416  Weapon* turret = NULL;
417
418  if ((float)rand()/RAND_MAX < .1)
419  {
420    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
421    {
422      turret = new Turret();
423      this->getWeaponManager()->addWeapon(turret, 2);
424      this->getWeaponManager()->changeWeaponConfig(2);
425    }
426  }
427  else
428  {
429    //if (this->getWeaponManager()->hasFreeSlot(3))
430    {
431      turret = new AimingTurret();
432      this->getWeaponManager()->addWeapon(turret, 3);
433
434      this->getWeaponManager()->changeWeaponConfig(3);
435    }
436  }
437
438  if(turret != NULL)
439  {
440    turret->setName("Turret");
441    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
442  }
443}
Note: See TracBrowser for help on using the repository browser.