Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6506 was 6506, checked in by bknecht, 18 years ago

Control: Some keys for SpaceShip and Helicopter can now be altered in orxonox.conf

File size: 14.4 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 = 1.0;
132  cycle = 0.0;
133
134  this->setMaxEnergy(100);
135  this->setEnergy(80);
136
137  travelSpeed = 15.0;
138  acceleration = 5.0;
139  this->velocity = this->getAbsDirX()*travelSpeed;
140  this->mouseDir = this->getAbsDir();
141
142//   GLGuiButton* button = new GLGuiPushButton();
143//    button->show();
144//    button->setLabel("orxonox");
145//    button->setBindNode(this);
146//     GLGuiBar* bar = new GLGuiBar();
147//     bar->show();
148//     bar->setValue(7.0);
149//     bar->setMaximum(10);
150//     bar->setSize2D( 20, 100);
151//     bar->setAbsCoor2D( 10, 200);
152
153  //add events to the eventlist
154  registerEvent(KeyMapper::PEV_UP);
155  registerEvent(KeyMapper::PEV_DOWN);
156  registerEvent(KeyMapper::PEV_LEFT);
157  registerEvent(KeyMapper::PEV_RIGHT);
158  //registerEvent(SDLK_q);
159  //registerEvent(SDLK_e);
160  registerEvent(KeyMapper::PEV_FIRE1);
161  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
162  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
163  //registerEvent(SDLK_PAGEUP);
164  //registerEvent(SDLK_PAGEDOWN);
165  registerEvent(EV_MOUSE_MOTION);
166
167  this->getWeaponManager()->setSlotCount(7);
168
169  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
170  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
171
172  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
173  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
174
175  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
176  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
177
178  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
179  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
180
181  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
182  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
183
184  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
185  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
186//
187   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
188   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
189   //
190//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
191//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
192//
193//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
194//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
195
196  this->getWeaponManager()->getFixedTarget()->setParent(this);
197  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
198
199  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
200}
201
202/**
203 * loads the Settings of a SpaceShip from an XML-element.
204 * @param root the XML-element to load the Spaceship's properties from
205 */
206void SpaceShip::loadParams(const TiXmlElement* root)
207{
208  static_cast<WorldEntity*>(this)->loadParams(root);
209}
210
211
212void SpaceShip::enter()
213{
214  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
215  this->attachCamera();
216}
217
218void SpaceShip::leave()
219{
220  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
221  this->detachCamera();
222}
223
224
225/**
226 *  effect that occurs after the SpaceShip is spawned
227*/
228void SpaceShip::postSpawn ()
229{
230  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
231}
232
233/**
234 *  the action occuring if the spaceship left the game
235*/
236void SpaceShip::leftWorld ()
237{}
238
239WorldEntity* ref = NULL;
240/**
241 *  this function is called, when two entities collide
242 * @param entity: the world entity with whom it collides
243 *
244 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
245 */
246void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
247{
248  Playable::collidesWith(entity, location);
249  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
250  {
251    this->ADDWEAPON();
252    ref = entity;
253    }
254//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
255}
256
257/**
258 *  draws the spaceship after transforming it.
259*/
260void SpaceShip::draw () const
261{
262  WorldEntity::draw();
263  this->getWeaponManager()->draw();
264
265  //this->debug(0);
266}
267
268/**
269 *  the function called for each passing timeSnap
270 * @param time The timespan passed since last update
271*/
272void SpaceShip::tick (float time)
273{
274
275  // spaceship controlled movement
276  this->calculateVelocity(time);
277
278  Vector move = velocity.getNormalized()*travelSpeed;
279
280  //orient the velocity in the direction of the spaceship.
281  travelSpeed = velocity.len();
282  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
283  velocity = (velocity.getNormalized())*travelSpeed;
284
285  //orient the spaceship in direction of the mouse
286   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
287   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
288    this->setAbsDir( rotQuat);
289   //this->setAbsDirSoft(mouseDir,5);
290
291  // this is the air friction (necessary for a smooth control)
292  if(velocity.len() != 0) velocity -= velocity*0.01;
293
294  //hoover effect
295  //cycle += time;
296  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
297
298  //readjust
299  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
300  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
301
302  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
303
304  this->shiftCoor (move);
305
306  this->getWeaponManager()->tick(time);
307  // weapon system manipulation
308  this->weaponAction();
309}
310
311/**
312 *  calculate the velocity
313 * @param time the timeslice since the last frame
314*/
315void SpaceShip::calculateVelocity (float time)
316{
317  //Vector accel(0.0, 0.0, 0.0);
318  /*
319  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
320  */
321  //float rotVal = 0.0;
322  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
323  /* calculate the direction in which the craft is heading  */
324
325  Plane plane(Vector(0,1,0), Vector(0,0,0));
326
327  if( this->bUp )
328   {
329     //this->shiftCoor(this->getAbsDirX());
330      //accel += (this->getAbsDirX())*2;
331      if(travelSpeed < 30) travelSpeed += acceleration*time;
332
333      /* Heli-Steuerung
334         accel += (this->getAbsDirX()*2;
335         if(
336      */
337   }
338
339  if( this->bDown )
340   {
341     //this->shiftCoor((this->getAbsDirX())*-1);
342     //accel -= (this->getAbsDirX())*2;
343     if(travelSpeed >= 0) travelSpeed -= acceleration*time;
344   }
345
346  if( this->bLeft/* > -this->getRelCoor().z*2*/)
347  {
348    this->shiftDir(Quaternion(time, Vector(0,1,0)));
349//    accel -= rightDirection;
350    //velocityDir.normalize();
351    //rot +=Vector(1,0,0);
352    //rotVal -= .4;
353  }
354  if( this->bRight /* > this->getRelCoor().z*2*/)
355  {
356    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
357
358    //    accel += rightDirection;
359    //velocityDir.normalize();
360    //rot += Vector(1,0,0);
361    //rotVal += .4;
362  }
363
364
365  if( this->bRollL /* > -this->getRelCoor().z*2*/)
366  {
367    mouseDir *= Quaternion(-time, Vector(1,0,0));
368//    accel -= rightDirection;
369    //velocityDir.normalize();
370    //rot +=Vector(1,0,0);
371    //rotVal -= .4;
372  }
373  if( this->bRollR /* > this->getRelCoor().z*2*/)
374  {
375    mouseDir *= Quaternion(time, Vector(1,0,0));
376
377    //    accel += rightDirection;
378    //velocityDir.normalize();
379    //rot += Vector(1,0,0);
380    //rotVal += .4;
381  }
382  if (this->bAscend )
383  {
384    this->shiftDir(Quaternion(time, Vector(0,0,1)));
385
386//    accel += upDirection;
387    //velocityDir.normalize();
388    //rot += Vector(0,0,1);
389    //rotVal += .4;
390  }
391  if (this->bDescend )
392  {
393    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
394
395    //    accel -= upDirection;
396    //velocityDir.normalize();
397    //rot += Vector(0,0,1);
398    //rotVal -= .4;
399  }
400
401  //velocity += accel;
402  //rot.normalize();
403  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
404}
405
406/**
407 * weapon manipulation by the player
408*/
409void SpaceShip::weaponAction()
410{
411  if( this->bFire)
412    {
413      this->getWeaponManager()->fire();
414    }
415}
416
417/**
418 * @todo switch statement ??
419 */
420void SpaceShip::process(const Event &event)
421{
422
423
424  if( event.type == KeyMapper::PEV_LEFT)
425      this->bRollL = event.bPressed;
426  else if( event.type == KeyMapper::PEV_RIGHT)
427      this->bRollR = event.bPressed;
428  else if( event.type == KeyMapper::PEV_FIRE1)
429      this->bFire = event.bPressed;
430  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
431  {
432    this->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
433  }
434  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
435    this->previousWeaponConfig();
436  else if( event.type == KeyMapper::PEV_UP)
437    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
438  else if( event.type == KeyMapper::PEV_DOWN)
439    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
440  else if( event.type == EV_MOUSE_MOTION)
441  {
442    this->xMouse = event.xRel;
443    this->yMouse = event.yRel;
444    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
445   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
446     //this->setAbsDir(mouseDir);
447  }
448}
449
450/**
451 *
452 */
453bool SpaceShip::pickup(PowerUp* powerUp)
454{
455  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
456    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
457    WeaponManager* manager = this->getWeaponManager();
458    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
459    if(slot >= 0) {
460      manager->addWeapon(weapon, 0, slot);
461      return true;
462    }
463  }
464  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
465    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
466    switch(ppu->getType()) {
467      case PARAM_SHIELD:
468        break;
469    }
470  }
471  return false;
472}
473
474#include "weapons/aiming_turret.h"
475// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
476void SpaceShip::ADDWEAPON()
477{
478  Weapon* turret = NULL;
479
480  if ((float)rand()/RAND_MAX < .9)
481  {
482    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
483    {
484      turret = new Turret();
485      this->addWeapon(turret, 2);
486      this->getWeaponManager()->changeWeaponConfig(2);
487    }
488  }
489  else
490  {
491    //if (this->getWeaponManager()->hasFreeSlot(3))
492    {
493      turret = dynamic_cast<Weapon*>(Factory::fabricate(CL_TARGETING_TURRET));
494      if (turret != NULL)
495      this->addWeapon(turret, 3);
496
497      this->getWeaponManager()->changeWeaponConfig(3);
498    }
499  }
500
501  if(turret != NULL)
502  {
503    turret->setName("Turret");
504    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
505  }
506}
507
508
509int SpaceShip::writeBytes( const byte * data, int length, int sender )
510{
511  setRequestedSync( false );
512  setIsOutOfSync( false );
513
514  SYNCHELP_READ_BEGIN();
515
516  SYNCHELP_READ_FKT( WorldEntity::writeState );
517
518  return SYNCHELP_READ_N;
519}
520
521int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
522{
523  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
524  {
525    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
526    setRequestedSync( true );
527  }
528
529  int rec = this->getRequestSync();
530  if ( rec > 0 )
531  {
532    *reciever = rec;
533
534    SYNCHELP_WRITE_BEGIN();
535
536    SYNCHELP_WRITE_FKT( WorldEntity::readState );
537
538    return SYNCHELP_WRITE_N;
539  }
540
541  *reciever = 0;
542  return 0;
543}
Note: See TracBrowser for help on using the repository browser.