Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7951 was 7868, checked in by bensch, 19 years ago

trunk: merge Check in the Event-changes:
r7867 | bensch | 2006-05-26 13:19:46 +0200 (Fri, 26 May 2006) | 1 line

Events better subscribed


r7866 | bensch | 2006-05-26 13:11:10 +0200 (Fri, 26 May 2006) | 1 line

Events are subscribed at the EventListener, and not the EventHandler

File size: 16.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
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  float rotSpeed = .3;
300
301  if( this->bForward )
302  {
303    accel += Vector(this->acceleration, 0, 0);
304  }
305
306  if( this->bBackward )
307  {
308    accel -= Vector(this->acceleration, 0, 0);
309  }
310  if( this->bLeft)
311  {
312    accel -= Vector(0, 0, this->acceleration);
313  }
314
315  if( this->bRight)
316  {
317    accel += Vector(0, 0, this->acceleration);
318  }
319
320  if (this->bAscend )
321  {
322    accel += Vector(0, this->acceleration, 0);
323  }
324  if (this->bDescend )
325  {
326    accel -= Vector(0, this->acceleration, 0);
327  }
328
329  switch(this->getPlaymode())
330  {
331    case Playable::Full3D:
332      {
333        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
334
335        // this is the air friction (necessary for a smooth control)
336        Vector damping = (this->velocity * this->airFriction);
337
338
339        this->velocity += (accelerationDir - damping)* dt;
340        this->shiftCoor (this->velocity * dt);
341
342        // limit the maximum rotation speed.
343        if (this->rotation != 0.0f)
344        {
345          float maxRot = 10.0 * dt;
346          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
347          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
348          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
349
350          this->rotation = 0.0f;
351        }
352
353        this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
354
355        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
356        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
357
358        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
359        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
360      }
361      break;
362
363    case Playable::Horizontal:
364      {
365        accel.y = 0.0;
366        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
367        accelerationDir.y = 0.0;
368
369        // this is the air friction (necessary for a smooth control)
370        Vector damping = (this->velocity * this->airFriction);
371
372
373        this->velocity += (accelerationDir - damping)* dt;
374        this->shiftCoor (this->velocity * dt);
375
376        // limit the maximum rotation speed.
377        if (this->rotation != 0.0f)
378        {
379          float maxRot = 10.0 * dt;
380          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
381          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
382          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
383
384          this->rotation = 0.0f;
385        }
386
387        this->setRelDirSoft(this->direction, 5);
388
389        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
390        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation, Vector(0,0,1)), 5);
391
392        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
393        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation, Vector(0,0,1)), 5);
394      }
395      break;
396
397    case Playable::Vertical:
398    {
399      accel.z = 0;
400      Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
401      accelerationDir.z=0;
402
403        // this is the air friction (necessary for a smooth control)
404      Vector damping = (this->velocity * this->airFriction);
405
406      this->velocity += (accelerationDir - damping)* dt;
407      this->shiftCoor (this->velocity * dt);
408
409      this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
410
411      this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
412      this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
413
414      this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
415      this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
416    }
417    break;
418  }
419}
420
421
422void TurbineHover::draw() const
423{
424  Vector tmpRot;
425  WorldEntity::draw();
426
427  glPushMatrix();
428  /// LEFT SIDE
429  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
430                this->wingNodeLeft.getAbsCoor ().y,
431                this->wingNodeLeft.getAbsCoor ().z);
432  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
433  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
434  this->getModel(3)->draw();
435  glPopMatrix ();
436
437  glPushMatrix();
438  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
439                this->rotorNodeLeft.getAbsCoor ().y,
440                this->rotorNodeLeft.getAbsCoor ().z);
441  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
442  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
443  this->getModel(4)->draw();
444  glRotatef(this->rotorCycle, 0,1,0);
445  this->getModel(5)->draw();
446  glPopMatrix ();
447
448  /// RIGHT SIDE
449  glPushMatrix();
450  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
451                this->wingNodeRight.getAbsCoor ().y,
452                this->wingNodeRight.getAbsCoor ().z);
453  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
454  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
455  glScalef(1,1,-1);
456  this->getModel(3)->draw();
457  glPopMatrix ();
458
459  glPushMatrix();
460  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
461                this->rotorNodeRight.getAbsCoor ().y,
462                this->rotorNodeRight.getAbsCoor ().z);
463  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
464  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
465  glScalef(1,1,-1);
466  this->getModel(4)->draw();
467  glRotatef(this->rotorCycle, 0,1,0);
468  this->getModel(5)->draw();
469  glPopMatrix ();
470}
471
472/**
473 * @todo switch statement ??
474 */
475void TurbineHover::process(const Event &event)
476{
477  Playable::process(event);
478
479  if( event.type == KeyMapper::PEV_LEFT)
480    this->bLeft = event.bPressed;
481  else if( event.type == KeyMapper::PEV_RIGHT)
482    this->bRight = event.bPressed;
483  else if( event.type == KeyMapper::PEV_UP)
484    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
485  else if( event.type == KeyMapper::PEV_DOWN)
486    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
487  else if( event.type == KeyMapper::PEV_FORWARD)
488    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
489  else if( event.type == KeyMapper::PEV_BACKWARD)
490    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
491  else if( event.type == EV_MOUSE_MOTION)
492  {
493    float xMouse, yMouse;
494    xMouse = event.xRel*mouseSensitivity;
495    yMouse = event.yRel*mouseSensitivity;
496
497    // rotate the Player around the y-axis
498    this->rotation += xMouse;
499
500    this->cameraLook += yMouse;
501    // rotate the Camera around the z-axis
502    if (cameraLook > M_PI_4)
503      cameraLook = M_PI_4;
504    else if (cameraLook < -M_PI_4)
505      cameraLook = -M_PI_4;
506    //this->cameraNode.setRelDirSoft(this->direction,10);
507  }
508}
Note: See TracBrowser for help on using the repository browser.