Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

merged the single_player branche to trunk

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