Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8359 was 8316, checked in by bensch, 19 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

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