Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the spaceshipcontrol branche back to the trunk

merged with command
svn merge https://svn.orxonox.net/orxonox/branches/spaceshipcontrol . -r6224:HEAD
small conflict in space_ship.cc fixed in favor of the control

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