Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/space_ship.cc @ 6440

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

orxonox/trunk: now the Widgets are only rendered within the HUD

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