Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/space_ships/space_ship.cc @ 6420

Last change on this file since 6420 was 6420, checked in by rennerc, 18 years ago

tried to place ParamPowerUp

File size: 14.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 "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 // if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
318  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
319
320  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
321
322  this->shiftCoor (move);
323
324  this->getWeaponManager()->tick(time);
325  // weapon system manipulation
326  this->weaponAction();
327}
328
329/**
330 *  calculate the velocity
331 * @param time the timeslice since the last frame
332*/
333void SpaceShip::calculateVelocity (float time)
334{
335  Vector accel(0.0, 0.0, 0.0);
336  /*
337  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
338  */
339  //float rotVal = 0.0;
340  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
341  /* calculate the direction in which the craft is heading  */
342
343  Plane plane(Vector(0,1,0), Vector(0,0,0));
344
345  if( this->bUp )
346   {
347     //this->shiftCoor(this->getAbsDirX());
348      accel += (this->getAbsDirX())*2;
349
350      /* Heli-Steuerung
351         accel += (this->getAbsDirX()*2;
352         if(
353      */
354   }
355
356  if( this->bDown )
357   {
358     //this->shiftCoor((this->getAbsDirX())*-1);
359     accel -= (this->getAbsDirX())*2;
360   }
361
362  if( this->bLeft/* > -this->getRelCoor().z*2*/)
363  {
364    this->shiftDir(Quaternion(time, Vector(0,1,0)));
365//    accel -= rightDirection;
366    //velocityDir.normalize();
367    //rot +=Vector(1,0,0);
368    //rotVal -= .4;
369  }
370  if( this->bRight /* > this->getRelCoor().z*2*/)
371  {
372    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
373
374    //    accel += rightDirection;
375    //velocityDir.normalize();
376    //rot += Vector(1,0,0);
377    //rotVal += .4;
378  }
379
380
381  if( this->bRollL /* > -this->getRelCoor().z*2*/)
382  {
383    mouseDir *= Quaternion(-time, Vector(1,0,0));
384//    accel -= rightDirection;
385    //velocityDir.normalize();
386    //rot +=Vector(1,0,0);
387    //rotVal -= .4;
388  }
389  if( this->bRollR /* > this->getRelCoor().z*2*/)
390  {
391    mouseDir *= Quaternion(time, Vector(1,0,0));
392
393    //    accel += rightDirection;
394    //velocityDir.normalize();
395    //rot += Vector(1,0,0);
396    //rotVal += .4;
397  }
398  if (this->bAscend )
399  {
400    this->shiftDir(Quaternion(time, Vector(0,0,1)));
401
402//    accel += upDirection;
403    //velocityDir.normalize();
404    //rot += Vector(0,0,1);
405    //rotVal += .4;
406  }
407  if (this->bDescend )
408  {
409    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
410
411    //    accel -= upDirection;
412    //velocityDir.normalize();
413    //rot += Vector(0,0,1);
414    //rotVal -= .4;
415  }
416
417  velocity += accel;
418  //rot.normalize();
419  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
420}
421
422/**
423 * weapon manipulation by the player
424*/
425void SpaceShip::weaponAction()
426{
427  if( this->bFire)
428    {
429      this->getWeaponManager()->fire();
430    }
431}
432
433/**
434 * @todo switch statement ??
435 */
436void SpaceShip::process(const Event &event)
437{
438
439
440  if( event.type == SDLK_a)
441      this->bRollL = event.bPressed;
442  else if( event.type == SDLK_d)
443      this->bRollR = event.bPressed;
444  else if( event.type == KeyMapper::PEV_FIRE1)
445      this->bFire = event.bPressed;
446  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
447    this->getWeaponManager()->nextWeaponConfig();//if( !event.bPressed) this->bWeaponChange = !this->bWeaponChange;
448  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
449    this->getWeaponManager()->previousWeaponConfig();
450  else if( event.type == SDLK_w)
451    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
452  else if( event.type == SDLK_s)
453    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
454  else if( event.type == EV_MOUSE_MOTION)
455  {
456    this->xMouse = event.xRel;
457    this->yMouse = event.yRel;
458    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity, Vector(0,0,1)));
459   // if( xMouse*xMouse + yMouse*yMouse < 0.9)
460     //this->setAbsDir(mouseDir);
461  }
462}
463
464/**
465 *
466 */
467bool SpaceShip::pickup(PowerUp* powerUp)
468{
469  if(powerUp->isA(CL_WEAPON_POWER_UP)) {
470    Weapon* weapon = dynamic_cast<WeaponPowerUp*>(powerUp)->getWeapon();
471    WeaponManager* manager = this->getWeaponManager();
472    int slot = manager->getNextFreeSlot(0, weapon->getCapability());
473    if(slot >= 0) {
474      manager->addWeapon(weapon, 0, slot);
475      return true;
476    }
477  }
478  else if(powerUp->isA(CL_PARAM_POWER_UP)) {
479    ParamPowerUp* ppu = dynamic_cast<ParamPowerUp*>(powerUp);
480    switch(ppu->getType()) {
481      case PARAM_SHIELD:
482        break;
483    }
484  }
485  return false;
486}
487
488#include "weapons/aiming_turret.h"
489// FIXME THIS MIGHT BE CONSIDERED EITHER A FEATURE, OR A BUG
490void SpaceShip::ADDWEAPON()
491{
492  Weapon* turret = NULL;
493
494  if ((float)rand()/RAND_MAX < .9)
495  {
496    //if (this->getWeaponManager()->hasFreeSlot(2, WTYPE_TURRET))
497    {
498      turret = new Turret();
499      this->getWeaponManager()->addWeapon(turret, 2);
500      this->getWeaponManager()->changeWeaponConfig(2);
501    }
502  }
503  else
504  {
505    //if (this->getWeaponManager()->hasFreeSlot(3))
506    {
507      turret = new AimingTurret();
508      this->getWeaponManager()->addWeapon(turret, 3);
509
510      this->getWeaponManager()->changeWeaponConfig(3);
511    }
512  }
513
514  if(turret != NULL)
515  {
516    turret->setName("Turret");
517    turret->setStateDuration(WS_SHOOTING, (float)rand()/RAND_MAX*.5+.1);
518  }
519}
520
521
522int SpaceShip::writeBytes( const byte * data, int length, int sender )
523{
524  setRequestedSync( false );
525  setIsOutOfSync( false );
526
527  SYNCHELP_READ_BEGIN();
528
529  SYNCHELP_READ_FKT( WorldEntity::writeState );
530
531  return SYNCHELP_READ_N;
532}
533
534int SpaceShip::readBytes( byte * data, int maxLength, int * reciever )
535{
536  if ( isOutOfSync() && !requestedSync() && this->getHostID()!=this->getOwner() )
537  {
538    (NetworkGameManager::getInstance())->sync( this->getUniqueID(), this->getOwner() );
539    setRequestedSync( true );
540  }
541
542  int rec = this->getRequestSync();
543  if ( rec > 0 )
544  {
545    *reciever = rec;
546
547    SYNCHELP_WRITE_BEGIN();
548
549    SYNCHELP_WRITE_FKT( WorldEntity::readState );
550
551    return SYNCHELP_WRITE_N;
552  }
553
554  *reciever = 0;
555  return 0;
556}
Note: See TracBrowser for help on using the repository browser.