Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

fpsweapon in the correct position

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->requestAction( WA_ACTIVATE );
160    //wpRight->addChild(this->aimingSystem);
161
162    //this->toList( OM_PLAYERS );
163  }
164#else
165                         
166  FPSSniperRifle* wpRight = new FPSSniperRifle(0);
167  wpRight->setName("testGun Right");
168  wpRight->toList( this->getOMListNumber() );
169  wpRight->setParent( &this->cameraNode );
170 
171  this->weaponMan.setParentEntity( this );
172  this->weaponMan.setSlotCount(2);
173  this->weaponMan.createWeaponSlot(0, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
174  this->weaponMan.createWeaponSlot(1, 0, 0, 0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
175  this->weaponMan.addWeapon(wpRight, 0, 0);
176  this->weaponMan.changeWeaponConfig(0);
177  Playable::weaponConfigChanged();
178  this->weaponMan.getFixedTarget()->setParent(&this->cameraNode );
179  this->weaponMan.getFixedTarget()->setRelCoor( 100000,0,0 );
180#endif
181
182  // network registration
183  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
184  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
185  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
186  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
187  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
188  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
189  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
190
191    //subscribe to collision reaction
192  this->subscribeReaction(CoRe::CREngine::CR_PHYSICS_FULL_WALK, BspEntity::staticClassID());
193
194  this->initWeapon = false;
195  this->damageTicker = 0.0f;
196
197}
198
199
200/**
201 * loads the Settings of a FPSPlayer from an XML-element.
202 * @param root the XML-element to load the Spaceship's properties from
203 */
204void FPSPlayer::loadParams(const TiXmlElement* root)
205{
206  Playable::loadParams(root);
207}
208
209/**
210 * was probabably designed for setting direction of FPSPlayer
211 * but hey, this connot work like this, can it?
212 */
213void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
214{
215  this->attitude = this->getAbsDir().getAttitude();
216  this->heading = this->getAbsDir().getHeading();
217}
218
219/**
220 * Resets FPSPlayer stats and freezes its moving directions
221 *
222 */
223void FPSPlayer::reset()
224{
225  this->bLeft = false;
226  this->bRight = false;
227  this->bForward = false;
228  this->bBackward = false;
229  this->xMouse = 0.0f;
230  this->yMouse = 0.0f;
231  this->inZoomMode = false;
232
233  this->setHealth(80);
234}
235
236/**
237 * Defines what happens to camera and other important elements when changing
238 * into FPS-view
239 */
240void FPSPlayer::enter()
241{
242  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
243
244  State::getCameraNode()->setParentSoft(this->cameraNode);
245  State::getCameraTargetNode()->setParentSoft(this->cameraNode);
246 
247  State::getCamera()->setViewMode(Camera::ViewFPS);
248 
249  this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
250  //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ALL);
251  //this->getWeaponManager().getFixedTarget()->setRelCoor(100,0,0);
252
253  if ( !State::isOnline() )
254  {
255    this->respawn();
256  }
257}
258
259/**
260 * Defines what happens if active player leaves FPSPlayer
261 * (basicly hides crosshair and frees camera)
262 */
263void FPSPlayer::leave()
264{
265  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
266  this->detachCamera();
267}
268
269
270
271/**
272 *  the function called for each passing timeSnap
273 * @param time The timespan passed since last update
274 */
275void FPSPlayer::tick (float time)
276{
277     // Second init-step
278  if ( !this->initWeapon )
279  {
280    this->initWeapon = true;
281
282    this->cameraNode->setParentMode(PNODE_ROTATE_AND_MOVE);
283
284    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
285    this->getWeaponManager().getFixedTarget()->setParent(this->cameraNode);
286    //this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
287    State::getCamera()->setViewMode(Camera::ViewFPS);
288
289
290  }
291  // end of second init-step
292 
293  // This box represents the dimension of the used model
294  AABB* box = this->getModelAABB();
295
296  if( box != NULL)
297  {
298      float f = 1.0;
299      if( this->bCrouch )
300          f = 0.3*f;
301     
302      this->cameraNode->setRelCoor(0, box->halfLength[1] * f, 0);
303//      this->weaponMan.setRelCoor(0, box->halfLength[1] * f, 0);
304//      this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
305     float v = 0.1f;
306     //this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
307     //this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
308     this->getWeaponManager().setSlotDirection( 0, Quaternion( 0.04, Vector( 0, 0, 1 ) ) );
309  }
310 
311  /*
312  if( this->bFire2 )
313  {
314     
315      // to change Zoom on click
316      if( this->changeZoom )
317      {
318          this->changeZoom = false;
319          if( this->inZoomMode )
320          {
321              State::getCamera()->setViewMode(Camera::ViewFPS);
322              this->inZoomMode = false;
323          }
324          else
325          {
326              State::getCamera()->setViewMode(Camera::ViewFPSZoom);
327              this->inZoomMode = true;
328          }
329         
330      }
331     
332  }
333  else
334  {
335      this->changeZoom = true;
336  }*/
337  if( this->bFire2 )
338  {
339      if( this->changeZoom )
340      {
341          if( !this->inZoomMode || this->changingZoom )
342          {
343              this->inZoomMode = true;
344              this->changingZoom = true;
345              float fovy = State::getCamera()->getFovy();
346              if( fovy > 30 )
347                  State::getCamera()->setFovy( fovy - 10*time );
348          }
349          else
350          {
351                State::getCamera()->setViewMode(Camera::ViewFPS);
352                this->inZoomMode = false;
353                this->changeZoom = false;
354          }
355      }
356  }
357  else
358  {
359      this->changeZoom = true;
360      this->changingZoom = false;
361  }
362
363  this->getWeaponManager().tick(time);
364  if( this->bFire)
365  {
366    PRINTF(0)("FPSPLAYER FIRE %d\n", this->getOMListNumber() );
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.