Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6330 was 6306, checked in by bensch, 19 years ago

orxonox/trunk: cool bars for the rocket-louncher

File size: 13.7 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//     GLGuiBar* bar = new GLGuiBar();
142//     bar->show();
143//     bar->setValue(7.0);
144//     bar->setMaximum(10);
145//     bar->setSize2D( 20, 100);
146//     bar->setAbsCoor2D( 10, 200);
147
148  //add events to the eventlist
149  registerEvent(SDLK_w);
150  registerEvent(SDLK_s);
151  registerEvent(SDLK_a);
152  registerEvent(SDLK_d);
153  registerEvent(SDLK_q);
154  registerEvent(SDLK_e);
155  registerEvent(KeyMapper::PEV_FIRE1);
156  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
157  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
158  registerEvent(SDLK_PAGEUP);
159  registerEvent(SDLK_PAGEDOWN);
160  registerEvent(EV_MOUSE_MOTION);
161
162  this->getWeaponManager()->setSlotCount(7);
163
164  this->getWeaponManager()->setSlotPosition(0, Vector(-2.6, .1, -3.0));
165  this->getWeaponManager()->setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
166
167  this->getWeaponManager()->setSlotPosition(1, Vector(-2.6, .1, 3.0));
168  this->getWeaponManager()->setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169
170  this->getWeaponManager()->setSlotPosition(2, Vector(-1.5, .5, -.5));
171  this->getWeaponManager()->setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
172
173  this->getWeaponManager()->setSlotPosition(3, Vector(-1.5, .5, .5));
174  this->getWeaponManager()->setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
175
176  this->getWeaponManager()->setSlotPosition(4, Vector(-1.5, -.5, .5));
177  this->getWeaponManager()->setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
178
179  this->getWeaponManager()->setSlotPosition(5, Vector(-1.5, -.5, -.5));
180  this->getWeaponManager()->setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
181//
182   this->getWeaponManager()->setSlotPosition(6, Vector(-1, 0.0, 0));
183   this->getWeaponManager()->setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
184   //
185//   this->getWeaponManager()->setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
186//   this->getWeaponManager()->setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
187//
188//   this->getWeaponManager()->setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
189//   this->getWeaponManager()->setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
190
191  this->getWeaponManager()->getFixedTarget()->setParent(this);
192  this->getWeaponManager()->getFixedTarget()->setRelCoor(100000,0,0);
193
194  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
195}
196
197/**
198 * loads the Settings of a SpaceShip from an XML-element.
199 * @param root the XML-element to load the Spaceship's properties from
200 */
201void SpaceShip::loadParams(const TiXmlElement* root)
202{
203  static_cast<WorldEntity*>(this)->loadParams(root);
204}
205
206
207void SpaceShip::enter()
208{
209  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( true);
210  this->attachCamera();
211
212
213}
214
215void SpaceShip::leave()
216{
217  dynamic_cast<Element2D*>(this->getWeaponManager()->getFixedTarget())->setVisibility( false);
218  this->detachCamera();
219
220
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  if (entity->isA(CL_TURRET_POWER_UP) && entity != ref)
266  {
267    this->ADDWEAPON();
268    ref = entity;
269    }
270//  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
271}
272
273/**
274 *  draws the spaceship after transforming it.
275*/
276void SpaceShip::draw () const
277{
278  WorldEntity::draw();
279  this->getWeaponManager()->draw();
280
281  //this->debug(0);
282}
283
284/**
285 *  the function called for each passing timeSnap
286 * @param time The timespan passed since last update
287*/
288void SpaceShip::tick (float time)
289{
290
291  // spaceship controlled movement
292  this->calculateVelocity(time);
293
294  Vector move = (velocity)*time;
295
296  //orient the velocity in the direction of the spaceship.
297  travelSpeed = velocity.len();
298  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
299  velocity = (velocity.getNormalized())*travelSpeed;
300
301  //orient the spaceship in direction of the mouse
302   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, fabsf(time)*3.0);
303   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
304    this->setAbsDir( rotQuat);
305   //this->setAbsDirSoft(mouseDir,5);
306
307  // this is the air friction (necessary for a smooth control)
308  if(velocity.len() != 0) velocity -= velocity*0.01;
309
310  //hoover effect
311  cycle += time;
312  this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
313
314  //readjust
315 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
316  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
317
318  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
319
320  this->shiftCoor (move);
321
322  this->getWeaponManager()->tick(time);
323  // weapon system manipulation
324  this->weaponAction();
325}
326
327/**
328 *  calculate the velocity
329 * @param time the timeslice since the last frame
330*/
331void SpaceShip::calculateVelocity (float time)
332{
333  Vector accel(0.0, 0.0, 0.0);
334  /*
335  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
336  */
337  //float rotVal = 0.0;
338  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
339  /* calculate the direction in which the craft is heading  */
340
341  Plane plane(Vector(0,1,0), Vector(0,0,0));
342
343  if( this->bUp )
344   {
345     //this->shiftCoor(this->getAbsDirX());
346      accel += (this->getAbsDirX())*2;
347
348      /* Heli-Steuerung
349         accel += (this->getAbsDirX()*2;
350         if(
351      */
352   }
353
354  if( this->bDown )
355   {
356     //this->shiftCoor((this->getAbsDirX())*-1);
357     accel -= (this->getAbsDirX())*2;
358   }
359
360  if( this->bLeft/* > -this->getRelCoor().z*2*/)
361  {
362    this->shiftDir(Quaternion(time, Vector(0,1,0)));
363//    accel -= rightDirection;
364    //velocityDir.normalize();
365    //rot +=Vector(1,0,0);
366    //rotVal -= .4;
367  }
368  if( this->bRight /* > this->getRelCoor().z*2*/)
369  {
370    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
371
372    //    accel += rightDirection;
373    //velocityDir.normalize();
374    //rot += Vector(1,0,0);
375    //rotVal += .4;
376  }
377
378
379  if( this->bRollL /* > -this->getRelCoor().z*2*/)
380  {
381    mouseDir *= Quaternion(-time, Vector(1,0,0));
382//    accel -= rightDirection;
383    //velocityDir.normalize();
384    //rot +=Vector(1,0,0);
385    //rotVal -= .4;
386  }
387  if( this->bRollR /* > this->getRelCoor().z*2*/)
388  {
389    mouseDir *= Quaternion(time, Vector(1,0,0));
390
391    //    accel += rightDirection;
392    //velocityDir.normalize();
393    //rot += Vector(1,0,0);
394    //rotVal += .4;
395  }
396  if (this->bAscend )
397  {
398    this->shiftDir(Quaternion(time, Vector(0,0,1)));
399
400//    accel += upDirection;
401    //velocityDir.normalize();
402    //rot += Vector(0,0,1);
403    //rotVal += .4;
404  }
405  if (this->bDescend )
406  {
407    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
408
409    //    accel -= upDirection;
410    //velocityDir.normalize();
411    //rot += Vector(0,0,1);
412    //rotVal -= .4;
413  }
414
415  velocity += accel;
416  //rot.normalize();
417  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
418}
419
420/**
421 * weapon manipulation by the player
422*/
423void SpaceShip::weaponAction()
424{
425  if( this->bFire)
426    {
427      this->getWeaponManager()->fire();
428    }
429}
430
431/**
432 * @todo switch statement ??
433 */
434void SpaceShip::process(const Event &event)
435{
436
437
438  if( event.type == SDLK_a)
439      this->bRollL = event.bPressed;
440  else if( event.type == SDLK_d)
441      this->bRollR = event.bPressed;
442  else if( event.type == KeyMapper::PEV_FIRE1)
443      this->bFire = event.bPressed;
444  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
445    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
446  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
447    this->getWeaponManager()->previousWeaponConfig();
448  else if( event.type == SDLK_w)
449    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
450  else if( event.type == SDLK_s)
451    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
452  else if( event.type == EV_MOUSE_MOTION)
453  {
454    this->xMouse = event.xRel;
455    this->yMouse = event.yRel;
456    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
457   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
458     //this->setAbsDir(mouseDir);
459  }
460}
461
462/**
463 *
464 */
465bool SpaceShip::pickup(PowerUp* powerUp)
466{
467  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
468    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
469    WeaponManager* manager = this->getWeaponManager();
470    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
471    if(slot >= 0) {
472      manager->addWeapon(weapon, 0, slot);
473      return true;
474    }
475  }
476  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
477    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
478    switch(ppu->getType()) {
479      case PARAM_SHIELD:
480        break;
481    }
482  }
483  return false;
484}
485
486#include "weapons/aiming_turret.h"
487// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
488void SpaceShip::ADDWEAPON()
489{
490  Weapon* turret = NULL;
491
492  if ((float)rand()/RAND_MAX < .9)
493  {
494    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
495    {
496      turret = new Turret();
497      this->getWeaponManager()->addWeapon(turret, 2);
498      this->getWeaponManager()->changeWeaponConfig(2);
499    }
500  }
501  else
502  {
503    //if (this->getWeaponManager()->hasFreeSlot(3))
504    {
505      turret = new AimingTurret();
506      this->getWeaponManager()->addWeapon(turret, 3);
507
508      this->getWeaponManager()->changeWeaponConfig(3);
509    }
510  }
511
512  if(turret != NULL)
513  {
514    turret->setName("Turret");
515    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
516  }
517}
Note: See TracBrowser for help on using the repository browser.