Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some nice fixes

File size: 15.0 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 "event_handler.h"
29#include "state.h"
30
31#include "graphics_engine.h"
32#include "dot_emitter.h"
33#include "sprite_particles.h"
34
35using namespace std;
36
37CREATE_FACTORY(TurbineHover, CL_TURBINE_HOVER);
38
39/**
40 *  destructs the turbine_hover, deletes alocated memory
41 */
42TurbineHover::~TurbineHover ()
43{
44  this->setPlayer(NULL);
45}
46
47/**
48 * @brief loads a TurbineHover information from a specified file.
49 * @param fileName the name of the File to load the turbine_hover from (absolute path)
50 */
51TurbineHover::TurbineHover(const std::string& fileName)
52{
53  this->init();
54  TiXmlDocument doc(fileName);
55
56  if(!doc.LoadFile())
57  {
58    PRINTF(2)("Loading file %s failed for TurbineHover.\n", fileName.c_str());
59    return;
60  }
61
62  this->loadParams(doc.RootElement());
63}
64
65/**
66 * @brief creates a new Spaceship from Xml Data
67 * @param root the xml element containing spaceship data
68
69   @todo add more parameters to load
70*/
71TurbineHover::TurbineHover(const TiXmlElement* root)
72{
73  this->init();
74  if (root != NULL)
75    this->loadParams(root);
76
77  //weapons:
78  Weapon* wpRight = new TestGun(0);
79  wpRight->setName("testGun Right");
80  Weapon* wpLeft = new TestGun(1);
81  wpLeft->setName("testGun Left");
82  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_HYPERBLASTER));
83
84  //  cannon->setName("BFG");
85
86  this->addWeapon(wpLeft, 1, 0);
87  this->addWeapon(wpRight,1 ,1);
88  //this->addWeapon(cannon, 0, 2);
89
90  this->getWeaponManager().changeWeaponConfig(1);
91  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
92
93  this->loadModel("models/ships/hoverglider_mainbody.obj");
94}
95
96
97/**
98 * @brief initializes a TurbineHover
99 */
100void TurbineHover::init()
101{
102  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
103  this->setClassID(CL_TURBINE_HOVER, "TurbineHover");
104
105  this->setSupportedPlaymodes(Playable::Full3D | Playable::Horizontal);
106
107  this->loadModel("models/ships/hoverglider_wing.obj", 1.0f, 3);
108  this->loadModel("models/ships/hoverglider_turbine.obj", 1.0f, 4);
109  this->loadModel("models/ships/hoverglider_turbine_rotors.obj", 1.0f, 5);
110
111  bForward = bBackward = bLeft = bRight = bAscend = bDescend = false;
112  mouseSensitivity = 0.005;
113
114  this->rotorSpeed = 1000.0f;
115  this->rotorCycle = 0.0f;
116  this->cameraLook = 0.0f;
117  this->rotation = 0.0f;
118  this->acceleration = 10.0f;
119  this->airFriction = 2.0f;
120
121  this->setHealthMax(100);
122  this->setHealth(100);
123
124
125  // camera - issue
126  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
127  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
128  //this->cameraNode.setParentMode(PNODE_ROTATE_MOVEMENT);
129  //this->cameraNode.setParent(this);
130
131  // rotors
132  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT );
133  this->wingNodeLeft.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
134  this->wingNodeLeft.setParent(this);
135  this->wingNodeLeft.setRelCoor(-1.5, -.3, -1.0);
136  this->rotorNodeLeft.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
137  this->rotorNodeLeft.setParent(&this->wingNodeLeft);
138  this->rotorNodeLeft.setRelCoor(0, 1.0, -2.3);
139
140  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
141  this->wingNodeRight.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
142  this->wingNodeRight.setParent(this);
143  this->wingNodeRight.setRelCoor(-1.5, -0.3, 1.0);
144  this->rotorNodeRight.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
145  this->rotorNodeRight.setParent(&this->wingNodeRight);
146  this->rotorNodeRight.setRelCoor(0, 1.0, 2.3);
147
148  // PARTICLES
149  this->burstEmitter[0] = new DotEmitter(200, 5.0, .01);
150  this->burstEmitter[0]->setParent(&this->rotorNodeLeft);
151  this->burstEmitter[0]->setRelCoor(0, -0.7, 0);
152  this->burstEmitter[0]->setRelDir(Quaternion(-M_PI_2, Vector(0,0,1)));
153  this->burstEmitter[0]->setName("TurbineHover_Burst_emitter_Left");
154
155  this->burstEmitter[1] = new DotEmitter(200, 5.0, .01);
156  this->burstEmitter[1]->setParent(&this->rotorNodeRight);
157  this->burstEmitter[1]->setRelCoor(0, -0.7, 0);
158  this->burstEmitter[1]->setRelDir(Quaternion(-M_PI_2, Vector(0,0,1)));
159  this->burstEmitter[1]->setName("TurbineHover_Burst_emitter_Right");
160
161
162  this->burstSystem = new SpriteParticles(1000);
163  this->burstSystem->addEmitter(this->burstEmitter[0]);
164  this->burstSystem->addEmitter(this->burstEmitter[1]);
165  this->burstSystem->setName("SpaceShip_Burst_System");
166  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
167  this->burstSystem->setLifeSpan(1.0, .3);
168  this->burstSystem->setRadius(0.0, 1.5);
169  this->burstSystem->setRadius(0.05, 1.8);
170  this->burstSystem->setRadius(.5, .8);
171  this->burstSystem->setRadius(1.0, 0);
172  this->burstSystem->setColor(0.0, .7,.7,1,.5);
173  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
174  this->burstSystem->setColor(0.5, .5,.5,.8,.3);
175  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
176
177
178  //add events to the eventlist
179  registerEvent(KeyMapper::PEV_FORWARD);
180  registerEvent(KeyMapper::PEV_BACKWARD);
181  registerEvent(KeyMapper::PEV_LEFT);
182  registerEvent(KeyMapper::PEV_RIGHT);
183  registerEvent(KeyMapper::PEV_UP);
184  registerEvent(KeyMapper::PEV_DOWN);
185  registerEvent(KeyMapper::PEV_FIRE1);
186  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
187  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
188  registerEvent(EV_MOUSE_MOTION);
189
190  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
191
192  // WEAPON_MANAGER configuration
193  this->getWeaponManager().setSlotCount(5);
194
195  this->getWeaponManager().setSlotPosition(0, Vector(-0.28, 1.186, -2.750), &this->wingNodeLeft);
196  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
197
198  this->getWeaponManager().setSlotPosition(1, Vector(-0.28, 1.186, 2.750), &this->wingNodeRight);
199  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
200
201  this->getWeaponManager().setSlotPosition(2, Vector(-1.63, .809, -.003));
202  this->getWeaponManager().setSlotCapability(2, WTYPE_HEAVY);
203
204  /// TODO: THESE ARE TOO MUCH
205  this->getWeaponManager().setSlotPosition(3, Vector(-1.63, .678, -.652));
206  this->getWeaponManager().setSlotDirection(3, Quaternion(-24/180 * M_PI, Vector(1,0,0)));
207
208  this->getWeaponManager().setSlotPosition(4, Vector(-1.63, .678, .652));
209  this->getWeaponManager().setSlotDirection(4, Quaternion(24/180 * M_PI, Vector(1,0,0)));
210
211  this->cameraNode.setRelCoor(1,5,0);
212  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
213  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
214}
215
216/**
217 * loads the Settings of a TurbineHover from an XML-element.
218 * @param root the XML-element to load the Spaceship's properties from
219 */
220void TurbineHover::loadParams(const TiXmlElement* root)
221{
222  WorldEntity::loadParams(root);
223}
224
225
226void TurbineHover::enter()
227{
228  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
229
230  if (State::getCameraNode != NULL)
231  {
232    State::getCameraNode()->setParentSoft(&this->cameraNode);
233    State::getCameraNode()->setRelCoorSoft(-10, 0,0);
234    State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
235  }
236}
237
238void TurbineHover::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 TurbineHover is spawned
248*/
249void TurbineHover::postSpawn ()
250{
251  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
252}
253
254/**
255 *  the action occuring if the turbine_hover left the game
256*/
257void TurbineHover::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 TurbineHover::collidesWith(WorldEntity* entity, const Vector& location)
267{
268  Playable::collidesWith(entity, location);
269}
270
271
272
273/**
274 *  the function called for each passing timeSnap
275 * @param time The timespan passed since last update
276*/
277void TurbineHover::tick (float dt)
278{
279  //  this->debugNode(1);
280  Playable::tick(dt);
281
282  // spaceship controlled movement
283  this->movement(dt);
284  this->rotorCycle += this->rotorSpeed * dt;
285
286  // TRYING TO FIX PNode.
287  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
288  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
289}
290
291/**
292 *  calculate the velocity
293 * @param time the timeslice since the last frame
294*/
295void TurbineHover::movement (float dt)
296{
297  Vector accel(0.0, 0.0, 0.0);
298  float rotSpeed = .3;
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.z = 0.0;
365        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
366        accelerationDir.z = 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}
397
398
399void TurbineHover::draw() const
400{
401  Vector tmpRot;
402  WorldEntity::draw();
403
404  glPushMatrix();
405  /// LEFT SIDE
406  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
407                this->wingNodeLeft.getAbsCoor ().y,
408                this->wingNodeLeft.getAbsCoor ().z);
409  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
410  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
411  this->getModel(3)->draw();
412  glPopMatrix ();
413
414  glPushMatrix();
415  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
416                this->rotorNodeLeft.getAbsCoor ().y,
417                this->rotorNodeLeft.getAbsCoor ().z);
418  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
419  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
420  this->getModel(4)->draw();
421  glRotatef(this->rotorCycle, 0,1,0);
422  this->getModel(5)->draw();
423  glPopMatrix ();
424
425  /// RIGHT SIDE
426  glPushMatrix();
427  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
428                this->wingNodeRight.getAbsCoor ().y,
429                this->wingNodeRight.getAbsCoor ().z);
430  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
431  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
432  glScalef(1,1,-1);
433  this->getModel(3)->draw();
434  glPopMatrix ();
435
436  glPushMatrix();
437  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
438                this->rotorNodeRight.getAbsCoor ().y,
439                this->rotorNodeRight.getAbsCoor ().z);
440  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
441  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
442  glScalef(1,1,-1);
443  this->getModel(4)->draw();
444  glRotatef(this->rotorCycle, 0,1,0);
445  this->getModel(5)->draw();
446  glPopMatrix ();
447}
448
449/**
450 * @todo switch statement ??
451 */
452void TurbineHover::process(const Event &event)
453{
454  Playable::process(event);
455
456  if( event.type == KeyMapper::PEV_LEFT)
457    this->bLeft = event.bPressed;
458  else if( event.type == KeyMapper::PEV_RIGHT)
459    this->bRight = event.bPressed;
460  else if( event.type == KeyMapper::PEV_UP)
461    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
462  else if( event.type == KeyMapper::PEV_DOWN)
463    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
464  else if( event.type == KeyMapper::PEV_FORWARD)
465    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
466  else if( event.type == KeyMapper::PEV_BACKWARD)
467    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
468  else if( event.type == EV_MOUSE_MOTION)
469  {
470    float xMouse, yMouse;
471    xMouse = event.xRel*mouseSensitivity;
472    yMouse = event.yRel*mouseSensitivity;
473
474    // rotate the Player around the y-axis
475    this->rotation += xMouse;
476
477    this->cameraLook += yMouse;
478    // rotate the Camera around the z-axis
479    if (cameraLook > M_PI_4)
480      cameraLook = M_PI_4;
481    else if (cameraLook < -M_PI_4)
482      cameraLook = -M_PI_4;
483    //this->cameraNode.setRelDirSoft(this->direction,10);
484  }
485}
Note: See TracBrowser for help on using the repository browser.