Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/world_entities/space_ships/space_ship.cc @ 10667

Last change on this file since 10667 was 10667, checked in by rennerc, 17 years ago

ActionboxEnemy

File size: 36.6 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Knecht
13   co-programmer: Silvan Nellen
14
15*/
16
17#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD_ENTITY
18
19#include "executor/executor.h"
20#include "space_ship.h"
21
22#include "util/loading/resource_manager.h"
23
24#include "weapons/test_gun.h"
25#include "weapons/light_blaster.h"
26#include "weapons/medium_blaster.h"
27#include "weapons/heavy_blaster.h"
28#include "weapons/rf_cannon.h"
29#include "weapons/nadion_laser.h"
30#include "weapons/disruptor.h"
31#include "weapons/swarm_launcher.h"
32#include "weapons/spike_thrower.h"
33#include "weapons/acid_launcher.h"
34#include "weapons/boomerang_gun.h"
35#include "weapons/turret.h"
36#include "weapons/cannon.h"
37
38#include "elements/glgui_energywidgetvertical.h"
39#include "glgui_bar.h"
40
41#include "particles/dot_emitter.h"
42#include "particles/emitter_node.h"
43#include "particles/sprite_particles.h"
44#include "effects/trail.h"
45
46#include "effects/wobblegrid.h"
47
48#include "util/loading/factory.h"
49#include "key_mapper.h"
50
51#include "network_game_manager.h"
52#include "shared_network_data.h"
53
54#include "items/power_ups/weapon_power_up.h"
55#include "items/power_ups/param_power_up.h"
56
57#include "graphics_engine.h"
58
59#include "plane.h"
60
61#include "state.h"
62#include "player.h"
63#include "tools/camera.h"
64#include "tools/cameraman.h"
65
66
67#include "util/loading/load_param.h"
68#include "time.h"
69
70#include "track/track.h"
71#include "track/action_box.h"
72
73
74ObjectListDefinition(SpaceShip);
75CREATE_FACTORY(SpaceShip);
76
77#include "script_class.h"
78CREATE_SCRIPTABLE_CLASS(SpaceShip,
79                        addMethod("hasPlayer", Executor0ret<Playable, lua_State*,bool>(&Playable::hasPlayer))
80                        ->addMethod("fire", Executor1<Playable, lua_State*, bool>(&Playable::fire))
81                        ->addMethod("loadModel", Executor2<WorldEntity, lua_State*,const std::string& ,float>(&WorldEntity::loadModel2))
82                        ->addMethod("setName", Executor1<BaseObject, lua_State*,const std::string&>(&BaseObject::setName))
83                        ->addMethod("hide", Executor0<WorldEntity, lua_State*>(&WorldEntity::hide))
84                        ->addMethod("unhide", Executor0<WorldEntity, lua_State*>(&WorldEntity::unhide))
85                        //Coordinates
86                        ->addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
87                        ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
88                        ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
89                        ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
90                        //->addMethod("setCameraSpeed", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraSpeed))
91                        ->addMethod("pause", Executor1<WorldEntity, lua_State*, bool>(&WorldEntity::pauseTrack))
92                        ->addMethod("setCameraDist", Executor1<SpaceShip, lua_State*, float>(&SpaceShip::setCameraDistance))
93                       );
94
95/**
96 *  destructs the spaceship, deletes alocated memory
97 */
98SpaceShip::~SpaceShip ()
99{
100  this->setPlayer(NULL);
101}
102
103/**
104 * loads a Spaceships information from a specified file.
105 * @param fileName the name of the File to load the spaceship from (absolute path)
106 */
107SpaceShip::SpaceShip(const std::string& fileName)
108    : secWeaponMan(this) //,
109    //supportedPlaymodes(Playable::Vertical) ,
110    //playmode(Playable::Vertical)
111{
112  this->init();
113  TiXmlDocument doc(fileName);
114
115  if(!doc.LoadFile())
116  {
117    PRINTF(2)("Loading file %s failed for spaceship.\n", fileName.c_str());
118    return;
119  }
120
121  this->loadParams(doc.RootElement());
122}
123
124/**
125 *  creates a new Spaceship from Xml Data
126 * @param root the xml element containing spaceship data
127
128   @todo add more parameters to load
129*/
130SpaceShip::SpaceShip(const TiXmlElement* root)
131    : secWeaponMan(this) //,
132    //supportedPlaymodes(Playable::Vertical) ,
133    //playmode(Playable::Vertical)
134{
135  this->init();
136  //this->setParentMode(PNODE_REPARENT_DELETE_CHILDREN);
137  if (root != NULL)
138    this->loadParams(root);
139
140}
141
142
143/**
144 * initializes a Spaceship
145 */
146void SpaceShip::init()
147{
148
149  srand(time(0));   //initialize Random Nomber Generator
150
151  //  this->setRelDir(Quaternion(M_PI, Vector(1,0,0)));
152  this->registerObject(this, SpaceShip::_objectList);
153  PRINTF(4)("SPACESHIP INIT\n");
154  this->weaponMan.setParentEntity( this);
155  //weapons:
156
157  this->weaponMan.setParentEntity( this);
158  this->secWeaponMan.setParentEntity( this);
159
160  this->setWMSlotCount(1,8);
161  this->setWMSlotCount(2,6);
162
163  this->createWeaponSlot(1, 0, Vector(3.006, 1.028, .155), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
164  this->createWeaponSlot(1, 1, Vector(3.006, 1.028, -.155), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
165  this->createWeaponSlot(1, 2, Vector(4.03, .063, .876), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
166  this->createWeaponSlot(1, 3, Vector(4.03, -.063, -.876), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
167  this->createWeaponSlot(1, 4, Vector(1.431, -.612, 2.691), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
168  this->createWeaponSlot(1, 5, Vector(1.431, -.612, -2.691), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
169  this->createWeaponSlot(1, 6, Vector(1.431, -.612, 3.254), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
170  this->createWeaponSlot(1, 7, Vector(1.431, -.612, -3.254), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
171
172
173  this->addWeaponToSlot(1, 0, 0, "RFCannon");
174  this->addWeaponToSlot(1, 0, 1, "RFCannon");
175  this->addWeaponToSlot(1, 0, 2, "RFCannon");
176  this->addWeaponToSlot(1, 0, 3, "RFCannon");
177  this->addWeaponToSlot(1, 1, 0, "RFCannon");
178  this->addWeaponToSlot(1, 1, 1, "RFCannon");
179  this->addWeaponToSlot(1, 1, 2, "RFCannon");
180  this->addWeaponToSlot(1, 1, 3, "RFCannon");
181
182  this->addWeaponToSlot(1, 0, 4, "NadionLaser");
183  this->addWeaponToSlot(1, 0, 5, "NadionLaser");
184  this->addWeaponToSlot(1, 2, 4, "NadionLaser");
185  this->addWeaponToSlot(1, 2, 5, "NadionLaser");
186
187  this->addWeaponToSlot(1, 0, 6, "Disruptor");
188  this->addWeaponToSlot(1, 0, 7, "Disruptor");
189  this->addWeaponToSlot(1, 3, 6, "Disruptor");
190  this->addWeaponToSlot(1, 3, 7, "Disruptor");
191
192
193  this->createWeaponSlot(2, 0, Vector(1.5, 3, 0), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
194  this->createWeaponSlot(2, 1, Vector(2.6, 0, 3.0), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
195  this->createWeaponSlot(2, 2, Vector(1.5, 0, -.5), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
196  this->createWeaponSlot(2, 3, Vector(1.5, 0, .5), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
197  this->createWeaponSlot(2, 4, Vector(1.5, 0, .5), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
198  this->createWeaponSlot(2, 5, Vector(1.5, 0, -.5), WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
199
200  this->addWeaponToSlot(2, 0, 2, "SwarmLauncher");
201
202
203  this->weaponMan.changeWeaponConfig(0);
204  this->secWeaponMan.changeWeaponConfig(0);
205
206
207  Playable::weaponConfigChanged();
208
209  setReactor(10);
210  setWeapon(10);
211  setEngine(15);
212
213
214  setEnergyShare(.3,.3,.4);
215  setShield(100, 100, .2, 2);
216  setArmor(100, 100);
217  setElectronic(50, 50, .7, 3.0);
218
219//   this->loadModel("models/spaceships/fighter_redesign9.obj");
220  //this->setVisibiliy(false);
221
222  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
223
224//   this->setHealthMax(shieldMax);
225//   this->setHealth(shieldCur);
226
227  this->travelNode = new PNode();
228
229  // camera - issue
230  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
231  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
232
233  // widget handling
234  /*
235  this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
236  this->electronicWidget->setDisplayedName(std::string(this->getClassName()) + " Electronics:");
237  this->electronicWidget->setSize2D(30,400);
238  this->electronicWidget->setAbsCoor2D(150,200);
239  this->electronicWidget->shiftDir2D(270);
240  this->updateElectronicWidget();
241  this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
242  this->shieldWidget->setDisplayedName(std::string(this->getClassName()) + " Shield:");
243  this->shieldWidget->setSize2D(30,400);
244  this->shieldWidget->setAbsCoor2D(200,200);
245  this->shieldWidget->shiftDir2D(270);
246  this->updateShieldWidget();
247  if (this->hasPlayer())
248  {
249    State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
250    State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
251  }
252  */
253  this->electronicWidget = NULL;
254  this->shieldWidget = NULL;
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
272  this->weaponMan.getFixedTarget()->setParent(this);
273  this->weaponMan.getFixedTarget()->setRelCoor(100000,0,0);
274
275
276  this->secWeaponMan.getFixedTarget()->setParent(this);
277  this->secWeaponMan.getFixedTarget()->setRelCoor(100000,0,0);
278  this->secWeaponMan.setRotationSpeed(0);
279
280  dynamic_cast<Element2D*>(this->weaponMan.getFixedTarget())->setVisibility( false);
281
282
283  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
284  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
285  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
286  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
287  registerVar( new SynchronizeableFloat( &cameraLook, &cameraLook, "cameraLook", PERMISSION_OWNER ) );
288  registerVar( new SynchronizeableFloat( &rotation, &rotation, "rotation", PERMISSION_OWNER ) );
289  registerVar( new SynchronizeableBool( &bFire, &bFire, "bSecFire", PERMISSION_OWNER));
290  registerVar( new SynchronizeableVector( &velocity, &velocity, "velocity", PERMISSION_MASTER_SERVER ) );
291
292  //this->airFriction = 0.5f;
293  //this->travelDistancePlus = Vector2D(38.0, 43.0);
294  //this->travelDistanceMinus = Vector2D(-38.0, -43.0);
295  this->travelDistancePlus = Vector2D(50,50);
296  this->travelDistanceMinus = Vector2D(-50,-50);
297  this->isTravelDistanceInit = true;
298  this->actionWidthPercentage = 1;
299
300  this->cameraSpeed = 40;
301  this->cameraLook = 0.0f;
302  //this->airFriction = 0.0f;
303
304  srand(time(0));  //initaialize RNG
305
306  this->travelNode->debugDraw();
307
308  this->setSupportedPlaymodes(Playable::Horizontal | Playable::Vertical);
309
310  /// FIXME
311//   this->trail = new Trail( 5, 10, .2, this);
312//   this->trail->setTexture( "textures/engine.png");
313//
314//   this->trailL = new Trail( 5, 10, .2, this);
315//   this->trailL->setTexture( "textures/engine.png");
316//
317//   this->trailR = new ( 5, 10, .2, this);
318//   this->trailR->setTexture( "textures/engine.png");
319
320
321  this->toList(OM_GROUP_00);
322
323  //FIXME Just testaddition to show the wobblegrid
324/*
325  this->test  = new Wobblegrid(5);
326  test->setTexture("textures/blub.png");
327
328  test->setAbsCoor( this->getAbsCoor() + Vector(0, 2, 0));
329  test->setParent( this);
330*/
331
332}
333
334
335/**
336 * loads the Settings of a SpaceShip from an XML-element.
337 * @param root the XML-element to load the Spaceship's properties from
338 */
339void SpaceShip::loadParams(const TiXmlElement* root)
340{
341  Playable::loadParams(root);
342
343  LoadParam(root, "playmode", this, SpaceShip, setPlaymodeXML);
344  LoadParam(root, "cameraDistance", this, SpaceShip, setCameraDistance);
345  LoadParam(root, "cameraFovy", this, SpaceShip, setCameraFovy);
346  LoadParam(root, "actionWidthPercentage", this, SpaceShip, setActionWidthPercentage);
347
348  State::getCamera()->setViewMode(Camera::ViewNormal);
349  State::getCameraTargetNode()->setParent(this);
350  State::getCamera()->setParent(this);
351
352  LoadParam(root, "reactor", this, SpaceShip, setReactor);
353  LoadParam(root, "shield", this, SpaceShip, setShield);
354  LoadParam(root, "armor", this, SpaceShip, setArmor);
355  LoadParam(root, "electronic", this, SpaceShip, setElectronic);
356  LoadParam(root, "engine", this, SpaceShip, setEngine);
357  LoadParam(root, "energyshare", this, SpaceShip, setEnergyShare);
358  LoadParam(root, "weapon", this, SpaceShip, setWeapon);
359
360}
361
362
363void SpaceShip::setPlayDirection(const Quaternion& rot, float speed)
364{
365  //this->direction = Quaternion (rot.getHeading(), Vector(0,1,0));
366}
367
368void SpaceShip::reset()
369{
370  bForward = bBackward = bLeft = bRight = bAscend = bDescend = bRollL = bRollR = bFire = bSecFire = false;
371
372  //xMouse = yMouse = 0;
373
374  this->setHealth(80);
375  this->velocity = Vector(0.0, 0.0, 0.0);
376}
377
378
379void SpaceShip::enter()
380{
381  this->secWeaponMan.showCrosshair();
382  this->toList( OM_GROUP_01 );
383  State::getPlayer()->hud().setRadarCenterNode(this->travelNode);
384  State::getPlayer()->hud().setOverlayActive(true);
385  //dynamic_cast <OrxGui::GLGuiEnergyWidgetVertical*> (State::getPlayer()->hud().getArmorWidget())->setDisplayedName("Armor");
386  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( true);
387  //this->attachCamera();
388 // this->setPlaymode(Playable::Horizontal);
389}
390
391void SpaceShip::leave()
392{
393  this->secWeaponMan.hideCrosshair();
394  this->toList( OM_GROUP_00);
395  State::getPlayer()->hud().setOverlayActive(false);
396  State::getCamera()->setEventHandling(true);
397  State::getPlayer()->hud().setRadarCenterNode(NULL);
398  //dynamic_cast<Element2D*>(this->secWeaponMan.getFixedTarget())->setVisibility( false);
399  //this->detachCamera();
400}
401
402
403/**
404 *  effect that occurs after the SpaceShip is spawned
405*/
406void SpaceShip::postSpawn ()
407{
408  if(this->hasPlayer())
409    Playable::postSpawn();
410
411  //setCollision(new CollisionCluster(1.0, Vector(0,0,0)));
412}
413
414/**
415 *  the action occuring if the spaceship left the game
416*/
417void SpaceShip::leftWorld ()
418{
419
420}
421
422WorldEntity* ref = NULL;
423
424/**
425 *  draws the spaceship after transforming it.
426*/
427void SpaceShip::draw () const
428{
429  if( this->entityTrack != NULL && this->isDrawTrack())
430    this->entityTrack->drawGraph();
431
432  WorldEntity::draw();
433
434//   glMatrixMode(GL_MODELVIEW);
435//   glPushMatrix();
436
437//   float matrix[4][4];
438//   glTranslatef (this->getAbsCoor ().x-1, this->getAbsCoor ().y-.2, this->getAbsCoor ().z);
439//   this->getAbsDir().matrix (matrix);
440//   glMultMatrixf((float*)matrix);
441  //glScalef(2.0, 2.0, 2.0);  // no double rescale
442
443
444//   this->trail->draw();
445
446//   glTranslatef(0,0,-.5);
447//   this->trailL->draw();
448
449//   glTranslatef(0,0,1);
450//   this->trailR->draw();
451
452//   glPopMatrix();
453  //this->debug(0);
454}
455
456/**
457 *  the function called for each passing timeSnap
458 * @param time The timespan passed since last update
459*/
460void SpaceShip::tick (float time)
461{
462  // Playable::tick(time);
463
464 // this->test->tick(time);
465
466  // Own Tick Setup, as a different fire routine is used on the weapon manager
467  this->weaponMan.tick(time);
468  this->secWeaponMan.tick(time);
469
470  if( this->systemFailure() )
471    bFire = bSecFire = false;
472
473  // fire reqeust/release for primary weapons
474  if( this->bFire)
475    this->weaponMan.fire();
476  else
477    this->weaponMan.releaseFire();
478
479  // fire reqeust/release for secondary weapons
480  if( this->bSecFire)
481    this->secWeaponMan.fire();
482  else
483    this->secWeaponMan.releaseFire();
484
485  // Tracktick
486  if(this->entityTrack)
487    this->entityTrack->tick(time);
488
489
490  // Shield Regeneration and other regular calculations on the ship
491  this->regen(time);
492
493  // Weapon Regeneration and other regular calculations on the ship
494  this->weaponRegen(time);
495
496  // current engine speed output
497  this->engineSpeedCur = this->engineSpeedBase + this->reactorOutput * this->engineEnergyShare;
498
499  // calculation of maxSpeed and acceleration:
500  this->travelSpeed = this->engineSpeedCur * 5;
501  this->acceleration = this->travelSpeed * 2;
502
503  this->movement(time);
504
505   // TRYING TO FIX PNode.
506  this->cameraNode.setAbsCoorSoft(this->getAbsCoor() + Vector(0.0f, 5.0f, 0.0f), 30.0f);
507  this->cameraNode.setRelDirSoft(this->getAbsDir(), 30.0f);
508
509
510  this->velocity  = (this->getAbsCoor() - this->oldPos) / time;
511  this->oldPos    = this->getAbsCoor();
512
513//FIXME
514//   this->trail->tick(time);
515//   this->trailL->tick(time);
516//   this->trailR->tick(time);
517
518  if (!this->isTravelDistanceInit)
519  {
520    this->updateTravelDistance();
521    //this->isTravelDistanceInit = true;
522  }
523
524  //orient the spaceship in direction of the mouse
525  /*
526  rotQuat = Quaternion::quatSlerp( this->getAbsDir(), mouseDir, 0.5);//fabsf(time)*shipInertia);
527  if (this->getAbsDir().distance(rotQuat) > 0.00000000000001)
528    this->setAbsDir( rotQuat);
529  //this->setAbsDirSoft(mouseDir,5);
530  */
531  /*
532  this->shiftCoor(move);
533  */
534
535  //   PRINTF(0)("id of %s is: %i\n", this->getName(), this->getOMListNumber());
536
537}
538
539/**
540 * @todo switch statement ??
541 */
542void SpaceShip::process(const Event &event)
543{
544  //Playable::process(event);
545
546  if( event.type == KeyMapper::PEV_LEFT)
547    this->bLeft = event.bPressed;
548  else if( event.type == KeyMapper::PEV_RIGHT)
549  {
550    this->bRight = event.bPressed;
551//     printf("ShipCoors: %f , %f, %f \n", this->getAbsCoor().x, this->getAbsCoor().y, this->getAbsCoor().z);
552  }
553  else if( event.type == KeyMapper::PEV_FORWARD)
554  {
555    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
556
557  }
558  else if( event.type == KeyMapper::PEV_BACKWARD)
559    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
560  else if( event.type == KeyMapper::PEV_FIRE2)
561    this->bSecFire = event.bPressed;
562  else if( event.type == KeyMapper::PEV_FIRE1)
563    this->bFire = event.bPressed;
564  else if( event.type == KeyMapper::PEV_NEXT_WEAPON && event.bPressed)
565  {
566    this->nextWeaponConfig();
567  }
568  else if ( event.type == KeyMapper::PEV_PREVIOUS_WEAPON && event.bPressed)
569    this->previousWeaponConfig();
570
571  if (!(State::getCamera()->getEventHandling()))
572  {
573    //PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
574    if( event.type == KeyMapper::PEV_VIEW0)
575    {
576      State::getCamera()->setViewMode(Camera::ViewNormal);
577      State::getCameraTargetNode()->setParent(this);
578      State::getCamera()->setParent(this);
579    }
580    else if( event.type == KeyMapper::PEV_VIEW1)
581    {
582      State::getCamera()->setViewMode(Camera::ViewBehind);
583      State::getCameraTargetNode()->setParent(this);
584      State::getCamera()->setParent(this);
585    }
586    else if( event.type == KeyMapper::PEV_VIEW2)
587    {
588      State::getCamera()->setViewMode(Camera::ViewFront);
589      State::getCameraTargetNode()->setParent(this);
590      State::getCamera()->setParent(this);
591    }
592    else if( event.type == KeyMapper::PEV_VIEW3)
593    {
594      State::getCamera()->setViewMode(Camera::ViewLeft);
595      State::getCameraTargetNode()->setParent(this);
596      State::getCamera()->setParent(this);
597    }
598    else if( event.type == KeyMapper::PEV_VIEW4)
599    {
600      State::getCamera()->setViewMode(Camera::ViewRight);
601      State::getCameraTargetNode()->setParent(this);
602      State::getCamera()->setParent(this);
603    }
604    else if( event.type == KeyMapper::PEV_VIEW5)
605    {
606      State::getCamera()->setViewMode(Camera::ViewTop);
607      State::getCameraTargetNode()->setParent(this->travelNode);
608      State::getCamera()->setParent(this->travelNode);
609    }
610  }
611
612
613  /*
614  else if( event.type == EV_MOUSE_MOTION)
615  {
616
617    this->xMouse += event.xRel;
618    this->yMouse += event.yRel;
619  }
620  */
621}
622
623void SpaceShip::destroy( WorldEntity* killer )
624{
625  if(this->hasPlayer())
626    Playable::destroy( killer);
627
628  PRINTF(5)("spaceship destroy\n");
629
630  EmitterNode* node  = NULL;
631  DotEmitter* emitter = NULL;
632  SpriteParticles*  explosionParticles  = NULL;
633
634  explosionParticles = new SpriteParticles(200);
635  explosionParticles->setName("SpaceShipExplosionParticles");
636  explosionParticles->setLifeSpan(.2, .3);
637  explosionParticles->setRadius(0.0, 10.0);
638  explosionParticles->setRadius(.5, 6.0);
639  explosionParticles->setRadius(1.0, 3.0);
640  explosionParticles->setColor(0.0, 1,1,1,.9);
641  explosionParticles->setColor(0.1,  1,1,0,.9);
642  explosionParticles->setColor(0.5, .8,.4,0,.5);
643  explosionParticles->setColor(1.0, .2,.2,.2,.5);
644
645
646  emitter = new DotEmitter( 2000, 70, 360);
647  //emitter->setSpread( 0, M_2_PI);
648  emitter->setEmissionRate( 200.0);
649  //emitter->setEmissionVelocity( 200.0);
650  //emitter->setSystem( explosionParticles);
651  //emitter->setAbsCoor( this->getAbsCoor());
652
653  node  = new EmitterNode( .1f);
654  node->setupParticle( emitter, explosionParticles);
655  node->setAbsDir( this->getAbsDir());
656  node->setVelocity( this->getVelocity() * .9f);
657  node->setAbsCoor( this->getAbsCoor());
658  if( !node->start())
659    PRINTF(0)("Explosion node not correctly started!");
660/*
661  PNode* node          = new PNode();
662  node->setAbsCoor(this->getAbsCoor());
663  Explosion* explosion = new Explosion();
664  explosion->explode( node, Vector(5,5,5));
665*/
666/*
667  if( this->hasPlayer())
668  {
669        this->setAbsCoor(Vector(-10000,10000,10000));
670        this->hide();
671  }
672  else
673  {*/
674    this->setAbsCoor( this->getAbsCoor() + Vector(100,0,0) + Vector(1,0,0) * VECTOR_RAND(150).dot(Vector(1,0,0)));
675  //}
676
677}
678
679void SpaceShip::respawn( )
680{
681  Playable::respawn();
682}
683
684
685void SpaceShip::damage(float pDamage, float eDamage){
686  PRINTF(5)("ship hit for (%f,%f) \n",pDamage,eDamage);
687
688  if( this->shieldActive) {
689    if( this->shieldCur > pDamage) {
690      this->shieldCur = this->shieldCur - pDamage;
691    }
692    else { // shield <= pDamage
693      this->shieldCur -=pDamage;
694      this->shieldActive = false; //shield collapses
695      pDamage += this->shieldCur;
696      if( !this->shieldActive) {
697        this->armorCur -= pDamage / 2; // remaining damages hits armor at half rate
698        this->electronicCur -= eDamage;
699      }
700    }
701  }
702  else {
703    this->armorCur = this->armorCur - pDamage;
704    this->electronicCur = this->electronicCur - eDamage;
705  }
706  if( this->armorCur <= 0) { /* FIXME implement shipcrash*/ }
707    this->destroy(this);
708
709  updateElectronicWidget();
710  updateShieldWidget();
711
712  this->setHealth( this->armorCur);
713}
714
715
716void SpaceShip::regen(float time){
717  float tmp;
718  if (this->armorCur != this->armorMax || this->armorRegen != 0){
719    tmp = this->armorCur + this->armorRegen * time;
720    if ( tmp > electronicMax)
721      this->armorCur = this->armorMax;
722    else
723      this->armorCur = tmp;
724  }
725  if (this->shieldCur != this->shieldMax || this->shieldRegen != 0){
726    tmp =  this->shieldCur + (this->shieldRegen + this->reactorOutput * this->shieldEnergyShare) * time;
727    if( tmp > shieldMax)
728      this->shieldCur = this->shieldMax;
729    else
730      this->shieldCur = tmp;
731    this->shieldActive = ( this->shieldActive || this->shieldCur > shieldTH);
732
733    updateShieldWidget();
734  }
735
736  this->setHealth( this->shieldCur);      // FIXME currently just to test share system
737
738  if (this->electronicCur != this->electronicMax || this->electronicRegen != 0){
739    tmp = this->electronicCur + this->electronicRegen * time;
740    if ( tmp > electronicMax)
741      this->electronicCur = this->electronicMax;
742    else
743      this->electronicCur = tmp;
744
745    updateElectronicWidget();
746  }
747
748}
749
750
751/**
752 * Weapon regeneration
753 * does not use any reactor capacity, as it wouldn't work in a consistent way.
754 */
755void SpaceShip::weaponRegen(float time)
756{
757  float energy  = ( this->reactorOutput * this->weaponEnergyShare + this->weaponEnergyRegen) * time;
758  Weapon* weapon;
759  for( unsigned int i=0; i < this->weaponMan.getSlotCount(); i++)
760  {
761    weapon = this->weaponMan.getWeapon(i);
762    if( weapon != NULL && weapon->isActive())
763    {
764      weapon->increaseEnergy( energy);
765    }
766
767  }
768  // weaponMan.increaseAmmunition( weapon, energy);
769}
770
771
772void SpaceShip::enterPlaymode(Playable::Playmode playmode)
773{
774  switch(playmode)
775  {
776    case Playable::Full3D:
777      /*
778      if (State::getCameraNode != NULL)
779      {
780        Vector absCoor = this->getAbsCoor();
781        this->setParent(PNode::getNullParent());
782        this->setAbsCoor(absCoor);
783        State::getCameraNode()->setParentSoft(&this->cameraNode);
784        State::getCameraNode()->setRelCoorSoft(-10, 0,0);
785        State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
786        State::getCameraTargetNode()->setRelCoorSoft(100, 0,0);
787
788      }
789      */
790      //break;
791
792      break;
793    case Playable::Horizontal:
794      if (State::getCameraNode != NULL)
795      {
796        this->debugNode(1);
797        this->travelNode->debugNode(1);
798
799        this->travelNode->setAbsCoor(this->getAbsCoor());
800        this->travelNode->updateNode(0.01f);
801
802        this->isTravelDistanceInit = false;
803
804        if(this->entityTrack)
805           this->travelNode->setParent(this->entityTrack->getTrackNode());
806
807        this->setParent(this->travelNode);
808        this->setRelCoor(0,0,0);
809
810        State::getCameraNode()->setParentSoft(this->travelNode);
811        //State::getCameraNode()->setParentSoft(this);
812        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
813        State::getCameraTargetNode()->setParentSoft(this->travelNode);
814        //State::getCameraTargetNode()->setParentSoft(this);
815        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
816        this->setCameraMode(Camera::ViewTop);
817        State::getCamera()->setEventHandling(false);
818        registerEvent(KeyMapper::PEV_VIEW0);
819        registerEvent(KeyMapper::PEV_VIEW1);
820        registerEvent(KeyMapper::PEV_VIEW2);
821        registerEvent(KeyMapper::PEV_VIEW3);
822        registerEvent(KeyMapper::PEV_VIEW4);
823        registerEvent(KeyMapper::PEV_VIEW5);
824
825        State::getCamera()->setParentMode(PNODE_ALL);
826
827        //this->updateTravelDistance();
828
829        this->debugNode(1);
830        this->travelNode->debugNode(1);
831      }
832      break;
833
834   
835    case Playable::Vertical:
836    {
837        this->travelNode->setAbsCoor(this->getAbsCoor());
838        this->travelNode->updateNode(0.01f);
839
840        this->isTravelDistanceInit = false;
841
842        if(this->entityTrack)
843           this->travelNode->setParent(this->entityTrack->getTrackNode());
844
845        this->setParent(this->travelNode);
846        this->setRelCoor(0,0,0);
847
848        State::getCameraNode()->setParentSoft(this->travelNode);
849        //State::getCameraNode()->setParentSoft(this);
850        //State::getCameraNode()->setRelCoorSoft(-0.01, 40, 0);
851        State::getCameraTargetNode()->setParentSoft(this->travelNode);
852        //State::getCameraTargetNode()->setParentSoft(this);
853        //State::getCameraTargetNode()->setRelCoorSoft(0,0,0);
854        //this->setCameraMode(Camera::ViewNormal);
855        State::getCamera()->setEventHandling(false);
856       
857        PRINTF(0)("\n\n\n\n\n\n\n\nSETCAMERA %x\n\n\n\n\n\n\n", State::getCamera());
858        State::getCamera()->setViewMode(Camera::ViewNormal);
859        State::getCameraTargetNode()->setParent(this);
860        State::getCamera()->setParent(this);
861
862       
863       
864        registerEvent(KeyMapper::PEV_VIEW0);
865        registerEvent(KeyMapper::PEV_VIEW1);
866        registerEvent(KeyMapper::PEV_VIEW2);
867        registerEvent(KeyMapper::PEV_VIEW3);
868        registerEvent(KeyMapper::PEV_VIEW4);
869        registerEvent(KeyMapper::PEV_VIEW5);
870
871        State::getCamera()->setParentMode(PNODE_ALL);
872     
873      break;
874    }
875     
876    default:
877      PRINTF(2)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
878  }
879}
880
881/**
882 * @brief calculate the velocity
883 * @param time the timeslice since the last frame
884*/
885
886void SpaceShip::movement (float dt)
887{
888  //by releasing the buttons, the velocity decreases with airCoeff*Acceleration. Should be strong enough so
889  //the ship doesn't slide too much.
890  float airCoeff = 2.5;
891  float pi = 3.14;
892
893  switch(this->getPlaymode())
894  {
895    case Playable::Horizontal:
896    {
897      // these routines will change the travel movement into zero in a short amout of time, if the player
898      // doesn't press any buttons.
899      if (this->travelVelocity.x >= 0)
900      {
901        if (this->travelVelocity.x > airCoeff*this->acceleration * dt)
902          this->travelVelocity.x -= airCoeff* this->acceleration * dt;
903        else
904          this->travelVelocity.x = 0;
905      }
906      else
907      {
908        if (this->travelVelocity.x < -airCoeff*this->acceleration * dt)
909          this->travelVelocity.x += airCoeff* this->acceleration * dt;
910        else
911          this->travelVelocity.x = 0;
912      }
913      if (this->travelVelocity.z >= 0)
914      {
915        if (this->travelVelocity.z > airCoeff*this->acceleration * dt)
916          this->travelVelocity.z -= airCoeff* this->acceleration * dt;
917        else
918          this->travelVelocity.z = 0;
919      }
920      else
921      {
922        if (this->travelVelocity.z < -airCoeff*this->acceleration * dt)
923          this->travelVelocity.z += airCoeff* this->acceleration * dt;
924        else
925          this->travelVelocity.z = 0;
926      }
927
928      // this will evite, that the ship moves outside the travelDistance- borders,when the player releases all buttons
929      // and its continuing to slide a bit.
930      Vector oldCoor = this->getRelCoor();
931      if (this->getRelCoor().x > this->travelDistancePlus.x) this->setRelCoor(this->travelDistancePlus.x, oldCoor.y, oldCoor.z);
932      if (this->getRelCoor().x < this->travelDistanceMinus.x) this->setRelCoor(this->travelDistanceMinus.x, oldCoor.y, oldCoor.z);
933      if (this->getRelCoor().z > this->travelDistancePlus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistancePlus.y);
934      if (this->getRelCoor().z < this->travelDistanceMinus.y) this->setRelCoor(oldCoor.x, oldCoor.y, this->travelDistanceMinus.y);
935
936      if( this->systemFailure() )
937        bForward = bBackward = bLeft = bRight = false;
938
939      if( this->bForward )
940      {
941        //printf("ShipCoorX: %f \n", this->getRelCoor().x);
942        if(this->getRelCoor().x < this->travelDistancePlus.x)
943        {
944          if (this->travelVelocity.x < this->travelSpeed)
945          {
946            this->travelVelocity.x += (airCoeff + 1.0)*this->acceleration*dt;
947          }
948          else
949          {
950            this->travelVelocity.x = this->travelSpeed;
951          }
952        }
953        else
954        {
955          this->travelVelocity.x = 0.0f;
956        }
957      }
958
959      if( this->bBackward )
960      {
961        if(this->getRelCoor().x > this->travelDistanceMinus.x)
962        {
963          if (this->travelVelocity.x > -this->travelSpeed)
964          {
965            this->travelVelocity.x -= (airCoeff + 1.0)*this->acceleration*dt;
966          }
967          else
968          {
969            this->travelVelocity.x = -this->travelSpeed;
970          }
971        }
972        else
973        {
974          this->travelVelocity.x = 0.0f;
975        }
976      }
977
978      if( this->bLeft)
979      {
980        if(this->getRelCoor().z > this->travelDistanceMinus.y)
981        {
982          if (this->travelVelocity.z > -this->travelSpeed)
983          {
984            this->travelVelocity.z -= (airCoeff + 1.0)*this->acceleration*dt;
985          }
986          else
987          {
988            this->travelVelocity.z = -this->travelSpeed;
989          }
990        }
991        else
992        {
993          this->travelVelocity.z = 0.0f;
994        }
995        this->setRelDirSoft(Quaternion(-pi/6, Vector(1,0,0)), 6);
996      }
997
998      if( this->bRight)
999      {
1000        //printf("ShipCoorZ: %f \n", this->getRelCoor().z);
1001        if(this->getRelCoor().z < this->travelDistancePlus.y)
1002        {
1003          if (this->travelVelocity.z < this->travelSpeed)
1004          {
1005            this->travelVelocity.z += (airCoeff + 1.0)*this->acceleration*dt;
1006          }
1007          else
1008          {
1009            this->travelVelocity.z = this->travelSpeed;
1010          }
1011        }
1012        else
1013        {
1014          this->travelVelocity.z = 0.0f;
1015        }
1016        this->setRelDirSoft(Quaternion(pi/6, Vector(1,0,0)), 6);
1017      }
1018      if (!this->bRight && !this->bLeft)
1019      {
1020        this->setRelDirSoft(Quaternion(0, Vector(1,0,0)), 6);
1021      }
1022
1023    //normalisation of the vectors (vector sum must be <= travelspeed)
1024    float xzNorm = sqrt(pow(this->travelVelocity.x, 2) + pow(this->travelVelocity.z, 2));
1025    if (xzNorm > this->travelSpeed)
1026    {
1027      this->travelVelocity.x = this->travelVelocity.x/xzNorm * this->travelSpeed;
1028      this->travelVelocity.z = this->travelVelocity.z/xzNorm * this->travelSpeed;
1029    }
1030
1031    //this moves camera and ship along the travel path.
1032    if(!this->entityTrack)
1033       this->travelNode->shiftCoor(Vector(this->cameraSpeed * dt, 0, 0));
1034
1035    break;
1036    }
1037    case Playable::Vertical:
1038    {
1039      if ( !entityTrack || !entityTrack->getActionBox() )
1040      {
1041        break;
1042      }
1043      ActionBox * box = entityTrack->getActionBox();
1044     
1045      this->travelVelocity = Vector(0, 0, 0);
1046      float ssss = 50;
1047      if ( this->bForward )
1048      {
1049        this->travelVelocity += Vector( 0, ssss, 0 );
1050      }
1051      if ( this->bBackward )
1052      {
1053        this->travelVelocity += Vector( 0, -ssss, 0 );
1054      }
1055      if ( this->bLeft )
1056      {
1057        this->travelVelocity += Vector( 0, 0, -ssss );
1058      }
1059      if ( this->bRight )
1060      {
1061        this->travelVelocity += Vector( 0, 0, ssss );
1062      }
1063     
1064      Vector ds = this->travelVelocity*dt;
1065      Vector newPos = this->getRelCoor() + ds;
1066      if ( newPos.y < -(box->getHeight_2()) || newPos.y > box->getHeight_2() )
1067      {
1068        this->travelVelocity.y = 0;
1069      }
1070      if ( newPos.z > box->getWidth_2() || newPos.z < -(box->getWidth_2()) )
1071      {
1072        this->travelVelocity.z = 0;
1073      }
1074    }
1075      break;
1076    default:
1077      PRINTF(4)("Playmode %s Not Implemented in %s\n", Playable::playmodeToString(this->getPlaymode()).c_str(), this->getClassCName());
1078  }
1079   //set new coordinates calculated through key- events.
1080  this->shiftCoor (this->travelVelocity * dt);
1081}
1082
1083void SpaceShip::setPlaymodeXML(const std::string& playmode)
1084{
1085  this->setPlaymode(Playable::stringToPlaymode(playmode));
1086}
1087
1088/**
1089 * @brief jumps to the next WeaponConfiguration
1090 */
1091void SpaceShip::nextWeaponConfig()
1092{
1093  PRINTF(0)("Requested next weapon config!\n");
1094  this->weaponMan.nextWeaponConfig();
1095  Playable::weaponConfigChanged();
1096}
1097
1098/**
1099 * @brief moves to the last WeaponConfiguration
1100 */
1101void SpaceShip::previousWeaponConfig()
1102{
1103  /*
1104  this->curWeaponPrimary    = (this->curWeaponPrimary + 1) % 3;
1105  this->weaponMan.changeWeaponConfig(this->curWeaponPrimary);
1106  */
1107  this->weaponMan.previousWeaponConfig();
1108  Playable::weaponConfigChanged();
1109}
1110
1111void SpaceShip::hit( float damage, WorldEntity* killer)
1112{
1113  this->damage(killer->getDamage(),0);
1114}
1115
1116void SpaceShip::updateElectronicWidget()
1117{
1118  if (this->electronicWidget != NULL)
1119  { //if it exists already: update it
1120     this->electronicWidget->setMaximum(this->electronicMax);
1121     this->electronicWidget->setValue(this->electronicCur);
1122  }
1123  else
1124  { //create the widget
1125    this->electronicWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1126    this->electronicWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1127    //this->electronicWidget->setDisplayedName("Electronics:");
1128    //this->electronicWidget->setSize2D(100,20);
1129    //this->electronicWidget->setAbsCoor2D(150,200);
1130    this->updateElectronicWidget();
1131    if (this->hasPlayer())
1132      State::getPlayer()->hud().setEnergyWidget(this->electronicWidget);
1133  }
1134}
1135
1136void SpaceShip::updateShieldWidget()
1137{
1138  if (this->shieldWidget != NULL)
1139  {
1140    this->shieldWidget->setMaximum(this->shieldMax);
1141    this->shieldWidget->setValue(this->shieldCur);;
1142  }
1143  else
1144  {
1145    this->shieldWidget = new OrxGui::GLGuiEnergyWidgetVertical();
1146    this->shieldWidget->getBarWidget()->setChangedValueColor(Color(1,0,0,1));
1147    //this->shieldWidget->setDisplayedName("Shield:");
1148    //his->shieldWidget->setSize2D(100,20);
1149    //this->shieldWidget->setAbsCoor2D(200,200);
1150    this->updateShieldWidget();
1151    if (this->hasPlayer())
1152      State::getPlayer()->hud().setShiledWidget(this->shieldWidget);
1153  }
1154}
1155
1156void SpaceShip::setCameraDistance(float dist)
1157{
1158  Camera* c = State::getCamera();
1159  c->setViewTopDistance(dist);
1160
1161  if (this->hasPlayer())
1162    this->isTravelDistanceInit = false;
1163}
1164
1165void SpaceShip::setCameraFovy(float fovy)
1166{
1167
1168  Camera* c = State::getCamera();
1169  c->setViewTopFovy(fovy);
1170
1171  if (this->hasPlayer())
1172    this->isTravelDistanceInit = false;
1173}
1174
1175void SpaceShip::updateTravelDistance()
1176{
1177
1178  Camera* c = State::getCamera();
1179
1180  float x = 1.25 * this->actionWidthPercentage * fabsf(c->getAbsCoor().y) * tan(c->getFovy()*M_PI /360.0);
1181  float y = x / c->getAspectRatio() / this->actionWidthPercentage;
1182  //State::getCamera()->setAbsCoor(-5, 1000, 0);
1183
1184
1185  //State::getCamera()->getAbsCoor().print();
1186  //printf("CameraRelCoorY: %f \n", State::getCamera()->getRelCoor().y);
1187
1188  //printf("x: %f, y: %f \n", x, y);
1189  this->travelDistancePlus = Vector2D(y, x);
1190  this->travelDistanceMinus = Vector2D(-y, -x);
1191
1192  State::getPlayer()->hud().setOverlayPercentage(100-int(100*this->actionWidthPercentage));
1193//   PRINTF(0)("TravelDistance has been updated\n");
1194  this->isTravelDistanceInit = true;
1195}
1196
1197void SpaceShip::setActionWidthPercentage(int i)
1198{
1199  if (i>100) i=100;
1200  if (i<0) i=0;
1201  this->actionWidthPercentage = i/100.0;
1202
1203  if (this->hasPlayer())
1204    this->isTravelDistanceInit = false;
1205};
1206
1207void SpaceShip::addWeaponToSlot(int wm, int config, int slot, const std::string& weaponName){
1208  if (wm == 1) { this->weaponMan.addWeapon( Weapon::createWeapon( weaponName ), config, slot); }
1209  if (wm == 1) { this->weaponMan.addWeapon( Weapon::createWeapon( weaponName ), config, slot); }
1210  else return;
1211}
1212
Note: See TracBrowser for help on using the repository browser.