Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10768 was 10753, checked in by bknecht, 18 years ago

few changes

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