Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10647 was 10647, checked in by bknecht, 17 years ago

Camera:

  • Added new ViewMode ViewFPS

FPSPlayer:

  • fixed crosshair bug

FPSPlayer/KeyMapper/Globals:

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