Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the Presentation back

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