Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/space_ships/human_scout.cc @ 10540

Last change on this file since 10540 was 10516, checked in by patrick, 18 years ago

merged playability back to trunk

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