Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added setName scriptable method to script trigger, added loadModel to spaceship

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