Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/space_ships/space_ship.cc @ 9958

Last change on this file since 9958 was 9958, checked in by marcscha, 17 years ago

Inline of damage get on projectile, part implementation of primary - secondary weapon and handling variables for the weapon manager

File size: 17.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 "particles/dot_emitter.h"
29#include "particles/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,
61                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
62                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
63                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
64                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
65                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
66                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
67                        //Coordinates
68                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
69                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
70                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
71                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, 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_FIRE2);                  // Added for secondary weapon support
180  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
181  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
182  //registerEvent(SDLK_PAGEUP);
183  //registerEvent(SDLK_PAGEDOWN);
184  registerEvent(EV_MOUSE_MOTION);
185
186  this->getWeaponManager().setSlotCount(7);
187
188  this->getWeaponManager().setSlotPosition(0, Vector(-2.6, .1, -3.0));
189  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
190
191  this->getWeaponManager().setSlotPosition(1, Vector(-2.6, .1, 3.0));
192  this->getWeaponManager().setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
193
194  this->getWeaponManager().setSlotPosition(2, Vector(-1.5, .5, -.5));
195  this->getWeaponManager().setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
196
197  this->getWeaponManager().setSlotPosition(3, Vector(-1.5, .5, .5));
198  this->getWeaponManager().setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
199
200  this->getWeaponManager().setSlotPosition(4, Vector(-1.5, -.5, .5));
201  this->getWeaponManager().setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
202
203  this->getWeaponManager().setSlotPosition(5, Vector(-1.5, -.5, -.5));
204  this->getWeaponManager().setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
205  //
206  this->getWeaponManager().setSlotPosition(6, Vector(-1, 0.0, 0));
207  this->getWeaponManager().setSlotCapability(6, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
208  //
209  //   this->getWeaponManager().setSlotPosition(8, Vector(-2.5, -0.3, -2.0));
210  //   this->getWeaponManager().setSlotDirection(8, Quaternion(-M_PI, Vector(1,0,0)));
211  //
212  //   this->getWeaponManager().setSlotPosition(9, Vector(-2.5, -0.3, 2.0));
213  //   this->getWeaponManager().setSlotDirection(9, Quaternion(+M_PI, Vector(1,0,0)));:
214
215  this->getWeaponManager().getFixedTarget()->setParent(this);
216  this->getWeaponManager().getFixedTarget()->setRelCoor(100000,0,0);
217
218  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
219
220  this->burstEmitter = new DotEmitter(200, 0.0, .01);
221  this->burstEmitter->setParent(this);
222  this->burstEmitter->setRelCoor(-1, .5, 0);
223  this->burstEmitter->setName("SpaceShip_Burst_emitter");
224
225  this->burstSystem = new SpriteParticles(1000);
226  this->burstSystem->addEmitter(this->burstEmitter);
227  this->burstSystem->setName("SpaceShip_Burst_System");
228  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
229  this->burstSystem->setLifeSpan(1.0, .3);
230  this->burstSystem->setRadius(0.0, 1.0);
231  this->burstSystem->setRadius(0.05, 1.0);
232  this->burstSystem->setRadius(.5, .8);
233  this->burstSystem->setRadius(1.0, 0);
234  this->burstSystem->setColor(0.0, .7,.7,1,.7);
235  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
236  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
237  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
238
239  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
240  registerVar( new SynchronizeableQuaternion( &mouseDir, &mouseDir, "mousedir", PERMISSION_OWNER ) );
241
242  registerVar( new SynchronizeableBool( &bUp, &bUp, "bUp", PERMISSION_OWNER ) );
243  registerVar( new SynchronizeableBool( &bDown, &bDown, "bDown", PERMISSION_OWNER ) );
244  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
245  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
246  registerVar( new SynchronizeableBool( &bAscend, &bAscend, "bAscend", PERMISSION_OWNER ) );
247  registerVar( new SynchronizeableBool( &bDescend, &bDescend, "bDescend", PERMISSION_OWNER ) );
248  registerVar( new SynchronizeableBool( &bRollL, &bRollL, "bRollL", PERMISSION_OWNER ) );
249  registerVar( new SynchronizeableBool( &bRollR, &bRollR, "bRollR", PERMISSION_OWNER ) );
250}
251
252
253/**
254 * loads the Settings of a SpaceShip from an XML-element.
255 * @param root the XML-element to load the Spaceship's properties from
256 */
257void SpaceShip::loadParams(const TiXmlElement* root)
258{
259  Playable::loadParams(root);
260}
261
262void SpaceShip::setPlayDirection(const Quaternion& quat, float speed)
263{
264  this->mouseDir = quat;
265}
266
267
268void SpaceShip::reset()
269{
270  bUp = bDown = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = false;
271
272  xMouse = yMouse = 0;
273
274  this->setHealth(80);
275  this->velocity = Vector(0.0, 0.0, 0.0);
276}
277
278
279void SpaceShip::enter()
280{
281  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true);
282  this->attachCamera();
283}
284
285void SpaceShip::leave()
286{
287  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
288  this->detachCamera();
289}
290
291
292/**
293 *  effect that occurs after the SpaceShip is spawned
294*/
295void SpaceShip::postSpawn ()
296{
297  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
298}
299
300/**
301 *  the action occuring if the spaceship left the game
302*/
303void SpaceShip::leftWorld ()
304{}
305
306WorldEntity* ref = NULL;
307/**
308 *  this function is called, when two entities collide
309 * @param entity: the world entity with whom it collides
310 *
311 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
312 */
313void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
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  // Shield Regeneration and other regular calculations on the ship
349  this->regen(time);
350
351
352  //   if( this != State::getPlayer()->getControllable())
353  //     return;
354
355  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
356  //if (this->getOwner() == this->getHostID())
357  this->calculateVelocity(time);
358
359
360  Vector move = velocity*time;
361
362  //orient the velocity in the direction of the spaceship.
363  travelSpeed = velocity.len();
364  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
365  velocity = (velocity.getNormalized())*travelSpeed;
366  this->burstEmitter->setEmissionRate(travelSpeed);
367  this->burstEmitter->setEmissionVelocity(travelSpeed*.5, travelSpeed *.1);
368
369  //orient the spaceship in direction of the mouse
370  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
371  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
372    this->setAbsDir( rotQuat);
373  //this->setAbsDirSoft(mouseDir,5);
374
375  // this is the air friction (necessary for a smooth control)
376  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
377  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
378
379  //other physics (gravity)
380  //if(travelSpeed < 120)
381  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
382
383  //hoover effect
384  //cycle += time;
385  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
386
387  //readjust
388  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
389  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
390
391  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
392
393  this->shiftCoor(move);
394
395  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
396
397}
398
399/**
400 *  calculate the velocity
401 * @param time the timeslice since the last frame
402*/
403void SpaceShip::calculateVelocity (float time)
404{
405  Vector accel(0.0, 0.0, 0.0);
406  /*
407  Vector rot(0.0, 0.0, 0.0); // wird ben�igt fr Helicopter
408  */
409  //float rotVal = 0.0;
410  /* FIXME: calculating the direction and orthDirection every timeSlice is redundant! save it somewhere */
411  /* calculate the direction in which the craft is heading  */
412
413  //Plane plane(Vector(0,1,0), Vector(0,0,0));
414
415  if( this->bUp )
416  {
417    //this->shiftCoor(this->getAbsDirX());
418    //accel += (this->getAbsDirX())*2;
419    accel += (this->getAbsDirX())*acceleration;
420
421  }
422
423  if( this->bDown )
424  {
425    //this->shiftCoor((this->getAbsDirX())*-1);
426    //accel -= (this->getAbsDirX())*2;
427    //if(velocity.len() > 50)
428    accel -= (this->getAbsDirX())*0.5*acceleration;
429
430
431
432  }
433
434  if( this->bLeft/* > -this->getRelCoor().z*2*/)
435  {
436    this->shiftDir(Quaternion(time, Vector(0,1,0)));
437    //    accel -= rightDirection;
438    //velocityDir.normalize();
439    //rot +=Vector(1,0,0);
440    //rotVal -= .4;
441  }
442  if( this->bRight /* > this->getRelCoor().z*2*/)
443  {
444    this->shiftDir(Quaternion(-time, Vector(0,1,0)));
445
446    //    accel += rightDirection;
447    //velocityDir.normalize();
448    //rot += Vector(1,0,0);
449    //rotVal += .4;
450  }
451
452
453  if( this->bRollL /* > -this->getRelCoor().z*2*/)
454  {
455    mouseDir *= Quaternion(-time*2, Vector(1,0,0));
456    //    accel -= rightDirection;
457    //velocityDir.normalize();
458    //rot +=Vector(1,0,0);
459    //rotVal -= .4;
460  }
461  if( this->bRollR /* > this->getRelCoor().z*2*/)
462  {
463    mouseDir *= Quaternion(time*2, Vector(1,0,0));
464
465    //    accel += rightDirection;
466    //velocityDir.normalize();
467    //rot += Vector(1,0,0);
468    //rotVal += .4;
469  }
470  if (this->bAscend )
471  {
472    this->shiftDir(Quaternion(time, Vector(0,0,1)));
473
474    //    accel += upDirection;
475    //velocityDir.normalize();
476    //rot += Vector(0,0,1);
477    //rotVal += .4;
478  }
479  if (this->bDescend )
480  {
481    this->shiftDir(Quaternion(-time, Vector(0,0,1)));
482
483    //    accel -= upDirection;
484    //velocityDir.normalize();
485    //rot += Vector(0,0,1);
486    //rotVal -= .4;
487  }
488
489  velocity += accel*time*10;
490  //rot.normalize();
491  //this->setRelDirSoft(Quaternion(rotVal, rot), 5);
492}
493
494/**
495 * @todo switch statement ??
496 */
497void SpaceShip::process(const Event &event)
498{
499  Playable::process(event);
500
501  if( event.type == KeyMapper::PEV_LEFT)
502    this->bRollL = event.bPressed;
503  else if( event.type == KeyMapper::PEV_RIGHT)
504    this->bRollR = event.bPressed;
505  else if( event.type == KeyMapper::PEV_FORWARD)
506    this->bUp = event.bPressed; //this->shiftCoor(0,.1,0);
507  else if( event.type == KeyMapper::PEV_BACKWARD)
508    this->bDown = event.bPressed; //this->shiftCoor(0,-.1,0);
509  else if( event.type == KeyMapper::PEV_FIRE1)
510    firedWeapon = curWeaponPrimary;
511  else if( event.type == KeyMapper::PEV_FIRE2)
512    firedWeapon = curWeaponSecondary;
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
531void SpaceShip::damage(float pDamage, float eDamage){
532if( this->shieldActive) {
533    if( this->shieldCur > pDamage) {
534      this->shieldCur = this->shieldCur - pDamage;
535    }
536    else { // shield <= pDamage
537      this->shieldCur = 0;
538      this->shieldActive = false; //shield collaptses
539      pDamage = pDamage - this->shieldCur;
540      if( !this->shieldActive) {
541        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
542        this->electronicCur -= eDamage;
543      }
544    }
545  }
546  else {
547    this->armorCur = this->armorCur - pDamage;
548    this->electronicCur = this->electronicCur - eDamage;
549  }
550  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
551}
552
553void SpaceShip::regen(float time){
554  float tmp;
555  if (this->shieldCur != this->shieldMax){
556    tmp =  this->shieldCur + this->shieldRegen * time;
557    if( tmp > shieldMax)
558      this->shieldCur = this->shieldMax;
559    else
560      this->shieldCur = tmp;
561    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
562  }
563  if (this->electronicCur != this->electronicMax){
564    tmp = this->electronicCur + this->electronicRegen * time;
565    if ( tmp > electronicMax)
566      this->electronicCur = this->electronicMax;
567    else
568      this->electronicCur = tmp;
569  }
570}
Note: See TracBrowser for help on using the repository browser.