Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Kralle entfernt?

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