Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/world_entities/space_ships/turbine_hover.cc @ 9507

Last change on this file since 9507 was 9507, checked in by patrick, 18 years ago

the player team change should now work on all clients in the network, its now a global synch variable

File size: 18.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 "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  this->cameraNode.setRelCoor(0,5,0);
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 of the Playable
179  this->registerEvent(KeyMapper::PEV_FORWARD);
180  this->registerEvent(KeyMapper::PEV_BACKWARD);
181  this->registerEvent(KeyMapper::PEV_LEFT);
182  this->registerEvent(KeyMapper::PEV_RIGHT);
183  this->registerEvent(KeyMapper::PEV_UP);
184  this->registerEvent(KeyMapper::PEV_DOWN);
185  this->registerEvent(KeyMapper::PEV_FIRE1);
186  this->registerEvent(KeyMapper::PEV_NEXT_WEAPON);
187  this->registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
188  this->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  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
216  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
217  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
218  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
219  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
220  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
221  registerVar( new SynchronizeableQuaternion( &direction, &direction, "direction", PERMISSION_OWNER ) );
222  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
223  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
224}
225
226/**
227 * @brief loads the Settings of a TurbineHover from an XML-element.
228 * @param root the XML-element to load the Spaceship's properties from
229 */
230void TurbineHover::loadParams(const TiXmlElement* root)
231{
232  Playable::loadParams(root);
233}
234
235
236void TurbineHover::setBoostColor(const Color& color)
237{
238  this->burstSystem->setColor(0.0, color);
239  this->burstSystem->setColor(0.2, color * 0.6);
240  this->burstSystem->setColor(0.5, color * .3 + Color(0.5, 0.5, 0.8, 0.3));
241  this->burstSystem->setColor(1.0, 0.8, 0.8, 0.8, 0.0);
242
243  printf(":::::::::::::::::::::::\n");
244  this->burstSystem->debug();
245}
246
247void TurbineHover::setPlayDirection(const Quaternion& rot, float speed)
248{
249  this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
250}
251
252void TurbineHover::enter()
253{
254  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
255
256  if (State::getCameraNode != NULL)
257  {
258    State::getCameraNode()->setParentSoft(&this->cameraNode);
259    State::getCameraNode()->setRelCoorSoft(-10, 0,0);
260    State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
261  }
262}
263
264void TurbineHover::leave()
265{
266  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
267  this->detachCamera();
268
269}
270
271void TurbineHover::setTeam(int teamID)
272{
273  printf("::::::::::::::::: TEAM ::: %d\n", teamID);
274  if (teamID == 0)
275    this->setBoostColor(Color::blue);
276  else if (teamID == 1)
277    this->setBoostColor(Color::red);
278
279  // set the local team id, very important
280  this->setTeamId(teamID);
281}
282
283
284
285/**
286 * @brief effect that occurs after the TurbineHover is spawned
287*/
288void TurbineHover::postSpawn ()
289{
290  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
291}
292
293/**
294 * @brief the action occuring if the turbine_hover left the game
295*/
296void TurbineHover::leftWorld ()
297{}
298
299/**
300 * @brief this function is called, when two entities collide
301 * @param entity: the world entity with whom it collides
302 *
303 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
304 */
305void TurbineHover::collidesWith(WorldEntity* entity, const Vector& location)
306{
307  Playable::collidesWith(entity, location);
308}
309
310
311
312/**
313 * @brief the function called for each passing timeSnap
314 * @param time The timespan passed since last update
315*/
316void TurbineHover::tick (float dt)
317{
318  //  this->debugNode(1);
319  Playable::tick(dt);
320
321  // spaceship controlled movement
322
323  // TRYING TO FIX PNode.
324  //this->cameraNode.setRelCoor(Vector(0.0f, 5.0f, 0.0f));//, 30.0f);
325  //this->cameraNode.setRelDir(Quaternion(this->smoothRotator  *10.0, Vector(0,1,0)));
326
327  this->movement(dt);
328  this->rotorCycle += this->rotorSpeed * dt;
329}
330
331
332#include "shared_network_data.h"
333
334/**
335 * @brief calculate the velocity
336 * @param time the timeslice since the last frame
337*/
338void TurbineHover::movement (float dt)
339{
340  Vector accel(0.0, 0.0, 0.0);
341
342  if( this->bForward )
343  {
344    accel += Vector(this->acceleration, 0, 0);
345  }
346
347  if( this->bBackward )
348  {
349    accel -= Vector(this->acceleration, 0, 0);
350  }
351  if( this->bLeft)
352  {
353    accel -= Vector(0, 0, this->acceleration);
354  }
355
356  if( this->bRight)
357  {
358    accel += Vector(0, 0, this->acceleration);
359  }
360
361  if (this->bAscend )
362  {
363    accel += Vector(0, this->acceleration, 0);
364  }
365  if (this->bDescend )
366  {
367    accel -= Vector(0, this->acceleration, 0);
368  }
369
370  switch(this->getPlaymode())
371  {
372    case Playable::Full3D:
373      {
374        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
375
376        // this is the air friction (necessary for a smooth control)
377        Vector damping = (this->velocity * this->airFriction);
378
379
380        this->velocity += (accelerationDir - damping)* dt;
381        //if (this->getOwner() != SharedNetworkData::getInstance()->getHostID())
382          this->shiftCoor (this->velocity * dt);
383
384        // limit the maximum rotation speed.
385        if (this->rotation != 0.0f)
386        {
387          float maxRot = 10.0 * dt;
388          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
389          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
390          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
391
392          this->rotation = 0.0f;
393        }
394
395        //if (this->getOwner() != SharedNetworkData::getInstance()->getHostID())
396          this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
397
398        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
399        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
400
401        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
402        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
403      }
404      break;
405
406    case Playable::Horizontal:
407      {
408        accel.y = 0.0;
409        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
410        accelerationDir.y = 0.0;
411
412        // this is the air friction (necessary for a smooth control)
413        Vector damping = (this->velocity * this->airFriction);
414
415
416        this->velocity += (accelerationDir - damping)* dt;
417        this->shiftCoor (this->velocity * dt);
418
419        // limit the maximum rotation speed.
420        if (this->rotation != 0.0f)
421        {
422          float maxRot = 10.0 * dt;
423          if (unlikely(this->rotation > maxRot)) this->rotation = maxRot;
424          if (unlikely(this->rotation < -maxRot)) this->rotation = -maxRot;
425          this->direction *= Quaternion(-M_PI/4.0*this->rotation, Vector(0,1,0));
426
427          this->rotation = 0.0f;
428        }
429
430        this->setRelDirSoft(this->direction, 5);
431
432        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
433        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation, Vector(0,0,1)), 5);
434
435        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
436        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation, Vector(0,0,1)), 5);
437      }
438      break;
439
440    case Playable::Vertical:
441      {
442        accel.z = 0;
443        Vector accelerationDir = this->getAbsDir().apply(accel * this->acceleration);
444        accelerationDir.z=0;
445
446        // this is the air friction (necessary for a smooth control)
447        Vector damping = (this->velocity * this->airFriction);
448
449        this->velocity += (accelerationDir - damping)* dt;
450        this->shiftCoor (this->velocity * dt);
451
452        this->setRelDirSoft(this->direction * Quaternion(-cameraLook, Vector(0,0,1)), 5);
453
454        this->wingNodeLeft.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
455        this->rotorNodeLeft.setRelDirSoft(Quaternion(-accel.x * .07+this->rotation + cameraLook, Vector(0,0,1)), 5);
456
457        this->wingNodeRight.setRelDirSoft(Quaternion(accel.z * .05 +this->rotation, Vector(1,0,0)), 5);
458        this->rotorNodeRight.setRelDirSoft(Quaternion(-accel.x*.07 -this->rotation + cameraLook, Vector(0,0,1)), 5);
459      }
460      break;
461    default:
462      PRINTF(2)("Playmode %s Not Implemented\n", Playable::playmodeToString(this->getPlaymode()).c_str());
463  }
464}
465
466
467void TurbineHover::draw() const
468{
469  Vector tmpRot;
470  WorldEntity::draw();
471
472  glPushMatrix();
473  /// LEFT SIDE
474  glTranslatef (this->wingNodeLeft.getAbsCoor ().x,
475                this->wingNodeLeft.getAbsCoor ().y,
476                this->wingNodeLeft.getAbsCoor ().z);
477  tmpRot = this->wingNodeLeft.getAbsDir().getSpacialAxis();
478  glRotatef (this->wingNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
479  this->getModel(3)->draw();
480  glPopMatrix ();
481
482  glPushMatrix();
483  glTranslatef (this->rotorNodeLeft.getAbsCoor ().x,
484                this->rotorNodeLeft.getAbsCoor ().y,
485                this->rotorNodeLeft.getAbsCoor ().z);
486  tmpRot = this->rotorNodeLeft.getAbsDir().getSpacialAxis();
487  glRotatef (this->rotorNodeLeft.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
488  this->getModel(4)->draw();
489  glRotatef(this->rotorCycle, 0,1,0);
490  this->getModel(5)->draw();
491  glPopMatrix ();
492
493  /// RIGHT SIDE
494  glPushMatrix();
495  glTranslatef (this->wingNodeRight.getAbsCoor ().x,
496                this->wingNodeRight.getAbsCoor ().y,
497                this->wingNodeRight.getAbsCoor ().z);
498  tmpRot = this->wingNodeRight.getAbsDir().getSpacialAxis();
499  glRotatef (this->wingNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
500  glScalef(1,1,-1);
501  this->getModel(3)->draw();
502  glPopMatrix ();
503
504  glPushMatrix();
505  glTranslatef (this->rotorNodeRight.getAbsCoor ().x,
506                this->rotorNodeRight.getAbsCoor ().y,
507                this->rotorNodeRight.getAbsCoor ().z);
508  tmpRot = this->rotorNodeRight.getAbsDir().getSpacialAxis();
509  glRotatef (this->rotorNodeRight.getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z );
510  glScalef(1,1,-1);
511  this->getModel(4)->draw();
512  glRotatef(this->rotorCycle, 0,1,0);
513  this->getModel(5)->draw();
514  glPopMatrix ();
515}
516
517/**
518 * @todo switch statement ??
519 */
520void TurbineHover::process(const Event &event)
521{
522  Playable::process(event);
523
524  if( event.type == KeyMapper::PEV_LEFT)
525    this->bLeft = event.bPressed;
526  else if( event.type == KeyMapper::PEV_RIGHT)
527    this->bRight = event.bPressed;
528  else if( event.type == KeyMapper::PEV_UP)
529    this->bAscend = event.bPressed; //this->shiftCoor(0,.1,0);
530  else if( event.type == KeyMapper::PEV_DOWN)
531    this->bDescend = event.bPressed; //this->shiftCoor(0,-.1,0);
532  else if( event.type == KeyMapper::PEV_FORWARD)
533    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
534  else if( event.type == KeyMapper::PEV_BACKWARD)
535    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
536  else if( event.type == EV_MOUSE_MOTION)
537  {
538    float xMouse, yMouse;
539    xMouse = event.xRel*mouseSensitivity;
540    yMouse = event.yRel*mouseSensitivity;
541
542    // rotate the Player around the y-axis
543    this->rotation += xMouse;
544
545    this->cameraLook += yMouse;
546    // rotate the Camera around the z-axis
547    if (cameraLook > M_PI_4)
548      cameraLook = M_PI_4;
549    else if (cameraLook < -M_PI_4)
550      cameraLook = -M_PI_4;
551    //this->cameraNode.setRelDirSoft(this->direction,10);
552  }
553}
Note: See TracBrowser for help on using the repository browser.