Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/world_entities/space_ships/space_ship.cc @ 9716

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

more renamings

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
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, SpaceShip::classID(),
61                        addMethod("hasPlayer", ExecutorLua0ret<Playable,bool>(&Playable::hasPlayer))
62                        ->addMethod("fire", ExecutorLua1<Playable, bool>(&Playable::fire))
63                        ->addMethod("loadModel", ExecutorLua2<WorldEntity,const std::string& ,float>(&WorldEntity::loadModel2))
64                        ->addMethod("setName", ExecutorLua1<BaseObject,const std::string&>(&BaseObject::setName))
65                        ->addMethod("hide", ExecutorLua0<WorldEntity>(&WorldEntity::hide))
66                        ->addMethod("unhide", ExecutorLua0<WorldEntity>(&WorldEntity::unhide))
67                       //Coordinates
68                        ->addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
69                        ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
70                        ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
71                        ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, 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/**
318 *  draws the spaceship after transforming it.
319*/
320void SpaceShip::draw () const
321{
322  WorldEntity::draw();
323
324  //this->debug(0);
325}
326
327/**
328 *  the function called for each passing timeSnap
329 * @param time The timespan passed since last update
330*/
331void SpaceShip::tick (float time)
332{
333  Playable::tick(time);
334
335  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
336   {
337    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
338    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
339    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
340    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
341
342    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
343
344    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
345    xMouse = yMouse = 0;
346   }
347
348
349//   if( this != State::getPlayer()->getControllable())
350//     return;
351
352  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
353  //if (this->getOwner() == this->getHostID())
354    this->calculateVelocity(time);
355
356
357  Vector move = velocity*time;
358
359  //orient the velocity in the direction of the spaceship.
360  travelSpeed = velocity.len();
361  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
362  velocity = (velocity.getNormalized())*travelSpeed;
363  this->burstEmitter->setEmissionRate(travelSpeed);
364  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
365
366  //orient the spaceship in direction of the mouse
367   rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
368   if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
369    this->setAbsDir( rotQuat);
370   //this->setAbsDirSoft(mouseDir,5);
371
372  // this is the air friction (necessary for a smooth control)
373  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
374  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
375
376  //other physics (gravity)
377  //if(travelSpeed < 120)
378  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
379
380  //hoover effect
381  //cycle += time;
382  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
383
384  //readjust
385  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
386  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
387
388  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
389
390  this->shiftCoor(move);
391
392//   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
393
394}
395
396/**
397 *  calculate the velocity
398 * @param time the timeslice since the last frame
399*/
400void SpaceShip::calculateVelocity (float time)
401{
402  Vector accel(0.0, 0.0, 0.0);
403  /*
404  Vector rot(0.0, 0.0, 0.0); // wird ben�igt fr Helicopter
405  */
406  //float rotVal = 0.0;
407  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
408  /* calculate the direction in which the craft is heading  */
409
410  //Plane plane(Vector(0,1,0), Vector(0,0,0));
411
412  if( this->bUp )
413   {
414     //this->shiftCoor(this->getAbsDirX());
415      //accel += (this->getAbsDirX())*2;
416      accel += (this->getAbsDirX())*acceleration;
417
418   }
419
420  if( this->bDown )
421   {
422     //this->shiftCoor((this->getAbsDirX())*-1);
423     //accel -= (this->getAbsDirX())*2;
424    //if(velocity.len() > 50)
425     accel -= (this->getAbsDirX())*0.5*acceleration;
426
427
428
429   }
430
431  if( this->bLeft/* > -this->getRelCoor().z*2*/)
432  {
433    this->shiftDir(Quaternion(time, Vector(0,1,0)));
434//    accel -= rightDirection;
435    //velocityDir.normalize();
436    //rot +=Vector(1,0,0);
437    //rotVal -= .4;
438  }
439  if( this->bRight /* > this->getRelCoor().z*2*/)
440  {
441    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
442
443    //    accel += rightDirection;
444    //velocityDir.normalize();
445    //rot += Vector(1,0,0);
446    //rotVal += .4;
447  }
448
449
450  if( this->bRollL /* > -this->getRelCoor().z*2*/)
451  {
452    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
453//    accel -= rightDirection;
454    //velocityDir.normalize();
455    //rot +=Vector(1,0,0);
456    //rotVal -= .4;
457  }
458  if( this->bRollR /* > this->getRelCoor().z*2*/)
459  {
460    mouseDir *= Quaternion(time*2, Vector(1,0,0));
461
462    //    accel += rightDirection;
463    //velocityDir.normalize();
464    //rot += Vector(1,0,0);
465    //rotVal += .4;
466  }
467  if (this->bAscend )
468  {
469    this->shiftDir(Quaternion(time, Vector(0,0,1)));
470
471//    accel += upDirection;
472    //velocityDir.normalize();
473    //rot += Vector(0,0,1);
474    //rotVal += .4;
475  }
476  if (this->bDescend )
477  {
478    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
479
480    //    accel -= upDirection;
481    //velocityDir.normalize();
482    //rot += Vector(0,0,1);
483    //rotVal -= .4;
484  }
485
486  velocity += accel*time*10;
487  //rot.normalize();
488  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
489}
490
491/**
492 * @todo switch statement ??
493 */
494void SpaceShip::process(const Event &event)
495{
496  Playable::process(event);
497
498  if( event.type == KeyMapper::PEV_LEFT)
499      this->bRollL = event.bPressed;
500  else if( event.type == KeyMapper::PEV_RIGHT)
501      this->bRollR = event.bPressed;
502  else if( event.type == KeyMapper::PEV_FORWARD)
503    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
504  else if( event.type == KeyMapper::PEV_BACKWARD)
505    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
506  else if( event.type == EV_MOUSE_MOTION)
507  {
508    this->xMouse += event.xRel;
509    this->yMouse += event.yRel;
510  }
511}
512
513void SpaceShip::destroy( WorldEntity* killer )
514{
515  PRINTF(0)("spaceship destroy\n");
516}
517
518void SpaceShip::respawn( )
519{
520  toList( OM_PLAYERS );
521}
522
523
524
525
Note: See TracBrowser for help on using the repository browser.