Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/turbine_hover.cc @ 9230

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

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8230:HEAD https://svn.orxonox.net/orxonox/branches/network .
conflicts resolved in favour of the network branche (conflicts were in network)

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