Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10714 was 10714, checked in by nicolasc, 17 years ago

initial upload form mouse aiming in VS

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(1);
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    weapon = new FPSSniperRifle(0);
155    weapon->setName("testGun Right");
156    this->addWeapon(weapon,1, 0);
157    weapon->toList( this->getOMListNumber() );
158    weapon->setParent( this->cameraNode );
159    weapon->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; // unused variable
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    this->getWeaponManager().fire();
368  }
369
370
371  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
372  {
373    xMouse *= time ;
374    yMouse *= time ;
375   
376    float amount;
377   
378    if( this->inZoomMode )
379        amount = 2.;
380    else
381        amount = 5.;
382
383    heading -= xMouse/amount;
384    attitude-= yMouse/amount;
385
386
387    if ( attitude > 1.95 )
388      attitude = 1.95;
389    else if ( attitude < -1.07 )
390      attitude = -1.07;
391
392    xMouse = yMouse = 0;
393  }
394
395  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
396  this->cameraNode->setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
397
398  Vector velocity;
399
400  if ( this->bForward )
401  {
402    velocity += this->getAbsDirX();
403  }
404
405  if ( this->bBackward )
406  {
407    velocity -= this->getAbsDirX();
408  }
409
410  if ( this->bRight )
411  {
412    velocity += this->getAbsDirZ();
413  }
414
415  if ( this->bLeft )
416  {
417    velocity -= this->getAbsDirZ();
418  }
419
420  // Uncomment this if you want your current position to be prined to the console when you press the jump button
421  /* if( this->bJump)
422    {
423      printf("panicGuy:runTo( %f, %f, %f ) \n", this->getAbsCoorX(), this->getAbsCoorY(), this->getAbsCoorZ() );
424      this->bJump = false;
425    }*/
426
427  int speed;
428  if( this->bCrouch )
429      speed = 50;
430  else
431      speed = 100;
432  velocity *= speed;
433
434  if( this->getModel( 0) != NULL && this->getModel(0)->isA(InteractiveModel::staticClassID()))
435  {
436      if( this->bJump)
437      {
438          if( this->jumpAcceleration < 1.0f)
439          {
440              this->jumpAcceleration = 300.0f;
441
442              if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
443                  ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
444          }
445       }
446       else if(velocity.len() != 0.0f)
447       {
448            if( this->bCrouch )
449            {
450                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_WALK)
451                    ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_WALK);
452            }
453            else
454            {
455                if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
456                    ((InteractiveModel*)this->getModel(0))->setAnimation(RUN); 
457            }
458                 
459       }
460       else
461       {
462           if( this->bCrouch )
463           {
464               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_STAND)
465                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_STAND);
466           }
467           else
468           {
469               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
470                   ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
471           }
472       }
473       
474       if( this->bFire )
475       {
476           if( this->bCrouch )
477           {
478               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != CROUCH_ATTACK)
479                   ((InteractiveModel*)this->getModel(0))->setAnimation(CROUCH_ATTACK);
480           }
481           else
482           {
483               if( ((InteractiveModel*)this->getModel(0))->getAnimation() != ATTACK)
484                   ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
485           }
486       }
487  }
488
489
490  velocity.y += this->jumpAcceleration;
491  if( this->jumpAcceleration > 1.0f)
492    this->jumpAcceleration *= pow(0.9f,time*100);
493
494
495  // physical falling of the player
496  if( !this->isOnGround())
497  {
498    if(this->fallVelocity + 300.0F*time < 10000.0f)this->fallVelocity += 300.0f * time;
499    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
500
501//     PRINTF(0)("vel %f\n", this->fallVelocity);
502  }
503  else
504  {
505    this->fallVelocity = 0.0f;
506  }
507  if((velocity * time).len() < 10.0f) this->shiftCoor( velocity*time );
508  else
509  {     
510         velocity.normalize();
511         velocity *= 10.0f;
512         this->shiftCoor( velocity );
513  }
514
515
516
517  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(InteractiveModel::staticClassID()))
518  {
519    ((InteractiveModel*)this->getModel(0))->tick(time);
520  }
521
522  this->setOnGround(false);
523   
524}
525
526
527
528/**
529 *  draws the MD2Creature after transforming it.
530 */
531void FPSPlayer::draw () const
532{
533  // only draw if this entity is not the player since the player nevers sees himself
534  if( this->getCurrentPlayer() == NULL)
535    WorldEntity::draw();
536}
537
538
539
540/**
541 * checks events
542 */
543void FPSPlayer::process(const Event &event)
544{
545  Playable::process(event);
546
547  if( event.type == KeyMapper::PEV_LEFT)
548    this->bLeft = event.bPressed;
549  else if( event.type == KeyMapper::PEV_RIGHT)
550    this->bRight = event.bPressed;
551  else if( event.type == KeyMapper::PEV_FORWARD)
552    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
553  else if( event.type == KeyMapper::PEV_BACKWARD)
554    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
555  else if( event.type == KeyMapper::PEV_JUMP)
556    this->bJump = event.bPressed;
557  else if( event.type == KeyMapper::PEV_FIRE1)
558    this->bFire = event.bPressed;
559  else if( event.type == KeyMapper::PEV_FIRE2)
560    this->bFire2 = event.bPressed;
561  else if( event.type == KeyMapper::PEV_CROUCH)
562    this->bCrouch = event.bPressed;
563  else if( event.type == EV_MOUSE_MOTION)
564  {
565    this->xMouse += event.xRel;
566    this->yMouse += event.yRel;
567  }
568}
569
570/**
571 * respawns FPSplayer
572 */
573void FPSPlayer::respawn( )
574{
575  if( State::isOnline())
576    toList( OM_PLAYERS );
577
578  this->damageTicker = 0.0f;
579
580  Playable::respawn();
581}
582
583/**
584 * Kills the FPSplayer defining its killer
585 */
586void FPSPlayer::destroy( WorldEntity* killer )
587{
588  Playable::destroy( killer );
589
590  toList( OM_DEAD );
591}
592
593void FPSPlayer::displayHUDText( const std::string& message )
594{
595        State::getPlayer()->hud().notifyUser(message);
596}
Note: See TracBrowser for help on using the repository browser.