Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

weapon, collision, particles

File size: 27.2 KB
RevLine 
[6868]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
[7193]22#include "util/loading/resource_manager.h"
[6868]23
24#include "weapons/test_gun.h"
[9970]25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
[10023]28#include "weapons/swarm_launcher.h"
[9970]29#include "weapons/boomerang_gun.h"
[6868]30#include "weapons/turret.h"
31#include "weapons/cannon.h"
32
[9869]33#include "particles/dot_emitter.h"
34#include "particles/sprite_particles.h"
[6868]35
[7193]36#include "util/loading/factory.h"
[6868]37#include "key_mapper.h"
38
39#include "network_game_manager.h"
[8708]40#include "shared_network_data.h"
[6868]41
42#include "power_ups/weapon_power_up.h"
43#include "power_ups/param_power_up.h"
44
45#include "graphics_engine.h"
46
47#include "plane.h"
48
49#include "state.h"
50#include "player.h"
51
[7193]52#include "util/loading/load_param.h"
[10020]53#include "time.h"
[6868]54
[7056]55
[6868]56// #include "lib/gui/gl_gui/glgui_bar.h"
57// #include "lib/gui/gl_gui/glgui_pushbutton.h"
58
59
[9869]60#include "class_id_DEPRECATED.h"
61ObjectListDefinitionID(SpaceShip, CL_SPACE_SHIP);
62CREATE_FACTORY(SpaceShip);
[9406]63
[9061]64#include "script_class.h"
[9869]65CREATE_SCRIPTABLE_CLASS(SpaceShip,
66                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
67                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
68                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
69                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
70                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
71                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
72                        //Coordinates
73                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
74                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
75                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
76                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
[10017]77                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
[9061]78                       );
[6868]79
80/**
81 *  destructs the spaceship, deletes alocated memory
82 */
83SpaceShip::~SpaceShip ()
84{
[6986]85  this->setPlayer(NULL);
[10064]86  delete this->emitter;
[6868]87}
88
89/**
90 * loads a Spaceships information from a specified file.
91 * @param fileName the name of the File to load the spaceship from (absolute path)
92 */
[7221]93SpaceShip::SpaceShip(const std::string& fileName)
[10019]94    : secWeaponMan(this) //,
95    //supportedPlaymodes(Playable::Vertical) ,
96    //playmode(Playable::Vertical)
[6868]97{
98  this->init();
99  TiXmlDocument doc(fileName);
100
101  if(!doc.LoadFile())
102  {
[7221]103    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
[6868]104    return;
105  }
106
107  this->loadParams(doc.RootElement());
108}
109
110/**
111 *  creates a new Spaceship from Xml Data
112 * @param root the xml element containing spaceship data
113
114   @todo add more parameters to load
115*/
116SpaceShip::SpaceShip(const TiXmlElement* root)
[10019]117    : secWeaponMan(this) //,
118    //supportedPlaymodes(Playable::Vertical) ,
119    //playmode(Playable::Vertical)
[6868]120{
121  this->init();
122  if (root != NULL)
123    this->loadParams(root);
124
125}
126
127
128/**
129 * initializes a Spaceship
130 */
131void SpaceShip::init()
132{
[9998]133
[10022]134  srand(time(0));   //initialize Random Nomber Generator
135
[9869]136  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
137  this->registerObject(this, SpaceShip::_objectList);
[10064]138  this->toList(OM_GROUP_00);
[6868]139  PRINTF(4)("SPACESHIP INIT\n");
140
[9998]141  secWeaponMan.showCrosshair();
142
[6868]143  //weapons:
[9998]144
[9996]145  Weapon* wpRight1 = new LightBlaster ();
[10030]146  wpRight1->setName("LightBlaster");
[9996]147  Weapon* wpLeft1 = new LightBlaster ();
[10030]148  wpLeft1->setName("LightBlaster");
[6868]149
[9996]150  Weapon* wpRight2 = new MediumBlaster ();
[10030]151  wpRight2->setName("MediumBlaster");
[9996]152  Weapon* wpLeft2 = new MediumBlaster ();
[10030]153  wpLeft2->setName("MediumBlaster");
[9996]154
[10030]155  Weapon* wpRight3 = new HeavyBlaster ();
156  wpRight3->setName("HeavyBlaster");
[9996]157  Weapon* wpLeft3 = new HeavyBlaster ();
[10030]158  wpLeft3->setName("HeavyBlaster");
[9996]159
[10023]160  Weapon* cannon = new SwarmLauncher();
161  cannon->setName("SwarmLauncher");
[10004]162 
[6868]163
[9961]164
[10016]165  this->weaponMan.addWeapon( wpLeft1, 0, 0);
166  this->weaponMan.addWeapon( wpRight1, 0, 1);
167  this->weaponMan.addWeapon( wpLeft2, 0, 2);
168  this->weaponMan.addWeapon( wpRight2, 0, 3);
[10030]169  this->weaponMan.addWeapon( wpLeft3, 0, 4);
170  this->weaponMan.addWeapon( wpRight3, 0, 5);
[9998]171
[10023]172  this->secWeaponMan.addWeapon( cannon, 1, 0);
[9961]173
[9965]174  this->weaponMan.changeWeaponConfig(0);
[10023]175  this->secWeaponMan.changeWeaponConfig(1);
[9961]176
[9996]177  wpRight1->requestAction(WA_ACTIVATE);
178  wpLeft1->requestAction(WA_ACTIVATE);
179  wpRight2->requestAction(WA_ACTIVATE);
180  wpLeft2->requestAction(WA_ACTIVATE);
[10030]181  wpRight3->requestAction(WA_ACTIVATE);
182  wpLeft3->requestAction(WA_ACTIVATE);
[9998]183
[9961]184  cannon->requestAction(WA_ACTIVATE);
[9965]185
[9970]186  curWeaponPrimary    = 0;
187  curWeaponSecondary  = 0;
[9965]188
189  Playable::weaponConfigChanged();
190
[9970]191  reactorOutput     = 10;
[9965]192
[9970]193  weaponEnergyRegen = 10;       // 10 einheiten pro Sekunde
194  engineSpeedBase   = 5;
195  shieldRegen       = 2;
196
197  shieldEnergyShare = 0.3;
198  weaponEnergyShare = 0.3;
199  engineEnergyShare = 0.4;
200
201  shieldCur         = 20;
202  shieldMax         = 100;
[10020]203  shieldTH          = .2* shieldMax;   // shield power must be 20% before shield kicks in again
[9970]204
[10024]205  electronicCur = 50;
[10022]206  electronicMax = 50;
[10020]207  electronicRegen   = 3;
208  electronicTH      = .7 * electronicMax; // 30% of eDamage can be handled by the ship
209
[6868]210
[9970]211  this->loadModel("models/ships/mantawing.obj");
[10017]212  //this->setVisibiliy(false);
[6868]213
[10017]214  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
215 
[6868]216
[9970]217  this->setHealthMax(shieldMax);
218  this->setHealth(shieldCur);
[6868]219
[10017]220  this->travelNode = new PNode();
221
222  // camera - issue
223  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
224  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
225
[6868]226
[9965]227
[6868]228  //add events to the eventlist
[6997]229  registerEvent(KeyMapper::PEV_FORWARD);
230  registerEvent(KeyMapper::PEV_BACKWARD);
[6868]231  registerEvent(KeyMapper::PEV_LEFT);
232  registerEvent(KeyMapper::PEV_RIGHT);
233  //registerEvent(SDLK_q);
234  //registerEvent(SDLK_e);
235  registerEvent(KeyMapper::PEV_FIRE1);
[9958]236  registerEvent(KeyMapper::PEV_FIRE2);                  // Added for secondary weapon support
[6868]237  registerEvent(KeyMapper::PEV_NEXT_WEAPON);
238  registerEvent(KeyMapper::PEV_PREVIOUS_WEAPON);
239  //registerEvent(SDLK_PAGEUP);
240  //registerEvent(SDLK_PAGEDOWN);
241  registerEvent(EV_MOUSE_MOTION);
242
[9965]243  this->weaponMan.setSlotCount(6);
[6868]244
[10064]245  this->weaponMan.setSlotPosition(0, Vector(-2.6, 0, -3.0));
[9965]246  this->weaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]247
[10064]248  this->weaponMan.setSlotPosition(1, Vector(-2.6, 0, 3.0));
[9965]249  this->weaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[6868]250
[10064]251  this->weaponMan.setSlotPosition(2, Vector(-1.5, 0, -.5));
[9965]252  this->weaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
[6868]253
[10064]254  this->weaponMan.setSlotPosition(3, Vector(-1.5, 0, .5));
[9965]255  this->weaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
[6868]256
[10064]257  this->weaponMan.setSlotPosition(4, Vector(-1.5, 0, .5));
[9965]258  this->weaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
[6868]259
[10064]260  this->weaponMan.setSlotPosition(5, Vector(-1.5, 0, -.5));
[9965]261  this->weaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9961]262
263  this->secWeaponMan.setSlotCount(6);
264
[10004]265  this->secWeaponMan.setSlotPosition(0, Vector(1.5, -1, 0));
[9961]266  this->secWeaponMan.setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
267
268  this->secWeaponMan.setSlotPosition(1, Vector(2.6, .1, 3.0));
269  this->secWeaponMan.setSlotCapability(1, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
270
271  this->secWeaponMan.setSlotPosition(2, Vector(1.5, .5, -.5));
272  this->secWeaponMan.setSlotDirection(2, Quaternion(-M_PI_4*.5, Vector(1,0,0)));
273
274  this->secWeaponMan.setSlotPosition(3, Vector(1.5, .5, .5));
275  this->secWeaponMan.setSlotDirection(3, Quaternion(M_PI_4*.5, Vector(1,0,0)));
276
277  this->secWeaponMan.setSlotPosition(4, Vector(1.5, -.5, .5));
278  this->secWeaponMan.setSlotDirection(4, Quaternion(-M_PI_4*.5+M_PI, Vector(1,0,0)));
279
280  this->secWeaponMan.setSlotPosition(5, Vector(1.5, -.5, -.5));
281  this->secWeaponMan.setSlotDirection(5, Quaternion(+M_PI_4*.5-M_PI, Vector(1,0,0)));
[9965]282
[6868]283
[9965]284  this->weaponMan.getFixedTarget()->setParent(this);
285  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]286
[9998]287 
[9961]288  this->secWeaponMan.getFixedTarget()->setParent(this);
289  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
[10004]290  this->secWeaponMan.setRotationSpeed(0);
[6868]291
[9965]292  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
[9961]293
[6868]294  this->burstEmitter = new DotEmitter(200, 0.0, .01);
295  this->burstEmitter->setParent(this);
296  this->burstEmitter->setRelCoor(-1, .5, 0);
297  this->burstEmitter->setName("SpaceShip_Burst_emitter");
[10064]298  this->burstEmitter->setEmissionRate(200);
[6868]299
300  this->burstSystem = new SpriteParticles(1000);
301  this->burstSystem->addEmitter(this->burstEmitter);
302  this->burstSystem->setName("SpaceShip_Burst_System");
303  ((SpriteParticles*)this->burstSystem)->setMaterialTexture("maps/radial-trans-noise.png");
[10064]304  this->burstSystem->setLifeSpan(0.4, .3);
[6868]305  this->burstSystem->setRadius(0.0, 1.0);
306  this->burstSystem->setRadius(0.05, 1.0);
[10064]307  this->burstSystem->setRadius(.5, .6);
[6868]308  this->burstSystem->setRadius(1.0, 0);
309  this->burstSystem->setColor(0.0, .7,.7,1,.7);
310  this->burstSystem->setColor(0.2, 0,0,0.8,.5);
311  this->burstSystem->setColor(0.5, .5,.5,.8,.8);
312  this->burstSystem->setColor(1.0, .8,.8,.8,.0);
[9008]313
[10064]314  this->emitter = NULL;
[10017]315
316  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
317  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
318  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
319  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
320  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
321  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
[10023]322  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
[10017]323  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
324
325  //this->airFriction = 0.5f;
[10019]326  this->travelDistancePlus = Vector2D(38.0, 43.0);
327  this->travelDistanceMinus = Vector2D(-38.0, -43.0);
[10017]328  this->cameraSpeed = 40;
[10019]329  this->cameraLook = 0.0f;
330  //this->airFriction = 0.0f;
[10017]331
[10020]332  srand(time(0));  //initaialize RNG
333
[10017]334  this->travelNode->debugDraw();
[10032]335
336  this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical);
[6868]337}
338
339
340/**
341 * loads the Settings of a SpaceShip from an XML-element.
342 * @param root the XML-element to load the Spaceship's properties from
343 */
344void SpaceShip::loadParams(const TiXmlElement* root)
345{
346  Playable::loadParams(root);
[10032]347
348  LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML);
[6868]349}
350
[10032]351
[10017]352void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
[7056]353{
[10032]354  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
[7056]355}
[6868]356
[7085]357void SpaceShip::reset()
358{
[10017]359  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
[7056]360
[10017]361  //xMouse = yMouse = 0;
[7085]362
363  this->setHealth(80);
364  this->velocity = Vector(0.0, 0.0, 0.0);
365}
366
367
[6868]368void SpaceShip::enter()
369{
[10004]370  this->secWeaponMan.showCrosshair();
[10064]371  this->toList( OM_GROUP_01 );
[10004]372  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
[10019]373  //this->attachCamera();
[10032]374 // this->setPlaymode(Playable::Horizontal);
[6868]375}
376
377void SpaceShip::leave()
378{
[10004]379  this->secWeaponMan.hideCrosshair();
[10064]380  this->toList(OM_GROUP_00);
[10004]381  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
[10019]382  //this->detachCamera();
[6868]383}
384
385
386/**
387 *  effect that occurs after the SpaceShip is spawned
388*/
389void SpaceShip::postSpawn ()
390{
391  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
392}
393
394/**
395 *  the action occuring if the spaceship left the game
396*/
397void SpaceShip::leftWorld ()
[10064]398{/*
399  this->emitter->stop();
400  delete this->emitter;
401  delete this->explosionParticles; */
402}
[6868]403
404WorldEntity* ref = NULL;
405/**
406 *  this function is called, when two entities collide
407 * @param entity: the world entity with whom it collides
408 *
409 * Implement behaviour like damage application or other miscellaneous collision stuff in this function
410 */
411void SpaceShip::collidesWith(WorldEntity* entity, const Vector& location)
412{
413}
414
415/**
416 *  draws the spaceship after transforming it.
417*/
418void SpaceShip::draw () const
419{
420  WorldEntity::draw();
421
422  //this->debug(0);
423}
424
425/**
426 *  the function called for each passing timeSnap
427 * @param time The timespan passed since last update
428*/
429void SpaceShip::tick (float time)
430{
[9961]431  // Playable::tick(time);$
[6868]432
[9961]433  // Own Tick Setup, as a different fire routine is used on the weapon manager
[9965]434  this->weaponMan.tick(time);
[9961]435  this->secWeaponMan.tick(time);
436
[10020]437  if( this->systemFailure() )
438    bFire = bSecFire = false;
439
[10037]440  // fire reqeust/release for primary weapons
[9965]441  if( this->bFire)
442    this->weaponMan.fire();
[10036]443  else
444    this->weaponMan.releaseFire();
445
[10037]446  // fire reqeust/release for secondary weapons
[9961]447  if( this->bSecFire)
448    this->secWeaponMan.fire();
[10036]449  else
450    this->secWeaponMan.releaseFire();
[9961]451
[9970]452
453  // Shield Regeneration and other regular calculations on the ship
454  this->regen(time);
455
456  // Weapon Regeneration and other regular calculations on the ship
[9965]457  this->weaponRegen(time);
458
[9970]459  // current engine speed output
460  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
461
[10043]462  // calculation of maxSpeed and acceleration:
463  this->travelSpeed = this->engineSpeedCur * 5;
464  this->acceleration = this->travelSpeed * 2;
[9970]465
[10017]466  /*
[8708]467  if( ( xMouse != 0 || yMouse != 0 ) && this->getOwner() == SharedNetworkData::getInstance()->getHostID() )
[9869]468  {
[6868]469    if (xMouse > controlVelocityX) xMouse = controlVelocityX;
470    else if (xMouse < -controlVelocityX) xMouse = -controlVelocityX;
471    if (yMouse > controlVelocityY) yMouse = controlVelocityY;
472    else if (yMouse < -controlVelocityY) yMouse = -controlVelocityY;
473
474    pitchDir = (Quaternion(xMouse*mouseSensitivity*0.5, Vector(1,0,0)));
475
476    mouseDir *= (Quaternion(-M_PI/4*xMouse*mouseSensitivity, Vector(0,1,0))*Quaternion(-M_PI/4*yMouse*mouseSensitivity*yInvert, Vector(0,0,1))*pitchDir);
477    xMouse = yMouse = 0;
[9869]478  }
[10017]479*/
[6868]480
[9235]481  // spaceship controlled movement fire(bool bF){ this->bFire = bF;}
[7954]482  //if (this->getOwner() == this->getHostID())
[6868]483
[10017]484  //is->calculateVelocity(time);
[6959]485
[6868]486
[10017]487  //vector move = velocity*time;
488
489  /*
[6868]490  //orient the velocity in the direction of the spaceship.
491  travelSpeed = velocity.len();
492  velocity += ((this->getAbsDirX())*travelSpeed-velocity)*airViscosity;
493  velocity = (velocity.getNormalized())*travelSpeed;
[10017]494  */
495  this->movement(time);
496
497   // TRYING TO FIX PNode.
498  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
499  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
500
[10043]501  this->burstEmitter->setEmissionRate(this->cameraSpeed + (velocity.z>0)*velocity.z);
502  this->burstEmitter->setEmissionVelocity(this->cameraSpeed + (velocity.z>0)*velocity.z, travelSpeed *.1);
[6868]503
504  //orient the spaceship in direction of the mouse
[10017]505  /*
[9869]506  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
507  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
[6868]508    this->setAbsDir( rotQuat);
[9869]509  //this->setAbsDirSoft(mouseDir,5);
[10017]510  */
[6868]511
512  // this is the air friction (necessary for a smooth control)
[10017]513  /*
[6868]514  if(travelSpeed >= 120) velocity -= velocity.getNormalized()*travelSpeed*travelSpeed*0.0001;
515  else if (travelSpeed <= 80) velocity -= velocity.getNormalized()*travelSpeed*0.001;
[10017]516  */
[6868]517
518  //other physics (gravity)
519  //if(travelSpeed < 120)
520  //move += Vector(0,-1,0)*60*time + Vector(0,1,0)*travelSpeed/2*time;
521
522  //hoover effect
523  //cycle += time;
524  //this->shiftCoor(Vector(0,1,0)*cos(this->cycle*2.0)*0.02);
525
526  //readjust
527  //if (this->getAbsDirZ().y > 0.1) this->shiftDir(Quaternion(time*0.3, Vector(1,0,0)));
528  //else if (this->getAbsDirZ().y < -0.1) this->shiftDir(Quaternion(-time*0.3, Vector(1,0,0)));
529
530  //SDL_WarpMouse(GraphicsEngine::getInstance()->getResolutionX()/2, GraphicsEngine::getInstance()->getResolutionY()/2);
531
[10017]532  /*
[6868]533  this->shiftCoor(move);
[10017]534  */
[6868]535
[9869]536  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
[6868]537
538}
539
540/**
541 * @todo switch statement ??
542 */
543void SpaceShip::process(const Event &event)
544{
[9961]545  //Playable::process(event);
[6868]546
547  if( event.type == KeyMapper::PEV_LEFT)
[10017]548    this->bLeft = event.bPressed;
[6868]549  else if( event.type == KeyMapper::PEV_RIGHT)
[10017]550    this->bRight = event.bPressed;
[6997]551  else if( event.type == KeyMapper::PEV_FORWARD)
[10017]552    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
[6997]553  else if( event.type == KeyMapper::PEV_BACKWARD)
[10017]554    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
[9961]555  else if( event.type == KeyMapper::PEV_FIRE2)
556    this->bSecFire = event.bPressed;
[9958]557  else if( event.type == KeyMapper::PEV_FIRE1)
[9965]558    this->bFire = event.bPressed;
[10017]559
560
561  /*
[6868]562  else if( event.type == EV_MOUSE_MOTION)
563  {
[9961]564
[6868]565    this->xMouse += event.xRel;
566    this->yMouse += event.yRel;
567  }
[10017]568  */
[6868]569}
570
[9235]571void SpaceShip::destroy( WorldEntity* killer )
[9008]572{
573  PRINTF(0)("spaceship destroy\n");
[10064]574
575  this->explosionParticles = new SpriteParticles(1000);
576  this->explosionParticles->setName("MBoltExplosionParticles");
577  this->explosionParticles->setLifeSpan(.5, .3);
578  this->explosionParticles->setRadius(0.0, 10.0);
579  this->explosionParticles->setRadius(.5, 6.0);
580  this->explosionParticles->setRadius(1.0, 3.0);
581  this->explosionParticles->setColor(0.0, 1,1,0,.9);
582  this->explosionParticles->setColor(0.5, .8,.8,0,.5);
583  this->explosionParticles->setColor(1.0, .8,.8,.7,.0);
584
585  this->emitter = new DotEmitter(100000, 0, 0);
586  this->emitter->setParent(this);
587  this->emitter->setSpread(M_PI,M_PI);
588  this->emitter->setEmissionRate(50.0);
589  this->emitter->setEmissionVelocity(50.0);
590
591  this->emitter->setSystem(this->explosionParticles);
592
593  this->emitter->setSpread(50);
594  this->emitter->setEmissionRate(50.0);
595  this->emitter->setEmissionVelocity(50.0);
596  this->updateNode(0);
597
598  this->toList(OM_DEAD);
[9008]599}
[6868]600
[9008]601void SpaceShip::respawn( )
602{
[10064]603  toList( OM_GROUP_00 );
[9008]604}
[6868]605
606
[9957]607void SpaceShip::damage(float pDamage, float eDamage){
[10064]608  PRINTF(0)("ship hit");
609  printf("Shit hit");
610  if( this->shieldActive) {
[9950]611    if( this->shieldCur > pDamage) {
612      this->shieldCur = this->shieldCur - pDamage;
613    }
614    else { // shield <= pDamage
[9987]615      this->shieldCur -=pDamage;
616      this->shieldActive = false; //shield collapses
[9950]617      pDamage = pDamage - this->shieldCur;
618      if( !this->shieldActive) {
[9957]619        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
[9953]620        this->electronicCur -= eDamage;
[9950]621      }
622    }
623  }
624  else {
625    this->armorCur = this->armorCur - pDamage;
626    this->electronicCur = this->electronicCur - eDamage;
627  }
628  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
[10064]629    this->destroy(this);
[9950]630}
631
[9987]632
[9957]633void SpaceShip::regen(float time){
634  float tmp;
[9963]635  if (this->armorCur != this->armorMax || this->armorRegen != 0){
636    tmp = this->armorCur + this->armorRegen * time;
637    if ( tmp > electronicMax)
638      this->armorCur = this->armorMax;
639    else
640      this->armorCur = tmp;
641  }
642  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
[9970]643    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
[9957]644    if( tmp > shieldMax)
[9953]645      this->shieldCur = this->shieldMax;
646    else
[9957]647      this->shieldCur = tmp;
648    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
[9953]649  }
[9970]650
651  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
652
[9963]653  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
[9957]654    tmp = this->electronicCur + this->electronicRegen * time;
655    if ( tmp > electronicMax)
[9953]656      this->electronicCur = this->electronicMax;
657    else
[9957]658      this->electronicCur = tmp;
[9953]659  }
[9950]660}
[9965]661
[9987]662
[9965]663/**
664 * Weapon regeneration
665 * does not use any reactor capacity, as it wouldn't work in a consistent way.
666 */
667void SpaceShip::weaponRegen(float time)
668{
669  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
670  Weapon* weapon;
[9987]671  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
[9965]672  {
673    weapon = this->weaponMan.getWeapon(i);
674    if( weapon != NULL && weapon->isActive())
675    {
676      weapon->increaseEnergy( energy);
677    }
678
679  }
680  // weaponMan.increaseAmmunition( weapon, energy);
681}
[9975]682
[9987]683
[9975]684void SpaceShip::enterPlaymode(Playable::Playmode playmode)
685{
[10017]686  switch(playmode)
687  {
688    case Playable::Full3D:
689      /*
690      if (State::getCameraNode != NULL)
691      {
692        Vector absCoor = this->getAbsCoor();
693        this->setParent(PNode::getNullParent());
694        this->setAbsCoor(absCoor);
695        State::getCameraNode()->setParentSoft(&this->cameraNode);
696        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
697        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
698        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
699
700      }
701      */
702      //break;
703
[10032]704      break;
705    case Playable::Horizontal:
[10017]706      if (State::getCameraNode != NULL)
707      {
708        this->debugNode(1);
709        this->travelNode->debugNode(1);
710
711        this->travelNode->setAbsCoor(this->getAbsCoor());
712        this->travelNode->updateNode(0.01f);
713
714        this->setParent(this->travelNode);
715        this->setRelCoor(0,0,0);
716
717        State::getCameraNode()->setParentSoft(this->travelNode);
[10032]718        State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
[10017]719        State::getCameraTargetNode()->setParentSoft(this->travelNode);
[10019]720        State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
[10017]721
722        this->debugNode(1);
723        this->travelNode->debugNode(1);
724      }
725      break;
726
727    default:
728      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
729  }
[9975]730}
731
[10017]732/**
733 * @brief calculate the velocity
734 * @param time the timeslice since the last frame
735*/
[9987]736
[9975]737void SpaceShip::movement (float dt)
738{
[10019]739  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
740  //the ship doesn't slide too much.
741  float airCoeff = 2.5;
742  float pi = 3.14;
[10032]743 
[10017]744
[10032]745  switch(this->getPlaymode())
[10017]746  {
[10032]747    case Playable::Horizontal:
[10017]748    {
[10032]749      // these routines will change the travel movement into zero in a short amout of time, if the player
750      // doesn't press any buttons.
751      if (this->velocity.x >= 0)
[10017]752      {
[10032]753        if (this->velocity.x > airCoeff*this->acceleration * dt)
754          this->velocity.x -= airCoeff* this->acceleration * dt;
755        else
756          this->velocity.x = 0;
[10017]757      }
758      else
759      {
[10032]760        if (this->velocity.x < -airCoeff*this->acceleration * dt)
761          this->velocity.x += airCoeff* this->acceleration * dt;
762        else
763          this->velocity.x = 0;
[10017]764      }
[10032]765      if (this->velocity.z >= 0)
[10017]766      {
[10032]767        if (this->velocity.z > airCoeff*this->acceleration * dt)
768          this->velocity.z -= airCoeff* this->acceleration * dt;
769        else
770          this->velocity.z = 0;
[10017]771      }
772      else
773      {
[10032]774        if (this->velocity.z < -airCoeff*this->acceleration * dt)
775          this->velocity.z += airCoeff* this->acceleration * dt;
776        else
777          this->velocity.z = 0;
[10017]778      }
[10032]779   
780      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
781      // and its continuing to slide a bit.
782      Vector oldCoor = this->getRelCoor();
783      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
784      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
785      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
786      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
787   
788      if( this->systemFailure() )
789        bForward = bBackward = bLeft = bRight = false;
790   
791      if( this->bForward )
[10019]792      {
[10032]793        if(this->getRelCoor().x < this->travelDistancePlus.x)
794        {
795          if (this->velocity.x < this->travelSpeed)
796          {
797            this->velocity.x += (airCoeff + 1.0)*this->acceleration*dt;
798          }
799          else
800          {
801            this->velocity.x = this->travelSpeed;
802          }
803        }
804        else
805        {
806          this->velocity.x = 0.0f;
807        }
[10019]808      }
[10032]809   
810      if( this->bBackward )
[10019]811      {
[10032]812        if(this->getRelCoor().x > this->travelDistanceMinus.x)
813        {
814          if (this->velocity.x > -this->travelSpeed)
815          {
816            this->velocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
817          }
818          else
819          {
820            this->velocity.x = -this->travelSpeed;
821          }
822        }
823        else
824        {
825          this->velocity.x = 0.0f;
826        }
[10019]827      }
[10032]828   
829      if( this->bLeft)
[10019]830      {
[10032]831        if(this->getRelCoor().z > this->travelDistanceMinus.y)
832        {
833          if (this->velocity.z > -this->travelSpeed)
834          {
835            this->velocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
836          }
837          else
838          {
839            this->velocity.z = -this->travelSpeed;
840          }
841        }
842        else
843        {
844          this->velocity.z = 0.0f;
845        }
[10043]846        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
[10019]847      }
[10032]848   
849      if( this->bRight)
[10019]850      {
[10032]851        if(this->getRelCoor().z < this->travelDistancePlus.y)
852        {
853          if (this->velocity.z < this->travelSpeed)
854          {
855            this->velocity.z += (airCoeff + 1.0)*this->acceleration*dt;
856          }
857          else
858          {
859            this->velocity.z = this->travelSpeed;
860          }
861        }
862        else
863        {
864          this->velocity.z = 0.0f;
865        }
[10043]866        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
[10019]867      }
[10032]868      if (!this->bRight && !this->bLeft)
869      {
[10043]870        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
[10032]871      }
[10043]872     
[10032]873
874      this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
875      this->shiftCoor (this->velocity * dt);
876
[10043]877    //normalisation of the vectors (vector sum must be <= travelspeed)
878    float xzNorm = sqrt(pow(this->velocity.x, 2) + pow(this->velocity.z, 2));
879    if (xzNorm > this->travelSpeed)
880    {
881      this->velocity.x = this->velocity.x/xzNorm * this->travelSpeed;
882      this->velocity.z = this->velocity.z/xzNorm * this->travelSpeed;
883    }
[10032]884
[10043]885    //this moves camera and ship along the travel path.
886    this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
887   
888    //set new coordinates calculated through key- events.
889    this->shiftCoor (this->velocity * dt);
890    break;
[10019]891    }
[10032]892    case Playable::Vertical:
893      break;
[10017]894    default:
[10032]895      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
[10017]896  }
[9975]897}
[10017]898
[10032]899void SpaceShip::setPlaymodeXML(const std::string& playmode)
900{
901  this->setPlaymode(Playable::stringToPlaymode(playmode));
902}
903
Note: See TracBrowser for help on using the repository browser.