Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/world_entities/space_ships/space_ship.cc @ 9452

Last change on this file since 9452 was 9452, checked in by patrick, 18 years ago

switched another interface funciton

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