Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: collides with function now takes away the Energy of our beloved Entity

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