Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: no more segfaults

File size: 13.5 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 "power_ups/weapon_power_up.h"
35#include "power_ups/param_power_up.h"
36
37#include "graphics_engine.h"
38
39
40
41#include "lib/gui/gl_gui/glgui_bar.h"
42#include "lib/gui/gl_gui/glgui_pushbutton.h"
43
44
45using namespace std;
46
47CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
48
49/**
50 *  creates the controlable Spaceship
51 */
52SpaceShip::SpaceShip()
53{
54  this->init();
55}
56
57/**
58 *  destructs the spaceship, deletes alocated memory
59 */
60SpaceShip::~SpaceShip ()
61{
62}
63
64/**
65 * loads a Spaceships information from a specified file.
66 * @param fileName the name of the File to load the spaceship from (absolute path)
67 */
68SpaceShip::SpaceShip(const char* fileName)
69{
70  this->init();
71  TiXmlDocument doc(fileName);
72
73  if(!doc.LoadFile())
74  {
75    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName);
76    return;
77  }
78
79  this->loadParams(doc.RootElement());
80}
81
82/**
83 *  creates a new Spaceship from Xml Data
84 * @param root the xml element containing spaceship data
85
86   @todo add more parameters to load
87*/
88SpaceShip::SpaceShip(const TiXmlElement* root)
89{
90  this->init();
91  if (root != NULL)
92    this->loadParams(root);
93
94  //weapons:
95  Weapon* wpRight = new TestGun(0);
96  wpRight->setName("testGun Right");
97  Weapon* wpLeft = new TestGun(1);
98  wpLeft->setName("testGun Left");
99  Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
100
101  cannon->setName("BFG");
102
103  this->getWeaponManager()->addWeapon(wpLeft, 1, 0);
104  this->getWeaponManager()->addWeapon(wpRight,1 ,1);
105  this->getWeaponManager()->addWeapon(cannon, 0, 6);
106
107  //this->getWeaponManager()->addWeapon(turret, 3, 0);
108
109  this->getWeaponManager()->changeWeaponConfig(1);
110}
111
112
113/**
114 * initializes a Spaceship
115 */
116void SpaceShip::init()
117{
118//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
119  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
120
121  PRINTF(4)("SPACESHIP INIT\n");
122
123  //EventHandler::getInstance()->grabEvents(true);
124
125  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
126  bFire = false;
127  xMouse = yMouse = 0;
128  mouseSensitivity = 0.001;
129  airViscosity = 1.0;
130  cycle = 0.0;
131
132
133  travelSpeed = 15.0;
134  this->velocity = Vector(0.0,0.0,0.0);
135  this->mouseDir = this->getAbsDir();
136
137//    GLGuiButton* button = new GLGuiPushButton();
138//    button->show();
139//    button->setLabel("orxonox");
140//    button->setBindNode(this);
141
142  //add events to the eventlist
143  registerEvent(SDLK_w);
144  registerEvent(SDLK_s);
145  registerEvent(SDLK_a);
146  registerEvent(SDLK_d);
147  registerEvent(SDLK_q);
148  registerEvent(SDLK_e);
149  registerEvent(KeyMapper::PEV_FIRE1);
150  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
151  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
152  registerEvent(SDLK_PAGEUP);
153  registerEvent(SDLK_PAGEDOWN);
154  registerEvent(EV_MOUSE_MOTION);
155
156  this->getWeaponManager()->setSlotCount(7);
157
158  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
159  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
160
161  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
162  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
163
164  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
165  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
166
167  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
168  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
169
170  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
171  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
172
173  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
174  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
175//
176   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
177   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
178   //
179//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
180//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
181//
182//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
183//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
184
185  this->getWeaponManager()->getFixedTarget()->setParent(this);
186  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
187
188  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
189}
190
191/**
192 * loads the Settings of a SpaceShip from an XML-element.
193 * @param root the XML-element to load the Spaceship's properties from
194 */
195void SpaceShip::loadParams(const TiXmlElement* root)
196{
197  static_cast<WorldEntity*>(this)->loadParams(root);
198}
199
200
201void SpaceShip::enter()
202{
203  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
204  this->attachCamera();
205
206
207}
208
209void SpaceShip::leave()
210{
211  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
212  this->detachCamera();
213
214
215}
216
217/**
218 * adds a weapon to the weapon list of the spaceship
219 * @param weapon to add
220*/
221void SpaceShip::addWeapon(Weapon* weapon)
222{
223  this->getWeaponManager()->addWeapon(weapon);
224}
225
226
227/**
228 *  removes a weapon from the spaceship
229 * @param weapon to remove
230*/
231void SpaceShip::removeWeapon(Weapon* weapon)
232{
233  this->getWeaponManager()->removeWeapon(weapon);
234}
235
236/**
237 *  effect that occurs after the SpaceShip is spawned
238*/
239void SpaceShip::postSpawn ()
240{
241  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
242}
243
244/**
245 *  the action occuring if the spaceship left the game
246*/
247void SpaceShip::leftWorld ()
248{}
249
250WorldEntity* ref = NULL;
251/**
252 *  this function is called, when two entities collide
253 * @param entity: the world entity with whom it collides
254 *
255 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
256 */
257void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
258{
259  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
260  {
261    this->ADDWEAPON();
262    ref = entity;
263    }
264//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
265}
266
267/**
268 *  draws the spaceship after transforming it.
269*/
270void SpaceShip::draw () const
271{
272  WorldEntity::draw();
273  this->getWeaponManager()->draw();
274
275  //this->debug(0);
276}
277
278/**
279 *  the function called for each passing timeSnap
280 * @param time The timespan passed since last update
281*/
282void SpaceShip::tick (float time)
283{
284
285  // spaceship controlled movement
286  this->calculateVelocity(time);
287
288  Vector move = (velocity)*time;
289
290  //orient the velocity in the direction of the spaceship.
291  travelSpeed = velocity.len();
292  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
293  velocity = (velocity.getNormalized())*travelSpeed;
294
295  //orient the spaceship in direction of the mouse
296   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
297   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
298    this->setAbsDir( rotQuat);
299   //this->setAbsDirSoft(mouseDir,5);
300
301  // this is the air friction (necessary for a smooth control)
302  if(velocity.len() != 0) velocity -= velocity*0.01;
303
304  //hoover effect
305  cycle += time;
306  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
307
308  //readjust
309 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
310  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
311
312  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
313
314  this->shiftCoor (move);
315
316  this->getWeaponManager()->tick(time);
317  // weapon system manipulation
318  this->weaponAction();
319}
320
321/**
322 *  calculate the velocity
323 * @param time the timeslice since the last frame
324*/
325void SpaceShip::calculateVelocity (float time)
326{
327  Vector accel(0.0, 0.0, 0.0);
328  /*
329  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
330  */
331  //float rotVal = 0.0;
332  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
333  /* calculate the direction in which the craft is heading  */
334
335  Plane plane(Vector(0,1,0), Vector(0,0,0));
336
337  if( this->bUp )
338   {
339     //this->shiftCoor(this->getAbsDirX());
340      accel += (this->getAbsDirX())*2;
341
342      /* Heli-Steuerung
343         accel += (this->getAbsDirX()*2;
344         if(
345      */
346   }
347
348  if( this->bDown )
349   {
350     //this->shiftCoor((this->getAbsDirX())*-1);
351     accel -= (this->getAbsDirX())*2;
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 == SDLK_a)
433      this->bRollL = event.bPressed;
434  else if( event.type == SDLK_d)
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    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
440  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
441    this->getWeaponManager()->previousWeaponConfig();
442  else if( event.type == SDLK_w)
443    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
444  else if( event.type == SDLK_s)
445    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
446  else if( event.type == EV_MOUSE_MOTION)
447  {
448    this->xMouse = event.xRel;
449    this->yMouse = event.yRel;
450    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
451   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
452     //this->setAbsDir(mouseDir);
453  }
454}
455
456/**
457 *
458 */
459bool SpaceShip::pickup(PowerUp* powerUp)
460{
461  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
462    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
463    WeaponManager* manager = this->getWeaponManager();
464    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
465    if(slot >= 0) {
466      manager->addWeapon(weapon, 0, slot);
467      return true;
468    }
469  }
470  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
471    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
472    switch(ppu->getType()) {
473      case PARAM_SHIELD:
474        break;
475    }
476  }
477  return false;
478}
479
480#include "weapons/aiming_turret.h"
481// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
482void SpaceShip::ADDWEAPON()
483{
484  Weapon* turret = NULL;
485
486  if ((float)rand()/RAND_MAX < .1)
487  {
488    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
489    {
490      turret = new Turret();
491      this->getWeaponManager()->addWeapon(turret, 2);
492      this->getWeaponManager()->changeWeaponConfig(2);
493    }
494  }
495  else
496  {
497    //if (this->getWeaponManager()->hasFreeSlot(3))
498    {
499      turret = new AimingTurret();
500      this->getWeaponManager()->addWeapon(turret, 3);
501
502      this->getWeaponManager()->changeWeaponConfig(3);
503    }
504  }
505
506  if(turret != NULL)
507  {
508    turret->setName("Turret");
509    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
510  }
511}
Note: See TracBrowser for help on using the repository browser.