Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10017 was 10017, checked in by muellmic, 17 years ago

implementing new scroller controls but still probs with camera

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