Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

dead gui :D

File size: 17.8 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().getFixedTarget()->setParent(this->cameraNode);
154  this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
155  this->getWeaponManager().getFixedTarget()->setRelCoor(10,0,0);
156 
157  if( true /*State::isOnline()*/ )
158  {
159    weapon = new FPSSniperRifle(0);
160    weapon->setName("testGun Right");
161    this->addWeapon(weapon,1, 0);
162    weapon->toList( this->getOMListNumber() );
163    weapon->setParent( this->cameraNode );
164    weapon->setForwardDamageToParent( true );
165    //wpRight->requestAction( WA_ACTIVATE );
166    //wpRight->addChild(this->aimingSystem);
167
168    //this->toList( OM_PLAYERS );
169  }
170#else
171                         
172  FPSSniperRifle* wpRight = new FPSSniperRifle(0);
173  wpRight->setName("testGun Right");
174  wpRight->toList( this->getOMListNumber() );
175  wpRight->setParent( &this->cameraNode );
176 
177  this->weaponMan.setParentEntity( this );
178  this->weaponMan.setSlotCount(2);
179  this->weaponMan.createWeaponSlot(0, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
180  //this->weaponMan.createWeaponSlot(1, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
181  this->weaponMan.addWeapon(wpRight, 0, 0);
182  this->weaponMan.changeWeaponConfig(0);
183  Playable::weaponConfigChanged();
184  this->weaponMan.getFixedTarget()->setParent(&this->cameraNode );
185  this->weaponMan.getFixedTarget()->setRelCoor( 100000,0,0 );
186#endif
187
188  // network registration
189  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
190  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
191  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
192  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
193  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
194  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
195  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
196
197    //subscribe to collision reaction
198  this->subscribeReaction(CoRe::CREngine::CR_PHYSICS_FULL_WALK, BspEntity::staticClassID());
199
200  this->initWeapon = false;
201  this->damageTicker = 0.0f;
202
203}
204
205
206/**
207 * loads the Settings of a FPSPlayer from an XML-element.
208 * @param root the XML-element to load the Spaceship's properties from
209 */
210void FPSPlayer::loadParams(const TiXmlElement* root)
211{
212  Playable::loadParams(root);
213}
214
215/**
216 * was probabably designed for setting direction of FPSPlayer
217 * but hey, this connot work like this, can it?
218 */
219void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
220{
221  this->attitude = this->getAbsDir().getAttitude();
222  this->heading = this->getAbsDir().getHeading();
223}
224
225/**
226 * Resets FPSPlayer stats and freezes its moving directions
227 *
228 */
229void FPSPlayer::reset()
230{
231  this->bLeft = false;
232  this->bRight = false;
233  this->bForward = false;
234  this->bBackward = false;
235  this->xMouse = 0.0f;
236  this->yMouse = 0.0f;
237  this->inZoomMode = false;
238
239  this->setHealth(80);
240}
241
242/**
243 * Defines what happens to camera and other important elements when changing
244 * into FPS-view
245 */
246void FPSPlayer::enter()
247{
248  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
249
250  State::getCameraNode()->setParentSoft(this->cameraNode);
251  State::getCameraTargetNode()->setParentSoft(this->cameraNode);
252 
253  State::getCamera()->setViewMode(Camera::ViewFPS);
254 
255  this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
256  //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
257  //this->getWeaponManager().getFixedTarget()->setRelCoor(100,0,0);
258
259  if ( !State::isOnline() )
260  {
261    this->respawn();
262  }
263}
264
265/**
266 * Defines what happens if active player leaves FPSPlayer
267 * (basicly hides crosshair and frees camera)
268 */
269void FPSPlayer::leave()
270{
271  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
272  this->detachCamera();
273}
274
275
276
277/**
278 *  the function called for each passing timeSnap
279 * @param time The timespan passed since last update
280 */
281void FPSPlayer::tick (float time)
282{
283  if ( deadBox != NULL )
284  {
285    OrxGui::GLGuiHandler::getInstance()->tick( time );
286  }
287     
288  // Second init-step
289  if ( !this->initWeapon )
290  {
291    this->initWeapon = true;
292
293    this->cameraNode->setParentMode(PNODE_ROTATE_AND_MOVE);
294
295    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
296    this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
297    //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
298    State::getCamera()->setViewMode(Camera::ViewFPS);
299
300
301  }
302  // end of second init-step
303 
304  // This box represents the dimension of the used model
305  AABB* box = this->getModelAABB();
306
307  if( box != NULL)
308  {
309      float f = 1.0;
310      if( this->bCrouch )
311          f = 0.3*f;
312     
313      this->cameraNode->setRelCoor(0, box->halfLength[1] * f, 0);
314//      this->weaponMan.setRelCoor(0, box->halfLength[1] * f, 0);
315//      this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
316//      float v = 0.1f; // unused variable
317     //this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
318     //this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
319     //this->getWeaponManager().setSlotDirection( 0, Quaternion( 0.04, Vector( 0, 0, 1 ) ) );
320  }
321 
322  /*
323  if( this->bFire2 )
324  {
325     
326      // to change Zoom on click
327      if( this->changeZoom )
328      {
329          this->changeZoom = false;
330          if( this->inZoomMode )
331          {
332              State::getCamera()->setViewMode(Camera::ViewFPS);
333              this->inZoomMode = false;
334          }
335          else
336          {
337              State::getCamera()->setViewMode(Camera::ViewFPSZoom);
338              this->inZoomMode = true;
339          }
340         
341      }
342     
343  }
344  else
345  {
346      this->changeZoom = true;
347  }*/
348  if( this->bFire2 )
349  {
350      if( this->changeZoom )
351      {
352          if( !this->inZoomMode || this->changingZoom )
353          {
354              this->inZoomMode = true;
355              this->changingZoom = true;
356              float fovy = State::getCamera()->getFovy();
357              if( fovy > 30 )
358                  State::getCamera()->setFovy( fovy - 10*time );
359          }
360          else
361          {
362                State::getCamera()->setViewMode(Camera::ViewFPS);
363                this->inZoomMode = false;
364                this->changeZoom = false;
365          }
366      }
367  }
368  else
369  {
370      this->changeZoom = true;
371      this->changingZoom = false;
372  }
373
374  this->getWeaponManager().tick(time);
375  if( this->bFire)
376  {
377    this->getWeaponManager().fire();
378  }
379
380
381  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
382  {
383    xMouse *= time ;
384    yMouse *= time ;
385   
386    float amount;
387   
388    if( this->inZoomMode )
389        amount = 2.;
390    else
391        amount = 5.;
392
393    heading -= xMouse/amount;
394    attitude-= yMouse/amount;
395
396
397    if ( attitude > 1.95 )
398      attitude = 1.95;
399    else if ( attitude < -1.07 )
400      attitude = -1.07;
401
402    xMouse = yMouse = 0;
403  }
404
405  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
406  this->cameraNode->setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
407
408  Vector velocity;
409
410  if ( this->bForward )
411  {
412    velocity += this->getAbsDirX();
413  }
414
415  if ( this->bBackward )
416  {
417    velocity -= this->getAbsDirX();
418  }
419
420  if ( this->bRight )
421  {
422    velocity += this->getAbsDirZ();
423  }
424
425  if ( this->bLeft )
426  {
427    velocity -= this->getAbsDirZ();
428  }
429
430  // Uncomment this if you want your current position to be prined to the console when you press the jump button
431  /* if( this->bJump)
432    {
433      printf("panicGuy:runTo( %f, %f, %f ) \n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
434      this->bJump = false;
435    }*/
436
437  int speed;
438  if( this->bCrouch )
439      speed = 50;
440  else
441      speed = 100;
442  velocity *= speed;
443
444  if( this->getModel( 0) != NULL && this->getModel(0)->isA(InteractiveModel::staticClassID()))
445  {
446      if( this->bJump)
447      {
448          if( this->jumpAcceleration < 1.0f)
449          {
450              this->jumpAcceleration = 300.0f;
451
452              if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
453                  ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
454          }
455       }
456       else if(velocity.len() != 0.0f)
457       {
458            if( this->bCrouch )
459            {
460                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_WALK)
461                    ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_WALK);
462            }
463            else
464            {
465                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
466                    ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); 
467            }
468                 
469       }
470       else
471       {
472           if( this->bCrouch )
473           {
474               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_STAND)
475                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_STAND);
476           }
477           else
478           {
479               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
480                   ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
481           }
482       }
483       
484       if( this->bFire )
485       {
486           if( this->bCrouch )
487           {
488               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_ATTACK)
489                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_ATTACK);
490           }
491           else
492           {
493               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != ATTACK)
494                   ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
495           }
496       }
497  }
498
499
500  velocity.y += this->jumpAcceleration;
501  if( this->jumpAcceleration > 1.0f)
502    this->jumpAcceleration *= pow(0.9f,time*100);
503
504
505  // physical falling of the player
506  if( !this->isOnGround())
507  {
508    if(this->fallVelocity + 300.0F*time < 10000.0f)this->fallVelocity += 300.0f * time;
509    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
510
511//     PRINTF(0)("vel %f\n", this->fallVelocity);
512  }
513  else
514  {
515    this->fallVelocity = 0.0f;
516  }
517  if((velocity * time).len() < 10.0f) this->shiftCoor( velocity*time );
518  else
519  {     
520         velocity.normalize();
521         velocity *= 10.0f;
522         this->shiftCoor( velocity );
523  }
524
525
526
527  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(InteractiveModel::staticClassID()))
528  {
529    ((InteractiveModel*)this->getModel(0))->tick(time);
530  }
531
532  this->setOnGround(false);
533   
534}
535
536
537
538/**
539 *  draws the MD2Creature after transforming it.
540 */
541void FPSPlayer::draw () const
542{
543  // only draw if this entity is not the player since the player nevers sees himself
544  if( this->getCurrentPlayer() == NULL)
545    WorldEntity::draw();
546}
547
548
549
550/**
551 * checks events
552 */
553void FPSPlayer::process(const Event &event)
554{
555  Playable::process(event);
556
557  if( event.type == KeyMapper::PEV_LEFT)
558    this->bLeft = event.bPressed;
559  else if( event.type == KeyMapper::PEV_RIGHT)
560    this->bRight = event.bPressed;
561  else if( event.type == KeyMapper::PEV_FORWARD)
562    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
563  else if( event.type == KeyMapper::PEV_BACKWARD)
564    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
565  else if( event.type == KeyMapper::PEV_JUMP)
566    this->bJump = event.bPressed;
567  else if( event.type == KeyMapper::PEV_FIRE1)
568    this->bFire = event.bPressed;
569  else if( event.type == KeyMapper::PEV_FIRE2)
570    this->bFire2 = event.bPressed;
571  else if( event.type == KeyMapper::PEV_CROUCH)
572    this->bCrouch = event.bPressed;
573  else if( event.type == EV_MOUSE_MOTION)
574  {
575    this->xMouse += event.xRel;
576    this->yMouse += event.yRel;
577  }
578}
579
580/**
581 * respawns FPSplayer
582 */
583void FPSPlayer::respawn( )
584{
585  if( State::isOnline())
586    toList( OM_PLAYERS );
587
588  this->damageTicker = 0.0f;
589
590  Playable::respawn();
591}
592
593/**
594 * Kills the FPSplayer defining its killer
595 */
596void FPSPlayer::destroy( WorldEntity* killer )
597{
598  Playable::destroy( killer );
599  this->showDeadScreen();
600  myList = this->getOMListNumber();
601  toList( OM_DEAD );
602}
603
604void FPSPlayer::displayHUDText( const std::string& message )
605{
606        State::getPlayer()->hud().notifyUser(message);
607}
608
609void FPSPlayer::onButtonContinue( )
610{
611  OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true );
612  EventHandler::getInstance()->popState();
613  WorldEntity::reset();
614  toList( myList );
615  deadBox->hideAll();
616}
617
618void FPSPlayer::onButtonExit( )
619{
620  State::getCurrentStoryEntity()->setNextStoryID( 0 );
621  State::getCurrentStoryEntity()->stop();
622}
623
624void FPSPlayer::showDeadScreen( )
625{
626  EventHandler::getInstance()->pushState( ES_MENU );
627
628  OrxGui::GLGuiHandler::getInstance()->activateCursor();
629 
630  if ( deadBox )
631    delete deadBox;
632  deadBox = new OrxGui::GLGuiBox();
633  deadBox->setAbsCoor2D( 100, 100 );
634
635  OrxGui::GLGuiText* text = new OrxGui::GLGuiText();
636  text->setText( "Game Over! - You died!" );
637  OrxGui::GLGuiPushButton* exitButton = new OrxGui::GLGuiPushButton( "exit" );
638  exitButton->released.connect(this, &FPSPlayer::onButtonExit);
639  OrxGui::GLGuiPushButton* continueButton = new OrxGui::GLGuiPushButton( "continue" );
640  continueButton->released.connect(this, &FPSPlayer::onButtonContinue);
641
642  deadBox->pack( text );
643  deadBox->pack( continueButton );
644  deadBox->pack( exitButton );
645
646  deadBox->showAll();
647}
Note: See TracBrowser for help on using the repository browser.