Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/world_entities/creatures/fps_player.cc @ 9470

Last change on this file since 9470 was 9470, checked in by patrick, 18 years ago

heavy permissions fight: no node was ever thought to be client and server at the same time, proxy server are hybrid nodes so there is need for a big framework extension.

  • made the obb creation saver
  • prevented segfaults in the aabb tree creation, this was very dangerous code
  • inserted handshake hack to make the handshake work.

No I will have to get the handshake right so the node works correctly

File size: 12.2 KB
RevLine 
[8685]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"
[8731]20#include "state.h"
[8685]21
22#include "src/lib/util/loading/factory.h"
23
[9110]24#include "md2/md2Model.h"
25
[8776]26#include "weapons/weapon_manager.h"
27#include "weapons/test_gun.h"
28#include "weapons/turret.h"
29#include "weapons/cannon.h"
[8894]30#include "weapons/fps_sniper_rifle.h"
[9235]31#include "weapons/aiming_system.h"
[8776]32
[9110]33#include "aabb.h"
34
[9235]35
[8685]36#include "key_mapper.h"
37
[8687]38#include "debug.h"
[8685]39
[9110]40#include "shared_network_data.h"
[8685]41
[8687]42
[8731]43
[8685]44CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
45
[8894]46#include "script_class.h"
47CREATE_SCRIPTABLE_CLASS(FPSPlayer, CL_FPS_PLAYER,
48                        addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
49                            ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
50                            ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
51                            ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
52                       );
[8685]53
[8894]54
[8685]55/**
56 *  destructs the FPSPlayer, deletes alocated memory
57 */
58FPSPlayer::~FPSPlayer ()
59{
60  this->setPlayer(NULL);
[9235]61
62  if( this->aimingSystem)
63    delete this->aimingSystem;
[8685]64}
65
66
67/**
68 *  creates a new FPSPlayer from Xml Data
69 * @param root the xml element containing FPSPlayer data
70 *
71 */
72FPSPlayer::FPSPlayer(const TiXmlElement* root)
73{
74  this->init();
75
76  if (root != NULL)
77    this->loadParams(root);
78
79}
80
81
82/**
83 * initializes a FPSPlayer
84 */
85void FPSPlayer::init()
86{
87  this->setClassID(CL_FPS_PLAYER, "FPSPlayer");
88
89  this->bLeft = false;
90  this->bRight = false;
91  this->bForward = false;
92  this->bBackward = false;
93  this->bJump = false;
[9003]94  this->bPosBut = false;
[9235]95  this->bFire = false;
[8731]96
[8685]97  this->xMouse = 0.0f;
98  this->yMouse = 0.0f;
99
100  this->setHealthMax(100);
101  this->setHealth(80);
102
[9003]103  this->fallVelocity = 0.0f;
[9110]104  this->jumpForce = 0.0f;
[8685]105
[8731]106  this->cameraNode.setParent(this);
107
108  this->attitude = this->getAbsDir().getAttitude();
109  this->heading = this->getAbsDir().getHeading();
110
[8685]111  //add events to the eventlist
112  registerEvent(KeyMapper::PEV_FORWARD);
113  registerEvent(KeyMapper::PEV_BACKWARD);
114  registerEvent(KeyMapper::PEV_LEFT);
115  registerEvent(KeyMapper::PEV_RIGHT);
116  registerEvent(KeyMapper::PEV_FIRE1);
[8687]117  registerEvent(KeyMapper::PEV_JUMP);
[8685]118  registerEvent(EV_MOUSE_MOTION);
119
120
121
[8776]122  // weapon manager for the fps
[8685]123  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
124
[8894]125  Weapon* wpRight = new FPSSniperRifle(0);
[8776]126  wpRight->setName("testGun Right");
[8894]127/*  Weapon* wpLeft = new TestGun(1);*/
[8776]128//   Weapon* wpLeft = new Turret();
[8894]129//   wpLeft->setName("testGun Left");
[8776]130
[8894]131//   this->addWeapon(wpLeft, 1, 0);
[9235]132  if( State::isOnline())
133    this->addWeapon(wpRight,1, 0);
[8776]134  this->getWeaponManager().changeWeaponConfig(1);
135
136  this->getWeaponManager().setSlotCount(2);
[9235]137  this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1)));
[8776]138  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[9110]139  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
140  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
[9003]141  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
[8776]142
[9235]143//   this->getWeaponManager().getFixedTarget()->setRelDir(Quaternion(M_PI_4*-0.6f, Vector(0,0,1)));
[9110]144
[9235]145
[8844]146  this->getWeaponManager().setParentNode(&this->cameraNode);
[8846]147  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
148
[8776]149  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
150  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
151
[9235]152  this->aimingSystem = new AimingSystem(this);
153  //this->addChild(this->aimingSystem);
154  wpRight->addChild(this->aimingSystem);
155//   this->getWeaponManager().sl
[8776]156
[9235]157
[8686]158  // network registration
[8685]159  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
160  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
161  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
162  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
[9235]163  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
[9110]164  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
165  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
[9235]166
167    //subscribe to collision reaction
168  this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY);
169
170  this->initWeapon = false;
171  this->damageTicker = 0.0f;
172
173  if( State::isOnline())
174    toList( OM_PLAYERS );
[8685]175}
176
177
178/**
179 * loads the Settings of a FPSPlayer from an XML-element.
180 * @param root the XML-element to load the Spaceship's properties from
181 */
182void FPSPlayer::loadParams(const TiXmlElement* root)
183{
184  Playable::loadParams(root);
185}
186
187void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
188{
[8731]189  this->attitude = this->getAbsDir().getAttitude();
190  this->heading = this->getAbsDir().getHeading();
[8685]191}
192
193
194void FPSPlayer::reset()
195{
196  this->bLeft = false;
197  this->bRight = false;
198  this->bForward = false;
199  this->bBackward = false;
200  this->xMouse = 0.0f;
201  this->yMouse = 0.0f;
202
203  this->setHealth(80);
204}
205
206
207void FPSPlayer::enter()
208{
[8731]209  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
210
211  State::getCameraNode()->setParentSoft(&this->cameraNode);
212  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
213
214  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
215  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
216
[9235]217  if ( !State::isOnline() )
[9110]218  {
[9235]219    this->respawn();
[9110]220  }
[8685]221}
222
223void FPSPlayer::leave()
224{
225  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
226  this->detachCamera();
227}
228
229
230
231/**
232 *  the function called for each passing timeSnap
233 * @param time The timespan passed since last update
234 */
235void FPSPlayer::tick (float time)
236{
[9110]237
[9235]238  if ( !this->initWeapon )
[9003]239  {
[9235]240    this->initWeapon = true;
241
242    this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE);
243
244    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
245    this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
246    this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
247
248
249    this->aimingSystem->toList(OM_GROUP_01);
250    this->aimingSystem->setParent(&this->cameraNode);
251//     this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE);
252    this->aimingSystem->setRelDir(Quaternion(M_PI_4*-0.58f, Vector(0,0,1)));
253    this->aimingSystem->setRelCoor(0, -1, -1);
254
255
256    AABB* box = this->getModelAABB();
257    if( box != NULL)
258    {
259      float f = 1.0;
260      this->cameraNode.setRelCoor(0, box->halfLength[1] * f, 0);
261//       this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
262
263      float v = 0.1f;
264      this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
265      this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
266    }
[9003]267  }
[9110]268
[8685]269
[9235]270  this->getWeaponManager().tick(time);
271  if( this->bFire)
272  {
273    this->getWeaponManager().fire();
274
275//     WorldEntity* target = this->aimingSystem->getNearestTarget();
276//     if( target != NULL)
277//     {
[9406]278//       PRINTF(0)("hit hit hit, got: %s\n", target->getClassCName());
[9235]279//     }
280//     else
281//     {
282//       PRINTF(0)("nothing hit\n");
283//     }
284  }
285
286
287  //dealing damage
288
[9470]289  if ( State::isOnline() && (SharedNetworkData::getInstance()->isMasterServer() /*|| SharedNetworkData::getInstance()->isProxyServerActive()*/))
[9235]290  {
291    this->damageTicker -= time;
292
293    if ( this->damageTicker <= 0.0f && this->beFire() )
294    {
295      this->damageTicker = 0.25;
296
297      WorldEntity * victim = aimingSystem->getNearestTarget();
298
299      if ( victim )
300      {
[9406]301        PRINTF(0)("FIRE: hit %s\n", victim->getClassCName());
[9235]302        victim->hit( 20, this );
303      }
304      else
305      {
306        PRINTF(0)("FIRE: nothing hit\n");
307      }
308    }
309  }
310
311
[9110]312  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
[8685]313  {
[8687]314    xMouse *= time ;
315    yMouse *= time ;
[8685]316
[8731]317    heading -= xMouse;
318    attitude-= yMouse;
[8685]319
320
[9235]321    if ( attitude > 1.95 )
322      attitude = 1.95;
323    else if ( attitude < -1.07 )
324      attitude = -1.07;
[8685]325
326    xMouse = yMouse = 0;
327  }
328
[9110]329  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
330  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
[8685]331
332  Vector velocity;
333
334  if ( this->bForward )
335  {
336    velocity += this->getAbsDirX();
337  }
338
339  if ( this->bBackward )
340  {
341    velocity -= this->getAbsDirX();
342  }
343
344  if ( this->bRight )
345  {
346    velocity += this->getAbsDirZ();
347  }
348
349  if ( this->bLeft )
350  {
351    velocity -= this->getAbsDirZ();
352  }
353
[9003]354
[8685]355  velocity *= 100;
356
[9110]357  if( this->bJump && likely(this->getModel(0) != NULL))
358  {
359    if( this->jumpForce < 1.0f)
360    {
361      this->jumpForce = 300.0f;
[9003]362
[9110]363      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
364        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
365    }
366  }
367  else if(velocity.len() != 0.0f)
368  {
369    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
370      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
371  }
372  else
373  {
374    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
375      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
376  }
377
378
379  velocity.y += this->jumpForce;
380  if( this->jumpForce > 1.0f)
381    this->jumpForce *= 0.9f;
382
383
[9003]384  // physical falling of the player
385  if( !this->isOnGround())
386  {
387    this->fallVelocity += 300.0f * time;
388    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
[9235]389
390//     PRINTF(0)("vel %f\n", this->fallVelocity);
[9003]391  }
392  else
393  {
394    this->fallVelocity = 0.0f;
395  }
396
[8685]397  this->shiftCoor( velocity*time );
398
399
400
401
[9003]402
403
404
[8693]405  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
406  {
407    ((InteractiveModel*)this->getModel(0))->tick(time);
[9110]408
409    // handle animations differently
410
411
412
413
414
[8685]415//     else if( this->bFire && likely(this->getModel(0) != NULL))
416//     {
417//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
418//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
419//     }
420//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
421//     {
422//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
423//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
424//     }
[8687]425//     else if (likely(this->getModel(0) != NULL))
426//     {
427//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
428//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
429//     }
[8693]430  }
[8685]431
[9235]432  this->setOnGround(false);
433  this->aimingSystem->flushList();
[8685]434}
435
436
437
[8686]438/**
439 *  draws the MD2Creature after transforming it.
440 */
441void FPSPlayer::draw () const
442{
443  // only draw if this entity is not the player since the player nevers sees himself
[8699]444  if( this->getCurrentPlayer() == NULL)
[8686]445    WorldEntity::draw();
446}
[8685]447
448
449
450/**
[8686]451 * process
[8685]452 */
453void FPSPlayer::process(const Event &event)
454{
455  Playable::process(event);
456
457  if( event.type == KeyMapper::PEV_LEFT)
458    this->bLeft = event.bPressed;
459  else if( event.type == KeyMapper::PEV_RIGHT)
460    this->bRight = event.bPressed;
461  else if( event.type == KeyMapper::PEV_FORWARD)
462    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
463  else if( event.type == KeyMapper::PEV_BACKWARD)
464    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
465  else if( event.type == EV_MOUSE_MOTION)
466  {
467    this->xMouse += event.xRel;
468    this->yMouse += event.yRel;
469  }
[8705]470  else if( event.type == KeyMapper::PEV_JUMP)
[9235]471  {
[9110]472    this->bJump = event.bPressed;
[9235]473  }
474  else if( event.type == KeyMapper::PEV_FIRE1)
475  {
476    this->bFire = event.bPressed;
477  }
[8685]478}
479
480
[9235]481void FPSPlayer::respawn( )
482{
483  if( State::isOnline())
484    toList( OM_PLAYERS );
[8685]485
[9235]486  this->damageTicker = 0.0f;
[8685]487
[9235]488  Playable::respawn();
489}
490
491
492void FPSPlayer::destroy( WorldEntity* killer )
493{
494  Playable::destroy( killer );
495
496  toList( OM_DEAD );
497}
498
Note: See TracBrowser for help on using the repository browser.