Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/space_ships/space_ship.cc @ 9221

Last change on this file since 9221 was 9221, checked in by snellen, 18 years ago

update

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