Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/turbine_hover.cc @ 8538

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

orxonox/trunk: removed stupid included in base_object.h
this should lead to faster compile-times

File size: 16.2 KB
RevLine 
[6443]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
[7000]19#include "turbine_hover.h"
[6443]20
21#include "weapons/weapon_manager.h"
22#include "weapons/test_gun.h"
23#include "weapons/turret.h"
24#include "weapons/cannon.h"
25
[7193]26#include "util/loading/factory.h"
[6443]27#include "key_mapper.h"
28#include "state.h"
29
30#include "graphics_engine.h"
[7001]31#include "dot_emitter.h"
32#include "sprite_particles.h"
[6443]33
[8362]34#include "debug.h"
35
[7000]36CREATE_FACTORY(TurbineHover, CL_TURBINE_HOVER);
[6443]37
38/**
[7000]39 *  destructs the turbine_hover, deletes alocated memory
[6443]40 */
[7000]41TurbineHover::~TurbineHover ()
[6986]42{
43  this->setPlayer(NULL);
44}
[6443]45
46/**
[7339]47 * @brief loads a TurbineHover information from a specified file.
[7000]48 * @param fileName the name of the File to load the turbine_hover from (absolute path)
[6443]49 */
[7221]50TurbineHover::TurbineHover(const std::string& fileName)
[6443]51{
52  this->init();
53  TiXmlDocument doc(fileName);
54
55  if(!doc.LoadFile())
56  {
[7221]57    PRINTF(2)("Loading file %s failed for TurbineHover.\n", fileName.c_str());
[6443]58    return;
59  }
60
61  this->loadParams(doc.RootElement());
62}
63
64/**
[7339]65 * @brief creates a new Spaceship from Xml Data
[6443]66 * @param root the xml element containing spaceship data
67
68   @todo add more parameters to load
69*/
[7000]70TurbineHover::TurbineHover(const TiXmlElement* root)
[6443]71{
72  this->init();
73  if (root != NULL)
74    this->loadParams(root);
75
76  //weapons:
77  Weapon* wpRight = new TestGun(0);
78  wpRight->setName("testGun Right");
79  Weapon* wpLeft = new TestGun(1);
80  wpLeft->setName("testGun Left");
[7126]81  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER));
[6443]82
[7126]83  //  cannon->setName("BFG");
[6443]84
85  this->addWeapon(wpLeft, 1, 0);
86  this->addWeapon(wpRight,1 ,1);
[7126]87  //this->addWeapon(cannon, 0, 2);
[6443]88
[7337]89  this->getWeaponManager().changeWeaponConfig(1);
90  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[6800]91
92  this->loadModel("models/ships/hoverglider_mainbody.obj");
[6443]93}
94
95
96/**
[7339]97 * @brief initializes a TurbineHover
[6443]98 */
[7000]99void TurbineHover::init()
[6443]100{
[6805]101  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
[7000]102  this->setClassID(CL_TURBINE_HOVER, "TurbineHover");
[6443]103
[7346]104  this->setSupportedPlaymodes(Playable::Full3D | Playable::Horizontal | Playable::Vertical);
[7339]105
[6807]106  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
[7000]107  this->loadModel("models/ships/hoverglider_turbine.obj", 1.0f, 4);
108  this->loadModel("models/ships/hoverglider_turbine_rotors.obj", 1.0f, 5);
[6443]109
[6805]110  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
[6807]111  mouseSensitivity = 0.005;
[6443]112
[6806]113  this->rotorSpeed = 1000.0f;
114  this->rotorCycle = 0.0f;
[6807]115  this->cameraLook = 0.0f;
116  this->rotation = 0.0f;
[7001]117  this->acceleration = 10.0f;
118  this->airFriction = 2.0f;
[6799]119
[7072]120  this->setHealthMax(100);
121  this->setHealth(100);
122
123
[6806]124  // camera - issue
[6800]125  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6806]126  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6880]127  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
[7003]128  //this->cameraNode.setParent(this);
[6799]129
[6724]130  // rotors
[6803]131  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
132  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6801]133  this->wingNodeLeft.setParent(this);
134  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
[6800]135  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6801]136  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
137  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
[6799]138
[6801]139  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6803]140  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
[6800]141  this->wingNodeRight.setParent(this);
[6801]142  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
143  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[6800]144  this->rotorNodeRight.setParent(&this->wingNodeRight);
[6801]145  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
[6443]146
[7001]147  // PARTICLES
148  this->burstEmitter[0] = new DotEmitter(200, 5.0, .01);
149  this->burstEmitter[0]->setParent(&this->rotorNodeLeft);
150  this->burstEmitter[0]->setRelCoor(0, -0.7, 0);
151  this->burstEmitter[0]->setRelDir(Quaternion(-M_PI_2, Vector(0,0,1)));
152  this->burstEmitter[0]->setName("TurbineHover_Burst_emitter_Left");
[6803]153
[7001]154  this->burstEmitter[1] = new DotEmitter(200, 5.0, .01);
155  this->burstEmitter[1]->setParent(&this->rotorNodeRight);
156  this->burstEmitter[1]->setRelCoor(0, -0.7, 0);
157  this->burstEmitter[1]->setRelDir(Quaternion(-M_PI_2, Vector(0,0,1)));
158  this->burstEmitter[1]->setName("TurbineHover_Burst_emitter_Right");
[6800]159
[7001]160
161  this->burstSystem = new SpriteParticles(1000);
162  this->burstSystem->addEmitter(this->burstEmitter[0]);
163  this->burstSystem->addEmitter(this->burstEmitter[1]);
164  this->burstSystem->setName("SpaceShip_Burst_System");
165  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
166  this->burstSystem->setLifeSpan(1.0, .3);
167  this->burstSystem->setRadius(0.0, 1.5);
168  this->burstSystem->setRadius(0.05, 1.8);
169  this->burstSystem->setRadius(.5, .8);
170  this->burstSystem->setRadius(1.0, 0);
171  this->burstSystem->setColor(0.0, .7,.7,1,.5);
172  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
173  this->burstSystem->setColor(0.5, .5,.5,.8,.3);
174  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
175
176
[7868]177  //add events to the eventlist of the Playable
178  this->registerEvent(KeyMapper::PEV_FORWARD);
179  this->registerEvent(KeyMapper::PEV_BACKWARD);
180  this->registerEvent(KeyMapper::PEV_LEFT);
181  this->registerEvent(KeyMapper::PEV_RIGHT);
182  this->registerEvent(KeyMapper::PEV_UP);
183  this->registerEvent(KeyMapper::PEV_DOWN);
184  this->registerEvent(KeyMapper::PEV_FIRE1);
185  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
186  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
187  this->registerEvent(EV_MOUSE_MOTION);
[6443]188
[7337]189  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[6443]190
[6803]191  // WEAPON_MANAGER configuration
[7337]192  this->getWeaponManager().setSlotCount(5);
[6803]193
[7337]194  this->getWeaponManager().setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
195  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6443]196
[7337]197  this->getWeaponManager().setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
198  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6443]199
[7337]200  this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003));
201  this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY);
[6443]202
[6803]203  /// TODO: THESE ARE TOO MUCH
[7337]204  this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652));
205  this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
[6443]206
[7337]207  this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652));
208  this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
[6443]209
[6807]210  this->cameraNode.setRelCoor(1,5,0);
[7337]211  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
212  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
[6443]213}
214
215/**
[7345]216 * @brief loads the Settings of a TurbineHover from an XML-element.
[6443]217 * @param root the XML-element to load the Spaceship's properties from
218 */
[7000]219void TurbineHover::loadParams(const TiXmlElement* root)
[6443]220{
[7348]221  Playable::loadParams(root);
[6443]222}
223
[7348]224void TurbineHover::setPlayDirection(const Quaternion& rot, float speed)
225{
226  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
227}
[6443]228
[7000]229void TurbineHover::enter()
[6443]230{
[7337]231  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
[6443]232
[7125]233  if (State::getCameraNode != NULL)
234  {
235    State::getCameraNode()->setParentSoft(&this->cameraNode);
236    State::getCameraNode()->setRelCoorSoft(-10, 0,0);
237    State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
238  }
[6443]239}
240
[7000]241void TurbineHover::leave()
[6443]242{
[7337]243  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
[6443]244  this->detachCamera();
245
246}
247
248
249/**
[7345]250 * @brief effect that occurs after the TurbineHover is spawned
[6443]251*/
[7000]252void TurbineHover::postSpawn ()
[6443]253{
254  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
255}
256
257/**
[7345]258 * @brief the action occuring if the turbine_hover left the game
[6443]259*/
[7000]260void TurbineHover::leftWorld ()
[6443]261{}
262
263/**
[7345]264 * @brief this function is called, when two entities collide
[6443]265 * @param entity: the world entity with whom it collides
266 *
267 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
268 */
[7000]269void TurbineHover::collidesWith(WorldEntity* entity, const Vector& location)
[7072]270{
271  Playable::collidesWith(entity, location);
272}
[6443]273
274
275
276/**
[7345]277 * @brief the function called for each passing timeSnap
[6443]278 * @param time The timespan passed since last update
279*/
[7000]280void TurbineHover::tick (float dt)
[6443]281{
[7339]282  //  this->debugNode(1);
[6804]283  Playable::tick(dt);
284
[6443]285  // spaceship controlled movement
[6805]286  this->movement(dt);
[6806]287  this->rotorCycle += this->rotorSpeed * dt;
[7001]288
289  // TRYING TO FIX PNode.
[7003]290  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
291  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
[6443]292}
293
294/**
[7345]295 * @brief calculate the velocity
[6443]296 * @param time the timeslice since the last frame
297*/
[7000]298void TurbineHover::movement (float dt)
[6443]299{
300  Vector accel(0.0, 0.0, 0.0);
301
[6814]302  if( this->bForward )
303  {
[6879]304    accel += Vector(this->acceleration, 0, 0);
[6443]305  }
306
[6814]307  if( this->bBackward )
308  {
[6879]309    accel -= Vector(this->acceleration, 0, 0);
[6443]310  }
[6814]311  if( this->bLeft)
312  {
[6879]313    accel -= Vector(0, 0, this->acceleration);
[6443]314  }
[6807]315
[6814]316  if( this->bRight)
317  {
[6879]318    accel += Vector(0, 0, this->acceleration);
[6443]319  }
320
[6814]321  if (this->bAscend )
322  {
[6879]323    accel += Vector(0, this->acceleration, 0);
[6443]324  }
[6814]325  if (this->bDescend )
326  {
[6879]327    accel -= Vector(0, this->acceleration, 0);
[6807]328  }
[6443]329
[7339]330  switch(this->getPlaymode())
331  {
332    case Playable::Full3D:
333      {
334        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
[6814]335
[7339]336        // this is the air friction (necessary for a smooth control)
337        Vector damping = (this->velocity * this->airFriction);
[6999]338
339
[7339]340        this->velocity += (accelerationDir - damping)* dt;
341        this->shiftCoor (this->velocity * dt);
[6879]342
[7339]343        // limit the maximum rotation speed.
344        if (this->rotation != 0.0f)
345        {
346          float maxRot = 10.0 * dt;
347          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
348          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
349          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
[7326]350
[7339]351          this->rotation = 0.0f;
352        }
[7326]353
[7339]354        this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
[6805]355
[7339]356        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
357        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
[6805]358
[7339]359        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
360        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
361      }
362      break;
363
364    case Playable::Horizontal:
365      {
[7345]366        accel.y = 0.0;
[7339]367        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
[7345]368        accelerationDir.y = 0.0;
[7339]369
370        // this is the air friction (necessary for a smooth control)
371        Vector damping = (this->velocity * this->airFriction);
372
373
374        this->velocity += (accelerationDir - damping)* dt;
375        this->shiftCoor (this->velocity * dt);
376
377        // limit the maximum rotation speed.
378        if (this->rotation != 0.0f)
379        {
380          float maxRot = 10.0 * dt;
381          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
382          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
383          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
384
385          this->rotation = 0.0f;
386        }
387
388        this->setRelDirSoft(this->direction, 5);
389
390        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
391        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation, Vector(0,0,1)), 5);
392
393        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
394        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation, Vector(0,0,1)), 5);
395      }
396      break;
[7346]397
398    case Playable::Vertical:
399    {
400      accel.z = 0;
401      Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
402      accelerationDir.z=0;
403
404        // this is the air friction (necessary for a smooth control)
405      Vector damping = (this->velocity * this->airFriction);
406
407      this->velocity += (accelerationDir - damping)* dt;
408      this->shiftCoor (this->velocity * dt);
409
410      this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
411
412      this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
413      this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
414
415      this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
416      this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
417    }
418    break;
[8316]419    default:
420      PRINTF(2)("Playmode %s Not Implemented\n", Playable::playmodeToString(this->getPlaymode()).c_str());
[7339]421  }
[6443]422}
423
424
[7000]425void TurbineHover::draw() const
[6443]426{
[6801]427  Vector tmpRot;
[6443]428  WorldEntity::draw();
[6799]429
[6801]430  glPushMatrix();
431  /// LEFT SIDE
432  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
433                this->wingNodeLeft.getAbsCoor ().y,
434                this->wingNodeLeft.getAbsCoor ().z);
435  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
436  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
437  this->getModel(3)->draw();
438  glPopMatrix ();
[6443]439
[6801]440  glPushMatrix();
441  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
442                this->rotorNodeLeft.getAbsCoor ().y,
443                this->rotorNodeLeft.getAbsCoor ().z);
444  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
445  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
446  this->getModel(4)->draw();
[6806]447  glRotatef(this->rotorCycle, 0,1,0);
448  this->getModel(5)->draw();
[6801]449  glPopMatrix ();
[6724]450
[6801]451  /// RIGHT SIDE
452  glPushMatrix();
453  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
454                this->wingNodeRight.getAbsCoor ().y,
455                this->wingNodeRight.getAbsCoor ().z);
456  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
457  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
458  glScalef(1,1,-1);
459  this->getModel(3)->draw();
460  glPopMatrix ();
[6724]461
[6801]462  glPushMatrix();
463  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
464                this->rotorNodeRight.getAbsCoor ().y,
465                this->rotorNodeRight.getAbsCoor ().z);
466  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
467  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
468  glScalef(1,1,-1);
469  this->getModel(4)->draw();
[6806]470  glRotatef(this->rotorCycle, 0,1,0);
471  this->getModel(5)->draw();
[6801]472  glPopMatrix ();
[6443]473}
474
475/**
476 * @todo switch statement ??
477 */
[7000]478void TurbineHover::process(const Event &event)
[6443]479{
[6804]480  Playable::process(event);
[6443]481
[6637]482  if( event.type == KeyMapper::PEV_LEFT)
[6805]483    this->bLeft = event.bPressed;
[6637]484  else if( event.type == KeyMapper::PEV_RIGHT)
[6805]485    this->bRight = event.bPressed;
[6998]486  else if( event.type == KeyMapper::PEV_UP)
[6443]487    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
[6998]488  else if( event.type == KeyMapper::PEV_DOWN)
[6443]489    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
[6997]490  else if( event.type == KeyMapper::PEV_FORWARD)
[6805]491    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6997]492  else if( event.type == KeyMapper::PEV_BACKWARD)
[6805]493    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[6443]494  else if( event.type == EV_MOUSE_MOTION)
495  {
[6807]496    float xMouse, yMouse;
497    xMouse = event.xRel*mouseSensitivity;
498    yMouse = event.yRel*mouseSensitivity;
[6443]499
[6805]500    // rotate the Player around the y-axis
[6807]501    this->rotation += xMouse;
[6799]502
[6807]503    this->cameraLook += yMouse;
[6805]504    // rotate the Camera around the z-axis
[6807]505    if (cameraLook > M_PI_4)
506      cameraLook = M_PI_4;
[6880]507    else if (cameraLook < -M_PI_4)
508      cameraLook = -M_PI_4;
509    //this->cameraNode.setRelDirSoft(this->direction,10);
[6805]510  }
[6443]511}
Note: See TracBrowser for help on using the repository browser.