Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

does not segfault anymore

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
388
389  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
390  {
391    xMouse *= time ;
392    yMouse *= time ;
393   
394    float amount;
395   
396    if( this->inZoomMode )
397        amount = 2.;
398    else
399        amount = 5.;
400
401    heading -= xMouse/amount;
402    attitude-= yMouse/amount;
403
404
405    if ( attitude > 1.95 )
406      attitude = 1.95;
407    else if ( attitude < -1.07 )
408      attitude = -1.07;
409
410    xMouse = yMouse = 0;
411  }
412
413  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
414  this->cameraNode->setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
415
416  Vector velocity;
417
418  if ( this->bForward )
419  {
420    velocity += this->getAbsDirX();
421  }
422
423  if ( this->bBackward )
424  {
425    velocity -= this->getAbsDirX();
426  }
427
428  if ( this->bRight )
429  {
430    velocity += this->getAbsDirZ();
431  }
432
433  if ( this->bLeft )
434  {
435    velocity -= this->getAbsDirZ();
436  }
437
438  // Uncomment this if you want your current position to be prined to the console when you press the jump button
439  /* if( this->bJump)
440    {
441      printf("panicGuy:runTo( %f, %f, %f ) \n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
442      this->bJump = false;
443    }*/
444
445  int speed;
446  if( this->bCrouch )
447      speed = 50;
448  else
449      speed = 100;
450  velocity *= speed;
451
452  if( this->getModel( 0) != NULL && this->getModel(0)->isA(InteractiveModel::staticClassID()))
453  {
454      if( this->bJump)
455      {
456          if( this->jumpAcceleration < 1.0f)
457          {
458              this->jumpAcceleration = 300.0f;
459
460              if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
461                  ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
462          }
463       }
464       else if(velocity.len() != 0.0f)
465       {
466            if( this->bCrouch )
467            {
468                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_WALK)
469                    ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_WALK);
470            }
471            else
472            {
473                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
474                    ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); 
475            }
476                 
477       }
478       else
479       {
480           if( this->bCrouch )
481           {
482               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_STAND)
483                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_STAND);
484           }
485           else
486           {
487               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
488                   ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
489           }
490       }
491       
492       if( this->bFire )
493       {
494           if( this->bCrouch )
495           {
496               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_ATTACK)
497                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_ATTACK);
498           }
499           else
500           {
501               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != ATTACK)
502                   ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
503           }
504       }
505  }
506
507
508  velocity.y += this->jumpAcceleration;
509  if( this->jumpAcceleration > 1.0f)
510    this->jumpAcceleration *= pow(0.9f,time*100);
511
512
513  // physical falling of the player
514  if( !this->isOnGround())
515  {
516    if(this->fallVelocity + 300.0F*time < 10000.0f)this->fallVelocity += 300.0f * time;
517    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
518
519//     PRINTF(0)("vel %f\n", this->fallVelocity);
520  }
521  else
522  {
523    this->fallVelocity = 0.0f;
524  }
525  if((velocity * time).len() < 10.0f) this->shiftCoor( velocity*time );
526  else
527  {     
528         velocity.normalize();
529         velocity *= 10.0f;
530         this->shiftCoor( velocity );
531  }
532
533
534
535  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(InteractiveModel::staticClassID()))
536  {
537    ((InteractiveModel*)this->getModel(0))->tick(time);
538  }
539
540  this->setOnGround(false);
541   
542}
543
544
545
546/**
547 *  draws the MD2Creature after transforming it.
548 */
549void FPSPlayer::draw () const
550{
551  // only draw if this entity is not the player since the player nevers sees himself
552  if( this->getCurrentPlayer() == NULL)
553    WorldEntity::draw();
554}
555
556
557
558/**
559 * checks events
560 */
561void FPSPlayer::process(const Event &event)
562{
563  Playable::process(event);
564
565  if( event.type == KeyMapper::PEV_LEFT)
566    this->bLeft = event.bPressed;
567  else if( event.type == KeyMapper::PEV_RIGHT)
568    this->bRight = event.bPressed;
569  else if( event.type == KeyMapper::PEV_FORWARD)
570    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
571  else if( event.type == KeyMapper::PEV_BACKWARD)
572    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
573  else if( event.type == KeyMapper::PEV_JUMP)
574    this->bJump = event.bPressed;
575  else if( event.type == KeyMapper::PEV_FIRE1)
576    this->bFire = event.bPressed;
577  else if( event.type == KeyMapper::PEV_FIRE2)
578    this->bFire2 = event.bPressed;
579  else if( event.type == KeyMapper::PEV_CROUCH)
580    this->bCrouch = event.bPressed;
581  else if( event.type == EV_MOUSE_MOTION)
582  {
583    this->xMouse += event.xRel;
584    this->yMouse += event.yRel;
585  }
586}
587
588/**
589 * respawns FPSplayer
590 */
591void FPSPlayer::respawn( )
592{
593  if( State::isOnline())
594    toList( OM_PLAYERS );
595
596  this->damageTicker = 0.0f;
597
598  Playable::respawn();
599}
600
601/**
602 * Kills the FPSplayer defining its killer
603 */
604void FPSPlayer::destroy( WorldEntity* killer )
605{
606  Playable::destroy( killer );
607  this->showDeadScreen();
608  myList = this->getOMListNumber();
609  toList( OM_DEAD );
610}
611
612void FPSPlayer::displayHUDText( const std::string& message )
613{
614        State::getPlayer()->hud().notifyUser(message);
615}
616
617void FPSPlayer::onButtonContinue( )
618{
619  OrxGui::GLGuiHandler::getInstance()->deactivateCursor( true );
620  EventHandler::getInstance()->popState();
621  WorldEntity::reset();
622  toList( myList );
623  deadBox->hideAll();
624}
625
626void FPSPlayer::onButtonExit( )
627{
628  State::getCurrentStoryEntity()->setNextStoryID( 0 );
629  State::getCurrentStoryEntity()->stop();
630}
631
632void FPSPlayer::showDeadScreen( )
633{
634  EventHandler::getInstance()->pushState( ES_MENU );
635
636  OrxGui::GLGuiHandler::getInstance()->activateCursor();
637 
638  if ( deadBox )
639    delete deadBox;
640  deadBox = new OrxGui::GLGuiBox();
641  deadBox->setAbsCoor2D( 100, 100 );
642
643  OrxGui::GLGuiText* text = new OrxGui::GLGuiText();
644  text->setText( "Game Over! - You died!" );
645  OrxGui::GLGuiPushButton* exitButton = new OrxGui::GLGuiPushButton( "exit" );
646  exitButton->released.connect(this, &FPSPlayer::onButtonExit);
647  OrxGui::GLGuiPushButton* continueButton = new OrxGui::GLGuiPushButton( "continue" );
648  continueButton->released.connect(this, &FPSPlayer::onButtonContinue);
649
650  deadBox->pack( text );
651  deadBox->pack( continueButton );
652  deadBox->pack( exitButton );
653
654  deadBox->showAll();
655}
Note: See TracBrowser for help on using the repository browser.