Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the proxy bache back with no conflicts

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