Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8365 was 8228, checked in by patrick, 18 years ago

trunk: merged the network branche back to trunk with command: svn merge branches/network trunk -r8150:HEAD

File size: 14.1 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 "util/loading/resource_manager.h"
23
24#include "weapons/test_gun.h"
25#include "weapons/turret.h"
26#include "weapons/cannon.h"
27
28#include "dot_emitter.h"
29#include "sprite_particles.h"
30
31#include "util/loading/factory.h"
32#include "key_mapper.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#include "plane.h"
42
43#include "state.h"
44#include "player.h"
45
46#include "util/loading/load_param.h"
47
48
49// #include "lib/gui/gl_gui/glgui_bar.h"
50// #include "lib/gui/gl_gui/glgui_pushbutton.h"
51
52
53using namespace std;
54
55CREATE_FACTORY(SpaceShip, CL_SPACE_SHIP);
56
57
58/**
59 *  destructs the spaceship, deletes alocated memory
60 */
61SpaceShip::~SpaceShip ()
62{
63  this->setPlayer(NULL);
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 std::string& 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.c_str());
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}
97
98
99/**
100 * initializes a Spaceship
101 */
102void SpaceShip::init()
103{
104//  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
105  this->setClassID(CL_SPACE_SHIP, "SpaceShip");
106
107  PRINTF(4)("SPACESHIP INIT\n");
108
109  //weapons:
110  Weapon* wpRight = new TestGun(0);
111  wpRight->setName("testGun Right");
112  Weapon* wpLeft = new TestGun(1);
113  wpLeft->setName("testGun Left");
114  //Weapon* cannon = dynamic_cast<Weapon*>(Factory::fabricate(CL_CANNON));
115
116  //cannon->setName("BFG");
117
118  this->addWeapon(wpLeft, 1, 0);
119  this->addWeapon(wpRight,1 ,1);
120  //this->addWeapon(cannon, 0, 6);
121
122  this->getWeaponManager().changeWeaponConfig(1);
123
124  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
125
126  xMouse = yMouse = 0;
127  yInvert = 1;
128  mouseSensitivity = 0.001;
129  airViscosity = 0.9;
130  controlVelocityX = 25;
131  controlVelocityY = 150;
132  shipInertia = 1.5;
133//  cycle = 0.0;
134
135  this->setHealthMax(100);
136  this->setHealth(80);
137
138  travelSpeed = 0.0;
139  acceleration = 3;
140  this->velocity = this->getAbsDirX()*travelSpeed;
141  this->mouseDir = this->getAbsDir();
142  this->pitchDir = this->getAbsDir();
143
144//   GLGuiButton* button = new GLGuiPushButton();
145//    button->show();
146//    button->setLabel("orxonox");
147//    button->setBindNode(this);
148//     GLGuiBar* bar = new GLGuiBar();
149//     bar->show();
150//     bar->setValue(7.0);
151//     bar->setMaximum(10);
152//     bar->setSize2D( 20, 100);
153//     bar->setAbsCoor2D( 10, 200);
154
155  //add events to the eventlist
156  registerEvent(KeyMapper::PEV_FORWARD);
157  registerEvent(KeyMapper::PEV_BACKWARD);
158  registerEvent(KeyMapper::PEV_LEFT);
159  registerEvent(KeyMapper::PEV_RIGHT);
160  //registerEvent(SDLK_q);
161  //registerEvent(SDLK_e);
162  registerEvent(KeyMapper::PEV_FIRE1);
163  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
164  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
165  //registerEvent(SDLK_PAGEUP);
166  //registerEvent(SDLK_PAGEDOWN);
167  registerEvent(EV_MOUSE_MOTION);
168
169  this->getWeaponManager().setSlotCount(7);
170
171  this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
172  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
173
174  this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
175  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
176
177  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
178  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
179
180  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
181  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
182
183  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
184  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
185
186  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
187  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
188//
189   this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
190   this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
191   //
192//   this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
193//   this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
194//
195//   this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
196//   this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
197
198  this->getWeaponManager().getFixedTarget()->setParent(this);
199  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
200
201  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
202
203  this->burstEmitter = new DotEmitter(200, 0.0, .01);
204  this->burstEmitter->setParent(this);
205  this->burstEmitter->setRelCoor(-1, .5, 0);
206  this->burstEmitter->setName("SpaceShip_Burst_emitter");
207
208  this->burstSystem = new SpriteParticles(1000);
209  this->burstSystem->addEmitter(this->burstEmitter);
210  this->burstSystem->setName("SpaceShip_Burst_System");
211  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
212  this->burstSystem->setLifeSpan(1.0, .3);
213  this->burstSystem->setRadius(0.0, 1.0);
214  this->burstSystem->setRadius(0.05, 1.0);
215  this->burstSystem->setRadius(.5, .8);
216  this->burstSystem->setRadius(1.0, 0);
217  this->burstSystem->setColor(0.0, .7,.7,1,.7);
218  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
219  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
220  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
221 
222  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity" ) );
223  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) );
224
225  registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) );
226  registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) );
227  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
228  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
229  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
230  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
231  registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) );
232  registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) );
233}
234
235
236/**
237 * loads the Settings of a SpaceShip from an XML-element.
238 * @param root the XML-element to load the Spaceship's properties from
239 */
240void SpaceShip::loadParams(const TiXmlElement* root)
241{
242  Playable::loadParams(root);
243}
244
245void SpaceShip::setPlayDirection(const Quaternion& quat, float speed)
246{
247  this->mouseDir = quat;
248}
249
250
251void SpaceShip::reset()
252{
253  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
254
255  xMouse = yMouse = 0;
256
257  this->setHealth(80);
258  this->velocity = Vector(0.0, 0.0, 0.0);
259}
260
261
262void SpaceShip::enter()
263{
264  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
265  this->attachCamera();
266}
267
268void SpaceShip::leave()
269{
270  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
271  this->detachCamera();
272}
273
274
275/**
276 *  effect that occurs after the SpaceShip is spawned
277*/
278void SpaceShip::postSpawn ()
279{
280  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
281}
282
283/**
284 *  the action occuring if the spaceship left the game
285*/
286void SpaceShip::leftWorld ()
287{}
288
289WorldEntity* ref = NULL;
290/**
291 *  this function is called, when two entities collide
292 * @param entity: the world entity with whom it collides
293 *
294 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
295 */
296void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
297{
298  Playable::collidesWith(entity, location);
299
300  if( entity->isA(CL_PROJECTILE) && entity != ref)
301  {
302    if ( isServer() )
303    {
304      //TODO handle this
305    }
306  }
307  PRINTF(3)("collision %s vs %s @ (%f,%f,%f)\n", this->getClassName(), entity->getClassName(), location.x, location.y, location.z);
308}
309
310/**
311 *  draws the spaceship after transforming it.
312*/
313void SpaceShip::draw () const
314{
315  WorldEntity::draw();
316
317  //this->debug(0);
318}
319
320/**
321 *  the function called for each passing timeSnap
322 * @param time The timespan passed since last update
323*/
324void SpaceShip::tick (float time)
325{
326  Playable::tick(time);
327
328  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == this->getHostID() )
329   {
330    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
331    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
332    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
333    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
334
335    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
336
337    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
338    xMouse = yMouse = 0;
339   }
340
341
342//   if( this != State::getPlayer()->getControllable())
343//     return;
344
345  // spaceship controlled movement
346  //if (this->getOwner() == this->getHostID())
347    this->calculateVelocity(time);
348
349
350  Vector move = velocity*time;
351
352  //orient the velocity in the direction of the spaceship.
353  travelSpeed = velocity.len();
354  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
355  velocity = (velocity.getNormalized())*travelSpeed;
356  this->burstEmitter->setEmissionRate(travelSpeed);
357  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
358
359  //orient the spaceship in direction of the mouse
360   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
361   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
362    this->setAbsDir( rotQuat);
363   //this->setAbsDirSoft(mouseDir,5);
364
365  // this is the air friction (necessary for a smooth control)
366  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
367  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
368
369  //other physics (gravity)
370  //if(travelSpeed < 120)
371  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
372
373  //hoover effect
374  //cycle += time;
375  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
376
377  //readjust
378  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
379  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
380
381  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
382
383  this->shiftCoor(move);
384
385
386}
387
388/**
389 *  calculate the velocity
390 * @param time the timeslice since the last frame
391*/
392void SpaceShip::calculateVelocity (float time)
393{
394  Vector accel(0.0, 0.0, 0.0);
395  /*
396  Vector rot(0.0, 0.0, 0.0); // wird benötigt für Helicopter
397  */
398  //float rotVal = 0.0;
399  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
400  /* calculate the direction in which the craft is heading  */
401
402  //Plane plane(Vector(0,1,0), Vector(0,0,0));
403
404  if( this->bUp )
405   {
406     //this->shiftCoor(this->getAbsDirX());
407      //accel += (this->getAbsDirX())*2;
408      accel += (this->getAbsDirX())*acceleration;
409
410   }
411
412  if( this->bDown )
413   {
414     //this->shiftCoor((this->getAbsDirX())*-1);
415     //accel -= (this->getAbsDirX())*2;
416    //if(velocity.len() > 50)
417     accel -= (this->getAbsDirX())*0.5*acceleration;
418
419
420
421   }
422
423  if( this->bLeft/* > -this->getRelCoor().z*2*/)
424  {
425    this->shiftDir(Quaternion(time, Vector(0,1,0)));
426//    accel -= rightDirection;
427    //velocityDir.normalize();
428    //rot +=Vector(1,0,0);
429    //rotVal -= .4;
430  }
431  if( this->bRight /* > this->getRelCoor().z*2*/)
432  {
433    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
434
435    //    accel += rightDirection;
436    //velocityDir.normalize();
437    //rot += Vector(1,0,0);
438    //rotVal += .4;
439  }
440
441
442  if( this->bRollL /* > -this->getRelCoor().z*2*/)
443  {
444    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
445//    accel -= rightDirection;
446    //velocityDir.normalize();
447    //rot +=Vector(1,0,0);
448    //rotVal -= .4;
449  }
450  if( this->bRollR /* > this->getRelCoor().z*2*/)
451  {
452    mouseDir *= Quaternion(time*2, Vector(1,0,0));
453
454    //    accel += rightDirection;
455    //velocityDir.normalize();
456    //rot += Vector(1,0,0);
457    //rotVal += .4;
458  }
459  if (this->bAscend )
460  {
461    this->shiftDir(Quaternion(time, Vector(0,0,1)));
462
463//    accel += upDirection;
464    //velocityDir.normalize();
465    //rot += Vector(0,0,1);
466    //rotVal += .4;
467  }
468  if (this->bDescend )
469  {
470    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
471
472    //    accel -= upDirection;
473    //velocityDir.normalize();
474    //rot += Vector(0,0,1);
475    //rotVal -= .4;
476  }
477
478  velocity += accel*time*10;
479  //rot.normalize();
480  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
481}
482
483/**
484 * @todo switch statement ??
485 */
486void SpaceShip::process(const Event &event)
487{
488  Playable::process(event);
489
490  if( event.type == KeyMapper::PEV_LEFT)
491      this->bRollL = event.bPressed;
492  else if( event.type == KeyMapper::PEV_RIGHT)
493      this->bRollR = event.bPressed;
494  else if( event.type == KeyMapper::PEV_FORWARD)
495    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
496  else if( event.type == KeyMapper::PEV_BACKWARD)
497    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
498  else if( event.type == EV_MOUSE_MOTION)
499  {
500    this->xMouse += event.xRel;
501    this->yMouse += event.yRel;
502  }
503}
504
505
506
507
Note: See TracBrowser for help on using the repository browser.