Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/spaceshipcontrol/src/world_entities/space_ships/space_ship.cc @ 6544

Last change on this file since 6544 was 6544, checked in by snellen, 18 years ago

space_ship.cc updated

File size: 15.3 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 Knecht
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
22#include "objModel.h"
23#include "resource_manager.h"
24
25#include "weapons/weapon_manager.h"
26#include "weapons/test_gun.h"
27#include "weapons/turret.h"
28#include "weapons/cannon.h"
29
30#include "factory.h"
31#include "key_mapper.h"
32#include "event_handler.h"
33
34#include "network_game_manager.h"
35
36#include "power_ups/weapon_power_up.h"
37#include "power_ups/param_power_up.h"
38
39#include "graphics_engine.h"
40
41
42
43// #include "lib/gui/gl_gui/glgui_bar.h"
44// #include "lib/gui/gl_gui/glgui_pushbutton.h"
45
46
47using namespace std;
48
49CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
50
51/**
52 *  creates the controlable Spaceship
53 */
54SpaceShip::SpaceShip()
55{
56  this->init();
57}
58
59/**
60 *  destructs the spaceship, deletes alocated memory
61 */
62SpaceShip::~SpaceShip ()
63{
64}
65
66/**
67 * loads a Spaceships information from a specified file.
68 * @param fileName the name of the File to load the spaceship from (absolute path)
69 */
70SpaceShip::SpaceShip(const char* fileName)
71{
72  this->init();
73  TiXmlDocument doc(fileName);
74
75  if(!doc.LoadFile())
76  {
77    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName);
78    return;
79  }
80
81  this->loadParams(doc.RootElement());
82}
83
84/**
85 *  creates a new Spaceship from Xml Data
86 * @param root the xml element containing spaceship data
87
88   @todo add more parameters to load
89*/
90SpaceShip::SpaceShip(const TiXmlElement* root)
91{
92  this->init();
93  if (root != NULL)
94    this->loadParams(root);
95
96  //weapons:
97  Weapon* wpRight = new TestGun(0);
98  wpRight->setName("testGun Right");
99  Weapon* wpLeft = new TestGun(1);
100  wpLeft->setName("testGun Left");
101  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
102
103  cannon->setName("BFG");
104
105  this->addWeapon(wpLeft, 1, 0);
106  this->addWeapon(wpRight,1 ,1);
107  this->addWeapon(cannon, 0, 6);
108
109  //this->addWeapon(turret, 3, 0);
110
111  this->getWeaponManager()->changeWeaponConfig(1);
112}
113
114
115/**
116 * initializes a Spaceship
117 */
118void SpaceShip::init()
119{
120//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
121  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
122
123  PRINTF(4)("SPACESHIP INIT\n");
124
125  EventHandler::getInstance()->grabEvents(true);
126
127  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
128  bFire = false;
129  xMouse = yMouse = 0;
130  mouseSensitivity = 0.001;
131  airViscosity = 0.95;
132  controlVelocityX = 5;
133  controlVelocityY = 150;
134  shipInertia = 3.0             ;
135//  cycle = 0.0;
136
137  this->setMaxEnergy(100);
138  this->setEnergy(80);
139
140  travelSpeed = 40.0;
141  acceleration = 3;
142  this->velocity = this->getAbsDirX()*travelSpeed;
143  this->mouseDir = this->getAbsDir();
144  this->pitchDir = this->getAbsDir();
145
146//   GLGuiButton* button = new GLGuiPushButton();
147//    button->show();
148//    button->setLabel("orxonox");
149//    button->setBindNode(this);
150//     GLGuiBar* bar = new GLGuiBar();
151//     bar->show();
152//     bar->setValue(7.0);
153//     bar->setMaximum(10);
154//     bar->setSize2D( 20, 100);
155//     bar->setAbsCoor2D( 10, 200);
156
157  //add events to the eventlist
158  registerEvent(KeyMapper::PEV_UP);
159  registerEvent(KeyMapper::PEV_DOWN);
160  registerEvent(KeyMapper::PEV_LEFT);
161  registerEvent(KeyMapper::PEV_RIGHT);
162  //registerEvent(SDLK_q);
163  //registerEvent(SDLK_e);
164  registerEvent(KeyMapper::PEV_FIRE1);
165  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
166  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
167  //registerEvent(SDLK_PAGEUP);
168  //registerEvent(SDLK_PAGEDOWN);
169  registerEvent(EV_MOUSE_MOTION);
170
171  this->getWeaponManager()->setSlotCount(7);
172
173  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
174  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
175
176  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
177  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
178
179  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
180  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
181
182  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
183  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
184
185  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
186  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
187
188  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
189  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
190//
191   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
192   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
193   //
194//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
195//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
196//
197//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
198//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
199
200  this->getWeaponManager()->getFixedTarget()->setParent(this);
201  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
202
203  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
204}
205
206/**
207 * loads the Settings of a SpaceShip from an XML-element.
208 * @param root the XML-element to load the Spaceship's properties from
209 */
210void SpaceShip::loadParams(const TiXmlElement* root)
211{
212  static_cast<WorldEntity*>(this)->loadParams(root);
213}
214
215
216void SpaceShip::enter()
217{
218  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
219  this->attachCamera();
220}
221
222void SpaceShip::leave()
223{
224  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
225  this->detachCamera();
226}
227
228
229/**
230 *  effect that occurs after the SpaceShip is spawned
231*/
232void SpaceShip::postSpawn ()
233{
234  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
235}
236
237/**
238 *  the action occuring if the spaceship left the game
239*/
240void SpaceShip::leftWorld ()
241{}
242
243WorldEntity* ref = NULL;
244/**
245 *  this function is called, when two entities collide
246 * @param entity: the world entity with whom it collides
247 *
248 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
249 */
250void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
251{
252  Playable::collidesWith(entity, location);
253  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
254  {
255    this->ADDWEAPON();
256    ref = entity;
257    }
258//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
259}
260
261/**
262 *  draws the spaceship after transforming it.
263*/
264void SpaceShip::draw () const
265{
266  WorldEntity::draw();
267  this->getWeaponManager()->draw();
268
269  //this->debug(0);
270}
271
272/**
273 *  the function called for each passing timeSnap
274 * @param time The timespan passed since last update
275*/
276void SpaceShip::tick (float time)
277{
278
279  // spaceship controlled movement
280  this->calculateVelocity(time);
281
282  Vector move = velocity*time;
283 
284  //orient the velocity in the direction of the spaceship.
285  travelSpeed = velocity.len();
286  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
287  velocity = (velocity.getNormalized())*travelSpeed;
288
289  //orient the spaceship in direction of the mouse
290   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*shipInertia);
291   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
292    this->setAbsDir( rotQuat);
293   //this->setAbsDirSoft(mouseDir,5);
294
295  // this is the air friction (necessary for a smooth control)
296  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
297  else velocity -= velocity.getNormalized()*travelSpeed*0.001;
298 
299  //other physics (gravity)
300  if((this->getAbsDirY()).y*travelSpeed < 120 && (this->getAbsDirY()).y>0)
301  move += Vector(0,-1,0)*60*time + Vector(0,1,0)*(this->getAbsDirY()).y*travelSpeed/2*time;
302  if((this->getAbsDirY()).y*travelSpeed < 120 && (this->getAbsDirY()).y<0)
303  move += Vector(0,-1,0)*60*time - Vector(0,1,0)*(this->getAbsDirY()).y*travelSpeed/2*time;
304   
305  //hoover effect
306  //cycle += time;
307  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
308
309  //readjust
310  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
311  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
312
313  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
314
315  this->shiftCoor(move);
316
317  this->getWeaponManager()->tick(time);
318  // weapon system manipulation
319  this->weaponAction();
320}
321
322/**
323 *  calculate the velocity
324 * @param time the timeslice since the last frame
325*/
326void SpaceShip::calculateVelocity (float time)
327{
328  Vector accel(0.0, 0.0, 0.0);
329  /*
330  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
331  */
332  //float rotVal = 0.0;
333  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
334  /* calculate the direction in which the craft is heading  */
335
336  //Plane plane(Vector(0,1,0), Vector(0,0,0));
337
338  if( this->bUp )
339   {
340     //this->shiftCoor(this->getAbsDirX());
341      //accel += (this->getAbsDirX())*2;
342     
343      accel += (this->getAbsDirX())*acceleration;
344
345   }
346
347  if( this->bDown )
348   {
349     //this->shiftCoor((this->getAbsDirX())*-1);
350     //accel -= (this->getAbsDirX())*2;
351     accel -= (this->getAbsDirX())*0.5*acceleration;
352   }
353
354  if( this->bLeft/* > -this->getRelCoor().z*2*/)
355  {
356    this->shiftDir(Quaternion(time, Vector(0,1,0)));
357//    accel -= rightDirection;
358    //velocityDir.normalize();
359    //rot +=Vector(1,0,0);
360    //rotVal -= .4;
361  }
362  if( this->bRight /* > this->getRelCoor().z*2*/)
363  {
364    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
365
366    //    accel += rightDirection;
367    //velocityDir.normalize();
368    //rot += Vector(1,0,0);
369    //rotVal += .4;
370  }
371
372
373  if( this->bRollL /* > -this->getRelCoor().z*2*/)
374  {
375    mouseDir *= Quaternion(-time, Vector(1,0,0));
376//    accel -= rightDirection;
377    //velocityDir.normalize();
378    //rot +=Vector(1,0,0);
379    //rotVal -= .4;
380  }
381  if( this->bRollR /* > this->getRelCoor().z*2*/)
382  {
383    mouseDir *= Quaternion(time, Vector(1,0,0));
384
385    //    accel += rightDirection;
386    //velocityDir.normalize();
387    //rot += Vector(1,0,0);
388    //rotVal += .4;
389  }
390  if (this->bAscend )
391  {
392    this->shiftDir(Quaternion(time, Vector(0,0,1)));
393
394//    accel += upDirection;
395    //velocityDir.normalize();
396    //rot += Vector(0,0,1);
397    //rotVal += .4;
398  }
399  if (this->bDescend )
400  {
401    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
402
403    //    accel -= upDirection;
404    //velocityDir.normalize();
405    //rot += Vector(0,0,1);
406    //rotVal -= .4;
407  }
408
409  velocity += accel;
410  //rot.normalize();
411  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
412}
413
414/**
415 * weapon manipulation by the player
416*/
417void SpaceShip::weaponAction()
418{
419  if( this->bFire)
420    {
421      this->getWeaponManager()->fire();
422    }
423}
424
425/**
426 * @todo switch statement ??
427 */
428void SpaceShip::process(const Event &event)
429{
430
431
432  if( event.type == KeyMapper::PEV_LEFT)
433      this->bRollL = event.bPressed;
434  else if( event.type == KeyMapper::PEV_RIGHT)
435      this->bRollR = event.bPressed;
436  else if( event.type == KeyMapper::PEV_FIRE1)
437      this->bFire = event.bPressed;
438  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
439  {
440    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
441  }
442  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
443    this->previousWeaponConfig();
444  else if( event.type == KeyMapper::PEV_UP)
445    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
446  else if( event.type == KeyMapper::PEV_DOWN)
447    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
448  else if( event.type == EV_MOUSE_MOTION)
449  {
450    this->xMouse = event.xRel;
451    this->yMouse = event.yRel;
452   
453   
454   
455    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
456    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
457    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
458    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
459   
460    //pitchDir *= (Quaternion(xMouse*mouseSensitivity*0.01, Vector(1,0,0)));
461   
462    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1))*pitchDir);
463   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
464     //this->setAbsDir(mouseDir);
465  }
466}
467
468/**
469 *
470 */
471bool SpaceShip::pickup(PowerUp* powerUp)
472{
473  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
474    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
475    WeaponManager* manager = this->getWeaponManager();
476    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
477    if(slot >= 0) {
478      manager->addWeapon(weapon, 0, slot);
479      return true;
480    }
481  }
482  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
483    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
484    switch(ppu->getType()) {
485      case PARAM_SHIELD:
486        break;
487    }
488  }
489  return false;
490}
491
492#include "weapons/aiming_turret.h"
493// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
494void SpaceShip::ADDWEAPON()
495{
496  Weapon* turret = NULL;
497
498  if ((float)rand()/RAND_MAX < .9)
499  {
500    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
501    {
502      turret = new Turret();
503      this->addWeapon(turret, 2);
504      this->getWeaponManager()->changeWeaponConfig(2);
505    }
506  }
507  else
508  {
509    //if (this->getWeaponManager()->hasFreeSlot(3))
510    {
511      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
512      if (turret != NULL)
513      this->addWeapon(turret, 3);
514
515      this->getWeaponManager()->changeWeaponConfig(3);
516    }
517  }
518
519  if(turret != NULL)
520  {
521    turret->setName("Turret");
522    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
523  }
524}
525
526
527int SpaceShip::writeBytes( const byte * data, int length, int sender )
528{
529  setRequestedSync( false );
530  setIsOutOfSync( false );
531
532  SYNCHELP_READ_BEGIN();
533
534  SYNCHELP_READ_FKT( WorldEntity::writeState );
535
536  return SYNCHELP_READ_N;
537}
538
539int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
540{
541  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
542  {
543    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
544    setRequestedSync( true );
545  }
546
547  int rec = this->getRequestSync();
548  if ( rec > 0 )
549  {
550    *reciever = rec;
551
552    SYNCHELP_WRITE_BEGIN();
553
554    SYNCHELP_WRITE_FKT( WorldEntity::readState );
555
556    return SYNCHELP_WRITE_N;
557  }
558
559  *reciever = 0;
560  return 0;
561}
Note: See TracBrowser for help on using the repository browser.