Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/hud/src/world_entities/creatures/fps_player.cc @ 10678

Last change on this file since 10678 was 10654, checked in by bknecht, 18 years ago

Implemented zooming function

File size: 16.3 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
23#include "src/lib/util/loading/factory.h"
24
25#include "md2/md2Model.h"
26
27#include "weapons/weapon_manager.h"
28#include "weapons/test_gun.h"
29#include "weapons/turret.h"
30#include "weapons/cannon.h"
31#include "weapons/fps_sniper_rifle.h"
32#include "weapons/aiming_system.h"
33
34#include "aabb.h"
35#include "environments/bsp_entity.h"
36
37#include "key_mapper.h"
38
39#include "debug.h"
40
41#include "shared_network_data.h"
42
43
44
45ObjectListDefinition(FPSPlayer);
46CREATE_FACTORY(FPSPlayer);
47
48#include "script_class.h"
49CREATE_SCRIPTABLE_CLASS(FPSPlayer,
50                        addMethod("setAbsCoor", Executor3<PNode, lua_State*,float,float,float>(&PNode::setAbsCoor))
51                            ->addMethod("getAbsCoorX", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorX))
52                            ->addMethod("getAbsCoorY", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorY))
53                            ->addMethod("getAbsCoorZ", Executor0ret<PNode, lua_State*, float>(&PNode::getAbsCoorZ))
54                       );
55
56
57/**
58 *  destructs the FPSPlayer, deletes alocated memory
59 */
60FPSPlayer::~FPSPlayer ()
61{
62  this->setPlayer(NULL);
63
64  if( this->aimingSystem)
65    delete this->aimingSystem;
66}
67
68
69/**
70 *  creates a new FPSPlayer from Xml Data
71 * @param root the xml element containing FPSPlayer data
72 *
73 */
74FPSPlayer::FPSPlayer(const TiXmlElement* root)
75{
76  if (root != NULL)
77    this->loadParams(root);
78
79    this->updateNode(0.001);
80    this->init();
81}
82
83
84/**
85 * initializes a FPSPlayer
86 */
87void FPSPlayer::init()
88{
89  this->registerObject(this, FPSPlayer::_objectList);
90
91  this->bLeft = false;
92  this->bRight = false;
93  this->bForward = false;
94  this->bBackward = false;
95  this->bJump = false;
96  this->bPosBut = false;
97  this->bFire = false;
98  this->bFire2 = false;
99  this->changeZoom = true;
100  this->inZoomMode = false;
101  this->changingZoom = false;
102
103  this->xMouse = 0.0f;
104  this->yMouse = 0.0f;
105
106  this->setHealthMax(100);
107  this->setHealth(80);
108
109  this->fallVelocity = 0.0f;
110  this->jumpAcceleration = 0.0f;
111
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(EV_MOUSE_MOTION);
127
128  this->aimingSystem = NULL;
129
130  // weapon manager for the fps
131  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
132
133  if( State::isOnline())
134  {
135    Weapon* wpRight = new FPSSniperRifle(0);
136    wpRight->setName("testGun Right");
137    this->addWeapon(wpRight,1, 0);
138    wpRight->addChild(this->aimingSystem);
139
140    this->toList( OM_PLAYERS );
141  }
142
143  this->aimingSystem = new AimingSystem(this);
144
145
146  this->getWeaponManager().changeWeaponConfig(1);
147  this->getWeaponManager().setSlotCount(2);
148  this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1)));
149  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
150  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
151  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
152  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
153
154  this->getWeaponManager().setParentNode(&this->cameraNode);
155  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
156
157  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
158  this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
159  this->getWeaponManager().getFixedTarget()->setRelCoor(10,0,0);
160
161
162  // network registration
163  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
164  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
165  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
166  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
167  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
168  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
169  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
170
171    //subscribe to collision reaction
172  this->subscribeReaction(CoRe::CREngine::CR_PHYSICS_FULL_WALK, BspEntity::staticClassID());
173
174  this->initWeapon = false;
175  this->damageTicker = 0.0f;
176
177}
178
179
180/**
181 * loads the Settings of a FPSPlayer from an XML-element.
182 * @param root the XML-element to load the Spaceship's properties from
183 */
184void FPSPlayer::loadParams(const TiXmlElement* root)
185{
186  Playable::loadParams(root);
187}
188
189/**
190 * was probabably designed for setting direction of FPSPlayer
191 * but hey, this connot work like this, can it?
192 */
193void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
194{
195  this->attitude = this->getAbsDir().getAttitude();
196  this->heading = this->getAbsDir().getHeading();
197}
198
199/**
200 * Resets FPSPlayer stats and freezes its moving directions
201 *
202 */
203void FPSPlayer::reset()
204{
205  this->bLeft = false;
206  this->bRight = false;
207  this->bForward = false;
208  this->bBackward = false;
209  this->xMouse = 0.0f;
210  this->yMouse = 0.0f;
211  this->inZoomMode = false;
212
213  this->setHealth(80);
214}
215
216/**
217 * Defines what happens to camera and other important elements when changing
218 * into FPS-view
219 */
220void FPSPlayer::enter()
221{
222  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
223
224  State::getCameraNode()->setParentSoft(&this->cameraNode);
225  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
226 
227  State::getCamera()->setViewMode(Camera::ViewFPS);
228 
229  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
230  //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
231  //this->getWeaponManager().getFixedTarget()->setRelCoor(100,0,0);
232
233  if ( !State::isOnline() )
234  {
235    this->respawn();
236  }
237}
238
239/**
240 * Defines what happens if active player leaves FPSPlayer
241 * (basicly hides crosshair and frees camera)
242 */
243void FPSPlayer::leave()
244{
245  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
246  this->detachCamera();
247}
248
249
250
251/**
252 *  the function called for each passing timeSnap
253 * @param time The timespan passed since last update
254 */
255void FPSPlayer::tick (float time)
256{
257     // Second init-step
258  if ( !this->initWeapon )
259  {
260    this->initWeapon = true;
261
262    this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE);
263
264    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
265    this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
266    //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
267    State::getCamera()->setViewMode(Camera::ViewFPS);
268
269
270    if( this->aimingSystem != NULL)
271    {
272      this->aimingSystem->toList(OM_GROUP_01);
273      this->aimingSystem->setParent(&this->cameraNode);
274  //     this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE);
275      this->aimingSystem->setRelDir(Quaternion(M_PI_4*-0.58f, Vector(0,0,1)));
276      this->aimingSystem->setRelCoor(0, -1, -1);
277    }
278
279  }
280  // end of second init-step
281 
282  // This box represents the dimension of the used model
283  AABB* box = this->getModelAABB();
284
285  if( box != NULL)
286  {
287      float f = 1.0;
288      if( this->bCrouch )
289          f = 0.3*f;
290     
291      this->cameraNode.setRelCoor(0, box->halfLength[1] * f, 0);
292//      this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
293     float v = 0.1f;
294     this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
295     this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
296  }
297 
298  /*
299  if( this->bFire2 )
300  {
301     
302      // to change Zoom on click
303      if( this->changeZoom )
304      {
305          this->changeZoom = false;
306          if( this->inZoomMode )
307          {
308              State::getCamera()->setViewMode(Camera::ViewFPS);
309              this->inZoomMode = false;
310          }
311          else
312          {
313              State::getCamera()->setViewMode(Camera::ViewFPSZoom);
314              this->inZoomMode = true;
315          }
316         
317      }
318     
319  }
320  else
321  {
322      this->changeZoom = true;
323  }*/
324  if( this->bFire2 )
325  {
326      if( this->changeZoom )
327      {
328          if( !this->inZoomMode || this->changingZoom )
329          {
330              this->inZoomMode = true;
331              this->changingZoom = true;
332              float fovy = State::getCamera()->getFovy();
333              if( fovy > 30 )
334                  State::getCamera()->setFovy( fovy - 10*time );
335          }
336          else
337          {
338                State::getCamera()->setViewMode(Camera::ViewFPS);
339                this->inZoomMode = false;
340                this->changeZoom = false;
341          }
342      }
343  }
344  else
345  {
346      this->changeZoom = true;
347      this->changingZoom = false;
348  }
349
350  this->getWeaponManager().tick(time);
351  if( this->bFire)
352  {
353    this->getWeaponManager().fire();
354    PRINTF(0)("Crosshair at position: %f, %f, %f\n", this->getWeaponManager().getFixedTarget()->getAbsCoor().x, this->getWeaponManager().getFixedTarget()->getAbsCoor().y, this->getWeaponManager().getFixedTarget()->getAbsCoor().z);
355  }
356
357
358  //dealing damage
359  if ( State::isOnline() && (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/))
360  {
361    this->damageTicker -= time;
362
363    if ( this->damageTicker <= 0.0f && this->beFire() )
364    {
365      this->damageTicker = 0.25;
366
367      WorldEntity * victim = aimingSystem->getNearestTarget();
368
369      if ( victim )
370      {
371        PRINTF(0)("FIRE: hit %s\n", victim->getClassCName());
372        victim->hit( 20, this );
373      }
374      else
375      {
376        PRINTF(0)("FIRE: nothing hit\n");
377      }
378    }
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  if( this->aimingSystem != NULL)
535    this->aimingSystem->flushList();
536   
537}
538
539
540
541/**
542 *  draws the MD2Creature after transforming it.
543 */
544void FPSPlayer::draw () const
545{
546  // only draw if this entity is not the player since the player nevers sees himself
547  if( this->getCurrentPlayer() == NULL)
548    WorldEntity::draw();
549}
550
551
552
553/**
554 * checks events
555 */
556void FPSPlayer::process(const Event &event)
557{
558  Playable::process(event);
559
560  if( event.type == KeyMapper::PEV_LEFT)
561    this->bLeft = event.bPressed;
562  else if( event.type == KeyMapper::PEV_RIGHT)
563    this->bRight = event.bPressed;
564  else if( event.type == KeyMapper::PEV_FORWARD)
565    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
566  else if( event.type == KeyMapper::PEV_BACKWARD)
567    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
568  else if( event.type == KeyMapper::PEV_JUMP)
569    this->bJump = event.bPressed;
570  else if( event.type == KeyMapper::PEV_FIRE1)
571    this->bFire = event.bPressed;
572  else if( event.type == KeyMapper::PEV_FIRE2)
573    this->bFire2 = event.bPressed;
574  else if( event.type == KeyMapper::PEV_CROUCH)
575    this->bCrouch = event.bPressed;
576  else if( event.type == EV_MOUSE_MOTION)
577  {
578    this->xMouse += event.xRel;
579    this->yMouse += event.yRel;
580  }
581}
582
583/**
584 * respawns FPSplayer
585 */
586void FPSPlayer::respawn( )
587{
588  if( State::isOnline())
589    toList( OM_PLAYERS );
590
591  this->damageTicker = 0.0f;
592
593  Playable::respawn();
594}
595
596/**
597 * Kills the FPSplayer defining its killer
598 */
599void FPSPlayer::destroy( WorldEntity* killer )
600{
601  Playable::destroy( killer );
602
603  toList( OM_DEAD );
604}
605
Note: See TracBrowser for help on using the repository browser.