Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/creatures/fps_player.cc @ 10751

Last change on this file since 10751 was 10748, checked in by bknecht, 17 years ago

fixed stupid gui segfault

File size: 18.0 KB
Line 
1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Patrick Boenzli
13   co-programmer: ...
14
15*/
16
17#include "fps_player.h"
18
19#include "interactive_model.h"
20#include "state.h"
21#include "tools/camera.h"
22#include "player.h"
23
24#include "src/lib/util/loading/factory.h"
25
26#include "md2/md2Model.h"
27
28#include "weapons/weapon_manager.h"
29#include "weapons/test_gun.h"
30#include "weapons/turret.h"
31#include "weapons/cannon.h"
32#include "weapons/fps_sniper_rifle.h"
33#include "weapons/aiming_system.h"
34
35#include "aabb.h"
36#include "environments/bsp_entity.h"
37
38#include "key_mapper.h"
39
40#include "debug.h"
41
42#include "shared_network_data.h"
43
44#include "event_handler.h"
45
46#include "story_entity.h"
47
48
49ObjectListDefinition(FPSPlayer);
50CREATE_FACTORY(FPSPlayer);
51
52#include "script_class.h"
53CREATE_SCRIPTABLE_CLASS(FPSPlayer,
54                        addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
55                            ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
56                            ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
57                            ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
58                            ->addMethod("displayHUDText", Executor1<FPSPlayer, lua_State*, const std::string&>(&FPSPlayer::displayHUDText))
59                       );
60
61
62/**
63 *  destructs the FPSPlayer, deletes alocated memory
64 */
65FPSPlayer::~FPSPlayer ()
66{
67  this->setPlayer(NULL);
68}
69
70
71/**
72 *  creates a new FPSPlayer from Xml Data
73 * @param root the xml element containing FPSPlayer data
74 *
75 */
76FPSPlayer::FPSPlayer(const TiXmlElement* root)
77{
78  if (root != NULL)
79    this->loadParams(root);
80
81    this->updateNode(0.001);
82    this->init();
83}
84
85
86/**
87 * initializes a FPSPlayer
88 */
89void FPSPlayer::init()
90{
91  this->registerObject(this, FPSPlayer::_objectList);
92
93  this->bLeft = false;
94  this->bRight = false;
95  this->bForward = false;
96  this->bBackward = false;
97  this->bJump = false;
98  this->bPosBut = false;
99  this->bFire = false;
100  this->bFire2 = false;
101  this->changeZoom = true;
102  this->inZoomMode = false;
103  this->changingZoom = false;
104
105  this->xMouse = 0.0f;
106  this->yMouse = 0.0f;
107
108  this->setHealthMax(100);
109  this->setHealth(80);
110
111  this->fallVelocity = 0.0f;
112  this->jumpAcceleration = 0.0f;
113
114  this->cameraNode = new PNode();
115  this->cameraNode->setParent(this);
116
117  this->attitude = this->getAbsDir().getAttitude();
118  this->heading = this->getAbsDir().getHeading();
119
120  //add events to the eventlist
121  registerEvent(KeyMapper::PEV_FORWARD);
122  registerEvent(KeyMapper::PEV_BACKWARD);
123  registerEvent(KeyMapper::PEV_LEFT);
124  registerEvent(KeyMapper::PEV_RIGHT);
125  registerEvent(KeyMapper::PEV_FIRE1);
126  registerEvent(KeyMapper::PEV_FIRE2);
127  registerEvent(KeyMapper::PEV_JUMP);
128  registerEvent(KeyMapper::PEV_CROUCH);
129  registerEvent(KeyMapper::PEV_FIRE1);
130  registerEvent(EV_MOUSE_MOTION);
131 
132  this->deadBox = NULL;
133
134  // weapon manager for the fps
135  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
136
137 
138
139  //this->aimingSystem = new AimingSystem(this);
140
141#if 1
142  this->getWeaponManager().changeWeaponConfig(1);
143  this->getWeaponManager().setSlotCount(1);
144  //this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1)));
145  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
146  /*this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
147  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
148  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));*/
149
150  this->getWeaponManager().setParentNode(this->cameraNode);
151  this->cameraNode->addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
152
153  this->getWeaponManager().setRotationSpeed(0);
154  this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
155  this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
156  this->getWeaponManager().getFixedTarget()->setRelCoor(10,0,0);
157 
158  if( true /*State::isOnline()*/ )
159  {
160    weapon = new FPSSniperRifle(0);
161    weapon->setName("testGun Right");
162    this->addWeapon(weapon,1, 0);
163    weapon->toList( this->getOMListNumber() );
164    weapon->setParent( this->cameraNode );
165    weapon->setForwardDamageToParent( true );
166    //wpRight->requestAction( WA_ACTIVATE );
167    //wpRight->addChild(this->aimingSystem);
168
169    //this->toList( OM_PLAYERS );
170  }
171#else
172                         
173  FPSSniperRifle* wpRight = new FPSSniperRifle(0);
174  wpRight->setName("testGun Right");
175  wpRight->toList( this->getOMListNumber() );
176  wpRight->setParent( &this->cameraNode );
177 
178  this->weaponMan.setParentEntity( this );
179  this->weaponMan.setSlotCount(2);
180  this->weaponMan.createWeaponSlot(0, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
181  //this->weaponMan.createWeaponSlot(1, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
182  this->weaponMan.addWeapon(wpRight, 0, 0);
183  this->weaponMan.changeWeaponConfig(0);
184  Playable::weaponConfigChanged();
185  this->weaponMan.getFixedTarget()->setParent(&this->cameraNode );
186  this->weaponMan.getFixedTarget()->setRelCoor( 100000,0,0 );
187#endif
188
189  // network registration
190  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
191  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
192  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
193  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
194  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
195  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
196  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
197
198    //subscribe to collision reaction
199  this->subscribeReaction(CoRe::CREngine::CR_PHYSICS_FULL_WALK, BspEntity::staticClassID());
200
201  this->initWeapon = false;
202  this->damageTicker = 0.0f;
203
204}
205
206
207/**
208 * loads the Settings of a FPSPlayer from an XML-element.
209 * @param root the XML-element to load the Spaceship's properties from
210 */
211void FPSPlayer::loadParams(const TiXmlElement* root)
212{
213  Playable::loadParams(root);
214}
215
216void FPSPlayer::hit(float damage, WorldEntity* killer)
217{
218        WorldEntity::hit(damage, killer);
219        if (this->hasPlayer())
220                this->getCurrentPlayer()->hud().getHit();
221}
222
223/**
224 * was probabably designed for setting direction of FPSPlayer
225 * but hey, this connot work like this, can it?
226 */
227void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
228{
229  this->attitude = this->getAbsDir().getAttitude();
230  this->heading = this->getAbsDir().getHeading();
231}
232
233/**
234 * Resets FPSPlayer stats and freezes its moving directions
235 *
236 */
237void FPSPlayer::reset()
238{
239  this->bLeft = false;
240  this->bRight = false;
241  this->bForward = false;
242  this->bBackward = false;
243  this->xMouse = 0.0f;
244  this->yMouse = 0.0f;
245  this->inZoomMode = false;
246
247  this->setHealth(80);
248}
249
250/**
251 * Defines what happens to camera and other important elements when changing
252 * into FPS-view
253 */
254void FPSPlayer::enter()
255{
256  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
257
258  State::getCameraNode()->setParentSoft(this->cameraNode);
259  State::getCameraTargetNode()->setParentSoft(this->cameraNode);
260 
261  State::getCamera()->setViewMode(Camera::ViewFPS);
262 
263  this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
264  //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
265  //this->getWeaponManager().getFixedTarget()->setRelCoor(100,0,0);
266
267  if ( !State::isOnline() )
268  {
269    this->respawn();
270  }
271}
272
273/**
274 * Defines what happens if active player leaves FPSPlayer
275 * (basicly hides crosshair and frees camera)
276 */
277void FPSPlayer::leave()
278{
279  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
280  this->detachCamera();
281}
282
283
284
285/**
286 *  the function called for each passing timeSnap
287 * @param time The timespan passed since last update
288 */
289void FPSPlayer::tick (float time)
290{
291  if ( deadBox != NULL )
292  {
293    OrxGui::GLGuiHandler::getInstance()->tick( time );
294  }
295     
296  // Second init-step
297  if ( !this->initWeapon )
298  {
299    this->initWeapon = true;
300
301    this->cameraNode->setParentMode(PNODE_ROTATE_AND_MOVE);
302
303    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
304    this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
305    //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
306    State::getCamera()->setViewMode(Camera::ViewFPS);
307
308
309  }
310  // end of second init-step
311 
312  // This box represents the dimension of the used model
313  AABB* box = this->getModelAABB();
314
315  if( box != NULL)
316  {
317      float f = 1.0;
318      if( this->bCrouch )
319          f = 0.3*f;
320     
321      this->cameraNode->setRelCoor(0, box->halfLength[1] * f, 0);
322//      this->weaponMan.setRelCoor(0, box->halfLength[1] * f, 0);
323//      this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
324//      float v = 0.1f; // unused variable
325     //this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
326     //this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
327     //this->getWeaponManager().setSlotDirection( 0, Quaternion( 0.04, Vector( 0, 0, 1 ) ) );
328  }
329 
330 
331  if( this->bFire2 )
332  {
333     
334      // to change Zoom on click
335      if( this->changeZoom )
336      {
337          this->changeZoom = false;
338          if( this->inZoomMode )
339          {
340              State::getCamera()->setViewMode(Camera::ViewFPS);
341              this->inZoomMode = false;
342          }
343          else
344          {
345              State::getCamera()->setViewMode(Camera::ViewFPSZoom);
346              this->inZoomMode = true;
347          }
348         
349      }
350     
351  }
352  else
353  {
354      this->changeZoom = true;
355  }
356  /*if( this->bFire2 )
357  {
358      if( this->changeZoom )
359      {
360          if( !this->inZoomMode || this->changingZoom )
361          {
362              this->inZoomMode = true;
363              this->changingZoom = true;
364              float fovy = State::getCamera()->getFovy();
365              if( fovy > 30 )
366                  State::getCamera()->setFovy( fovy - 10*time );
367          }
368          else
369          {
370                State::getCamera()->setViewMode(Camera::ViewFPS);
371                this->inZoomMode = false;
372                this->changeZoom = false;
373          }
374      }
375  }
376  else
377  {
378      this->changeZoom = true;
379      this->changingZoom = false;
380  }*/
381
382  /*this->getWeaponManager().tick(time);
383  if( this->bFire)
384  {
385    this->getWeaponManager().fire();
386  }*/
387  Playable::tick(time);
388
389
390  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
391  {
392    xMouse *= time ;
393    yMouse *= time ;
394   
395    float amount;
396   
397    if( this->inZoomMode )
398        amount = 2.;
399    else
400        amount = 5.;
401
402    heading -= xMouse/amount;
403    attitude-= yMouse/amount;
404
405
406    if ( attitude > 1.95 )
407      attitude = 1.95;
408    else if ( attitude < -1.07 )
409      attitude = -1.07;
410
411    xMouse = yMouse = 0;
412  }
413
414  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
415  this->cameraNode->setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
416
417  Vector velocity;
418
419  if ( this->bForward )
420  {
421    velocity += this->getAbsDirX();
422  }
423
424  if ( this->bBackward )
425  {
426    velocity -= this->getAbsDirX();
427  }
428
429  if ( this->bRight )
430  {
431    velocity += this->getAbsDirZ();
432  }
433
434  if ( this->bLeft )
435  {
436    velocity -= this->getAbsDirZ();
437  }
438
439  // Uncomment this if you want your current position to be prined to the console when you press the jump button
440  /* if( this->bJump)
441    {
442      printf("panicGuy:runTo( %f, %f, %f ) \n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
443      this->bJump = false;
444    }*/
445
446  int speed;
447  if( this->bCrouch )
448      speed = 50;
449  else
450      speed = 100;
451
452  velocity.normalize();
453  velocity *= speed;
454
455  if( this->getModel( 0) != NULL && this->getModel(0)->isA(InteractiveModel::staticClassID()))
456  {
457      if( this->bJump)
458      {
459          if( this->jumpAcceleration < 1.0f)
460          {
461              this->jumpAcceleration = 300.0f;
462
463              if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
464                  ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
465          }
466       }
467       else if(velocity.len() != 0.0f)
468       {
469            if( this->bCrouch )
470            {
471                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_WALK)
472                    ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_WALK);
473            }
474            else
475            {
476                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
477                    ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); 
478            }
479                 
480       }
481       else
482       {
483           if( this->bCrouch )
484           {
485               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_STAND)
486                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_STAND);
487           }
488           else
489           {
490               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
491                   ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
492           }
493       }
494       
495       if( this->bFire )
496       {
497           if( this->bCrouch )
498           {
499               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_ATTACK)
500                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_ATTACK);
501           }
502           else
503           {
504               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != ATTACK)
505                   ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
506           }
507       }
508  }
509
510
511  velocity.y += this->jumpAcceleration;
512  if( this->jumpAcceleration > 1.0f)
513    this->jumpAcceleration *= pow(0.9f,time*100);
514
515
516  // physical falling of the player
517  if( !this->isOnGround())
518  {
519    if(this->fallVelocity + 300.0F*time < 10000.0f)this->fallVelocity += 300.0f * time;
520    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
521
522//     PRINTF(0)("vel %f\n", this->fallVelocity);
523  }
524  else
525  {
526    this->fallVelocity = 0.0f;
527  }
528  if((velocity * time).len() < 10.0f) this->shiftCoor( velocity*time );
529  else
530  {     
531         velocity.normalize();
532         velocity *= 10.0f;
533         this->shiftCoor( velocity );
534  }
535
536
537
538  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(InteractiveModel::staticClassID()))
539  {
540    ((InteractiveModel*)this->getModel(0))->tick(time);
541  }
542
543  this->setOnGround(false);
544   
545}
546
547
548
549/**
550 *  draws the MD2Creature after transforming it.
551 */
552void FPSPlayer::draw () const
553{
554  // only draw if this entity is not the player since the player nevers sees himself
555  if( this->getCurrentPlayer() == NULL)
556    WorldEntity::draw();
557}
558
559
560
561/**
562 * checks events
563 */
564void FPSPlayer::process(const Event &event)
565{
566  Playable::process(event);
567
568  if( event.type == KeyMapper::PEV_LEFT)
569    this->bLeft = event.bPressed;
570  else if( event.type == KeyMapper::PEV_RIGHT)
571    this->bRight = event.bPressed;
572  else if( event.type == KeyMapper::PEV_FORWARD)
573    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
574  else if( event.type == KeyMapper::PEV_BACKWARD)
575    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
576  else if( event.type == KeyMapper::PEV_JUMP)
577    this->bJump = event.bPressed;
578  else if( event.type == KeyMapper::PEV_FIRE1)
579    this->bFire = event.bPressed;
580  else if( event.type == KeyMapper::PEV_FIRE2)
581    this->bFire2 = event.bPressed;
582  else if( event.type == KeyMapper::PEV_CROUCH)
583    this->bCrouch = event.bPressed;
584  else if( event.type == EV_MOUSE_MOTION)
585  {
586    this->xMouse += event.xRel;
587    this->yMouse += event.yRel;
588  }
589}
590
591/**
592 * respawns FPSplayer
593 */
594void FPSPlayer::respawn( )
595{
596  if( State::isOnline())
597    toList( OM_PLAYERS );
598
599  this->damageTicker = 0.0f;
600
601  Playable::respawn();
602}
603
604/**
605 * Kills the FPSplayer defining its killer
606 */
607void FPSPlayer::destroy( WorldEntity* killer )
608{
609  Playable::destroy( killer );
610  this->showDeadScreen();
611  myList = this->getOMListNumber();
612  toList( OM_DEAD );
613}
614
615void FPSPlayer::displayHUDText( const std::string& message )
616{
617        State::getPlayer()->hud().notifyUser(message);
618}
619
620void FPSPlayer::onButtonContinue( )
621{
622  OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true );
623  EventHandler::getInstance()->popState();
624  WorldEntity::reset();
625  toList( myList );
626  deadBox->hideAll();
627}
628
629void FPSPlayer::onButtonExit( )
630{
631  State::getCurrentStoryEntity()->setNextStoryID( 0 );
632  State::getCurrentStoryEntity()->stop();
633}
634
635void FPSPlayer::showDeadScreen( )
636{
637  EventHandler::getInstance()->pushState( ES_MENU );
638
639  OrxGui::GLGuiHandler::getInstance()->activateCursor();
640 
641  if ( deadBox )
642    delete deadBox;
643  deadBox = new OrxGui::GLGuiBox();
644  deadBox->setAbsCoor2D( 100, 100 );
645
646  OrxGui::GLGuiText* text = new OrxGui::GLGuiText();
647  text->setText( "Game Over! - You died!" );
648  OrxGui::GLGuiPushButton* exitButton = new OrxGui::GLGuiPushButton( "exit" );
649  exitButton->released.connect(this, &FPSPlayer::onButtonExit);
650  OrxGui::GLGuiPushButton* continueButton = new OrxGui::GLGuiPushButton( "continue" );
651  continueButton->released.connect(this, &FPSPlayer::onButtonContinue);
652
653  deadBox->pack( text );
654  deadBox->pack( continueButton );
655  deadBox->pack( exitButton );
656
657  deadBox->showAll();
658}
Note: See TracBrowser for help on using the repository browser.