Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/hover.cc @ 6801

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

trunk: hover positioned

File size: 15.4 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 "hover.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(Hover, CL_HOVER);
36
37/**
38 *  creates the controlable Hover
39 */
40Hover::Hover()
41{
42  this->init();
43}
44
45/**
46 *  destructs the hover, deletes alocated memory
47 */
48Hover::~Hover ()
49{
50}
51
52/**
53 * loads a Hover information from a specified file.
54 * @param fileName the name of the File to load the hover from (absolute path)
55 */
56Hover::Hover(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 Hover.\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*/
76Hover::Hover(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->addWeapon(wpLeft, 1, 0);
92  this->addWeapon(wpRight,1 ,1);
93  this->addWeapon(cannon, 0, 6);
94
95  //this->addWeapon(turret, 3, 0);
96
97  this->getWeaponManager()->changeWeaponConfig(1);
98  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
99
100  this->loadModel("models/ships/hoverglider_mainbody.obj");
101}
102
103
104/**
105 * initializes a Hover
106 */
107void Hover::init()
108{
109//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
110  this->setClassID(CL_HOVER, "Hover");
111
112  PRINTF(4)("HOVER INIT\n");
113
114  this->loadModel("models/ships/hover_#.obj", 1.0);
115
116  EventHandler::getInstance()->grabEvents(true);
117
118  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
119  bFire = false;
120  xMouse = yMouse = 0;
121  mouseSensitivity = 0.05;
122  controlVelocityX = 100;
123  controlVelocityY = 100;
124  //rotorspeed = 1;
125  //tailrotorspeed = 0;
126
127  //cycle = 0.0;
128
129  // cameraissue
130  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
131  this->cameraNode.setParent(this);
132  this->cameraNode.setParentMode(PNODE_ALL);
133
134
135  // rotors
136  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
137  this->wingNodeLeft.setParent(this);
138  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
139  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
140  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
141  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
142
143  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
144  this->wingNodeRight.setParent(this);
145  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
146  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
147  this->rotorNodeRight.setParent(&this->wingNodeRight);
148  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
149
150  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
151  this->loadModel("models/ships/hoverglider_rotor.obj", 1.0f, 4);
152
153
154  //travelSpeed = 15.0;
155  this->velocity = Vector(0.0,0.0,0.0);
156  this->velocityDir = Vector(1.0,0.0,0.0);
157//   GLGuiButton* button = new GLGuiPushButton();
158//   button->show();
159//   button->setLabel("orxonox");
160//   button->setBindNode(this);
161
162  //add events to the eventlist
163  registerEvent(KeyMapper::PEV_UP);
164  registerEvent(KeyMapper::PEV_DOWN);
165  registerEvent(KeyMapper::PEV_LEFT);
166  registerEvent(KeyMapper::PEV_RIGHT);
167  //registerEvent(SDLK_q);
168  registerEvent(SDLK_e);
169  registerEvent(SDLK_c);
170  registerEvent(KeyMapper::PEV_FIRE1);
171  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
172  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
173  //registerEvent(SDLK_PAGEUP);
174  //registerEvent(SDLK_PAGEDOWN);
175  registerEvent(EV_MOUSE_MOTION);
176
177  this->getWeaponManager()->setSlotCount(7);
178
179  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
180  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
181
182  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
183  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
184
185  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
186  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
187
188  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
189  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
190
191  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
192  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
193
194  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
195  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
196//
197   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
198   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
199   //
200//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
201//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
202//
203//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
204//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
205
206  this->getWeaponManager()->getFixedTarget()->setParent(&(this->cameraNode));
207  this->getWeaponManager()->getFixedTarget()->setRelCoor(0,0,0);
208
209  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
210
211}
212
213/**
214 * loads the Settings of a Hover from an XML-element.
215 * @param root the XML-element to load the Spaceship's properties from
216 */
217void Hover::loadParams(const TiXmlElement* root)
218{
219  WorldEntity::loadParams(root);
220}
221
222void  Hover::attachCamera()
223{
224  State::getCamera()->setParentSoft(this->getWeaponManager()->getFixedTarget());
225  State::getCameraTarget()->setParentSoft(this->getWeaponManager()->getFixedTarget());
226
227}
228
229
230void Hover::enter()
231{
232  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
233  this->attachCamera();
234
235
236}
237
238void Hover::leave()
239{
240  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
241  this->detachCamera();
242
243}
244
245
246/**
247 *  effect that occurs after the Hover is spawned
248*/
249void Hover::postSpawn ()
250{
251  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
252}
253
254/**
255 *  the action occuring if the hover left the game
256*/
257void Hover::leftWorld ()
258{}
259
260/**
261 *  this function is called, when two entities collide
262 * @param entity: the world entity with whom it collides
263 *
264 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
265 */
266void Hover::collidesWith(WorldEntity* entity, const Vector& location)
267{
268
269}
270
271
272
273/**
274 *  the function called for each passing timeSnap
275 * @param time The timespan passed since last update
276*/
277void Hover::tick (float time)
278{
279  if( xMouse != 0 || yMouse != 0)
280   {
281    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
282    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
283    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
284    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
285  }
286
287  // rotorrotation
288//   this->topRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
289//   this->tailRotor.shiftDir(Quaternion(time*10, Vector(0,1,0)));
290
291  // spaceship controlled movement
292  this->calculateVelocity(time);
293
294  Vector move = (velocity)*time;
295
296  // this is the air friction (necessary for a smooth control)
297  if(velocity.len() != 0) velocity -= velocity*0.1;
298
299  //travelSpeed = velocity.len();
300
301  //physics: Gravity
302  /*this->shiftCoor(Vector(0,-1,0));
303
304  this->shiftCoor(getAbsDirY()*rotorspeed);
305  */
306
307  /*
308  //hoover effect
309  cycle += time;
310  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
311  */
312
313  //readjust
314 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
315  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
316
317  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
318
319  this->shiftCoor (move);
320  //this->shiftDir(Quaternion(-M_PI/4*tailrotorspeed, Vector(0,1,0)));
321
322  this->getWeaponManager()->tick(time);
323  // weapon system manipulation
324  this->weaponAction();
325}
326
327/**
328 *  calculate the velocity
329 * @param time the timeslice since the last frame
330*/
331void Hover::calculateVelocity (float time)
332{
333  Vector accel(0.0, 0.0, 0.0);
334  float rotValX = 0.0;
335  float rotValZ = 0.0;
336  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
337  /* calculate the direction in which the craft is heading  */
338
339  if( this->bUp )
340   {
341     //this->shiftCoor(this->getAbsDirX());
342     //accel -= this->getAbsDirY();
343
344     accel += Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
345     if((this->getAbsDirX()).y >= -0.1) rotValZ -= time;
346   }
347   else
348   {
349       if(this->getAbsDirX().y < -.02) this->shiftDir(Quaternion(time, Vector(0,0,1)));
350   }
351
352  if( this->bDown )
353   {
354     //this->shiftCoor((this->getAbsDirX())*-1);
355     //accel -= this->getAbsDirY();
356
357     accel -= Vector((this->getAbsDirX()).x,0,(this->getAbsDirX()).z);
358     rotValZ += time;
359   }
360   else
361   {
362         if(this->getAbsDirX().y > 0.02) this->shiftDir(Quaternion(-time, Vector(0,0,1)));
363   }
364
365  if( this->bLeft /* > -this->getRelCoor().z*2*/)
366  {
367    //this->shiftDir(Quaternion(time, Vector(0,1,0)));
368    //accel -= this->getAbsDirY();
369    //velocityDir.normalize();
370
371    accel -= Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
372    rotValX -= time;
373  }
374  else
375   {
376         if(this->getAbsDirZ().y > 0.02) this->shiftDir(Quaternion(time, Vector(1,0,0)));
377   }
378
379  if( this->bRight /* > this->getRelCoor().z*2*/)
380  {
381    //this->shiftDir(Quaternion(-time, Vector(0,1,0)));
382    //accel += this->getAbsDirY();
383    //velocityDir.normalize();
384
385    accel += Vector((this->getAbsDirZ()).x,0,(this->getAbsDirZ()).z);
386    rotValX += time;
387  }
388  else
389   {
390         if(this->getAbsDirZ().y < -0.02) this->shiftDir(Quaternion(-time, Vector(1,0,0)));
391   }
392
393  if( this->bRollL /* > -this->getRelCoor().z*2*/)
394  {
395    this->shiftDir(Quaternion(-time, Vector(1,0,0)));
396    //accel -= rightDirection;
397    //velocityDir.normalize();
398    //rot +=Vector(1,0,0);
399    //rotVal -= .4;
400  }
401  if( this->bRollR /* > this->getRelCoor().z*2*/)
402  {
403    this->shiftDir(Quaternion(time, Vector(1,0,0)));
404
405    //accel += rightDirection;
406    //velocityDir.normalize();
407    //rot += Vector(1,0,0);
408    //rotVal += .4;
409  }
410  if (this->bAscend )
411  {
412    //this->shiftDir(Quaternion(time, Vector(0,0,1)));
413
414    accel += this->getAbsDirY();
415    //rotorspeed += 0.05;
416    //if (rotorspeed >= 2) rotorspeed = 2;
417    //velocityDir.normalize();
418    //rot += Vector(0,0,1);
419    //rotVal += .4;
420  }
421  else
422  {
423    //if(rotorspeed >= 1.05) rotorspeed -= 0.05;
424  }
425
426  if (this->bDescend )
427  {
428    //this->shiftDir(Quaternion(-time, Vector(0,0,1)));
429
430    accel -= this->getAbsDirY();
431    //rotorspeed -= 0.05;
432    //if (rotorspeed <= 0) rotorspeed = 0;
433    //velocityDir.normalize();
434    //rot += Vector(0,0,1);
435    //rotVal -= .4;
436  }
437  else
438  {
439    //if(rotorspeed <= 0.05) rotorspeed += 0.05;
440  }
441
442  velocity += accel*3;
443  if((this->getAbsDirX()).y <= 0.3 && (this->getAbsDirX()).y >= -0.3) this->shiftDir(Quaternion(rotValZ, Vector(0,0,1)));
444  if((this->getAbsDirZ()).y <= 0.3 && (this->getAbsDirZ()).y >= -0.3) this->shiftDir(Quaternion(rotValX, Vector(1,0,0)));
445}
446
447
448void Hover::draw() const
449{
450  Vector tmpRot;
451
452  WorldEntity::draw();
453
454  glPushMatrix();
455  /// LEFT SIDE
456  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
457                this->wingNodeLeft.getAbsCoor ().y,
458                this->wingNodeLeft.getAbsCoor ().z);
459  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
460  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
461  this->getModel(3)->draw();
462  glPopMatrix ();
463
464  glPushMatrix();
465  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
466                this->rotorNodeLeft.getAbsCoor ().y,
467                this->rotorNodeLeft.getAbsCoor ().z);
468  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
469  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
470  this->getModel(4)->draw();
471  glPopMatrix ();
472
473  /// RIGHT SIDE
474  glPushMatrix();
475  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
476                this->wingNodeRight.getAbsCoor ().y,
477                this->wingNodeRight.getAbsCoor ().z);
478  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
479  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
480  glScalef(1,1,-1);
481  this->getModel(3)->draw();
482  glPopMatrix ();
483
484  glPushMatrix();
485  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
486                this->rotorNodeRight.getAbsCoor ().y,
487                this->rotorNodeRight.getAbsCoor ().z);
488  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
489  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
490  glScalef(1,1,-1);
491  this->getModel(4)->draw();
492  glPopMatrix ();
493
494  this->getWeaponManager()->draw();
495}
496
497
498/**
499 * weapon manipulation by the player
500*/
501void Hover::weaponAction()
502{
503  if( this->bFire)
504    {
505      this->getWeaponManager()->fire();
506    }
507}
508
509/**
510 * @todo switch statement ??
511 */
512void Hover::process(const Event &event)
513{
514
515
516  if( event.type == KeyMapper::PEV_LEFT)
517      this->bLeft = event.bPressed;
518  else if( event.type == KeyMapper::PEV_RIGHT)
519      this->bRight = event.bPressed;
520  else if( event.type == KeyMapper::PEV_FIRE1)
521      this->bFire = event.bPressed;
522  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
523    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
524  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
525    this->getWeaponManager()->previousWeaponConfig();
526  else if( event.type == SDLK_e)
527    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
528  else if( event.type == SDLK_c)
529    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
530  else if( event.type == KeyMapper::PEV_UP)
531    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
532  else if( event.type == KeyMapper::PEV_DOWN)
533    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
534  else if( event.type == EV_MOUSE_MOTION)
535  {
536    this->xMouse = event.xRel*mouseSensitivity;
537    this->yMouse = event.yRel*mouseSensitivity;
538
539    this->shiftDir(Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0)));
540
541    Quaternion yDir = Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1));
542
543
544    if ((this->cameraNode.getAbsDirY()).y < 0.5)
545    {
546     if((this->cameraNode.getAbsDirX()).y > 0)
547     {
548        if(yMouse > 0) this->cameraNode.shiftDir(yDir);
549     }
550     else
551     {
552         if(yMouse < 0) this->cameraNode.shiftDir(yDir);
553     }
554    }
555    else this->cameraNode.shiftDir(yDir);;
556    }
557}
Note: See TracBrowser for help on using the repository browser.