Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/network/src/world_entities/space_ships/space_ship.cc @ 7444

Last change on this file since 7444 was 7444, checked in by rennerc, 18 years ago

new network system implemented. yet with a lot of empty function bodys

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