Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

FPSSniperRifle uses BspWeapon now

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