Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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