Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

improved damage handling for adm

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