Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Energy System on WorldEntity-level

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