Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

bugfix

File size: 11.6 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"
[9163]31#include "weapons/aiming_system.h"
[8776]32
[9110]33#include "aabb.h"
34
[9166]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);
[9191]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
90  this->bLeft = false;
91  this->bRight = false;
92  this->bForward = false;
93  this->bBackward = false;
94  this->bJump = false;
[9003]95  this->bPosBut = false;
[9166]96  this->bFire = false;
[8731]97
[8685]98  this->xMouse = 0.0f;
99  this->yMouse = 0.0f;
100
101  this->setHealthMax(100);
102  this->setHealth(80);
103
[9003]104  this->fallVelocity = 0.0f;
[9110]105  this->jumpForce = 0.0f;
[8685]106
[8731]107  this->cameraNode.setParent(this);
108
109  this->attitude = this->getAbsDir().getAttitude();
110  this->heading = this->getAbsDir().getHeading();
111
[8685]112  //add events to the eventlist
113  registerEvent(KeyMapper::PEV_FORWARD);
114  registerEvent(KeyMapper::PEV_BACKWARD);
115  registerEvent(KeyMapper::PEV_LEFT);
116  registerEvent(KeyMapper::PEV_RIGHT);
117  registerEvent(KeyMapper::PEV_FIRE1);
[8687]118  registerEvent(KeyMapper::PEV_JUMP);
[8685]119  registerEvent(EV_MOUSE_MOTION);
120
121
122
[8776]123  // weapon manager for the fps
[8685]124  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
125
[8894]126  Weapon* wpRight = new FPSSniperRifle(0);
[8776]127  wpRight->setName("testGun Right");
[8894]128/*  Weapon* wpLeft = new TestGun(1);*/
[8776]129//   Weapon* wpLeft = new Turret();
[8894]130//   wpLeft->setName("testGun Left");
[8776]131
[8894]132//   this->addWeapon(wpLeft, 1, 0);
133  this->addWeapon(wpRight,1, 0);
[8776]134  this->getWeaponManager().changeWeaponConfig(1);
135
136  this->getWeaponManager().setSlotCount(2);
[9207]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
[9207]143//   this->getWeaponManager().getFixedTarget()->setRelDir(Quaternion(M_PI_4*-0.6f, Vector(0,0,1)));
[9110]144
[9207]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
[9168]152  this->aimingSystem = new AimingSystem(this);
[9207]153  //this->addChild(this->aimingSystem);
154  wpRight->addChild(this->aimingSystem);
155//   this->getWeaponManager().sl
[8776]156
[9166]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 ) );
[9139]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 ) );
[9126]166
167    //subscribe to collision reaction
168  this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY);
[9148]169
[9139]170  this->initWeapon = false;
[8685]171}
172
173
174/**
175 * loads the Settings of a FPSPlayer from an XML-element.
176 * @param root the XML-element to load the Spaceship's properties from
177 */
178void FPSPlayer::loadParams(const TiXmlElement* root)
179{
180  Playable::loadParams(root);
181}
182
183void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
184{
[8731]185  this->attitude = this->getAbsDir().getAttitude();
186  this->heading = this->getAbsDir().getHeading();
[8685]187}
188
189
190void FPSPlayer::reset()
191{
192  this->bLeft = false;
193  this->bRight = false;
194  this->bForward = false;
195  this->bBackward = false;
196  this->xMouse = 0.0f;
197  this->yMouse = 0.0f;
198
199  this->setHealth(80);
200}
201
202
203void FPSPlayer::enter()
204{
[8731]205  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
206
207  State::getCameraNode()->setParentSoft(&this->cameraNode);
208  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
209
210  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
211  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
[9123]212
[9136]213  if ( !State::isOnline() )
214  {
215    this->respawn();
216  }
[8685]217}
218
219void FPSPlayer::leave()
220{
221  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
222  this->detachCamera();
223}
224
225
226
227/**
228 *  the function called for each passing timeSnap
229 * @param time The timespan passed since last update
230 */
231void FPSPlayer::tick (float time)
232{
[9148]233
[9139]234  if ( !this->initWeapon )
235  {
236    this->initWeapon = true;
[9148]237
[9207]238    this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE);
[9194]239
240    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
241    this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
242    this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
243
244
245    this->aimingSystem->toList(OM_GROUP_01);
246    this->aimingSystem->setParent(&this->cameraNode);
[9207]247//     this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE);
[9211]248    this->aimingSystem->setRelDir(Quaternion(M_PI_4*-0.58f, Vector(0,0,1)));
[9207]249    this->aimingSystem->setRelCoor(0, -1, -1);
[9194]250
251
[9139]252    AABB* box = this->getModelAABB();
253    if( box != NULL)
254    {
[9209]255      float f = 1.0;
[9207]256      this->cameraNode.setRelCoor(0, box->halfLength[1] * f, 0);
[9211]257//       this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
[9110]258
[9211]259      float v = 0.1f;
260      this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
261      this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
[9139]262    }
263  }
264
[9123]265  if( this->bJump)
[9003]266  {
[9110]267    printf("mechanic2:walkTo( %f, mtheight, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ());
[9003]268  }
[9110]269
[9151]270
[9166]271  this->getWeaponManager().tick(time);
272  if( this->bFire)
273  {
[9207]274    this->getWeaponManager().fire();
[9151]275
[9166]276    WorldEntity* target = this->aimingSystem->getNearestTarget();
277    if( target != NULL)
278    {
279      PRINTF(0)("hit hit hit, got: %s\n", target->getClassName());
280    }
281    else
282    {
283      PRINTF(0)("nothing hit\n");
284    }
285  }
[9151]286
[8685]287
[9166]288
289
[9110]290  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
[8685]291  {
[8687]292    xMouse *= time ;
293    yMouse *= time ;
[8685]294
[8731]295    heading -= xMouse;
296    attitude-= yMouse;
[8685]297
298
[9207]299    if ( attitude > 1.95 )
300      attitude = 1.95;
301    else if ( attitude < -1.07 )
302      attitude = -1.07;
[8685]303
304    xMouse = yMouse = 0;
305  }
306
[9110]307  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
308  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
[8685]309
310  Vector velocity;
311
312  if ( this->bForward )
313  {
314    velocity += this->getAbsDirX();
315  }
316
317  if ( this->bBackward )
318  {
319    velocity -= this->getAbsDirX();
320  }
321
322  if ( this->bRight )
323  {
324    velocity += this->getAbsDirZ();
325  }
326
327  if ( this->bLeft )
328  {
329    velocity -= this->getAbsDirZ();
330  }
331
[9003]332
[8685]333  velocity *= 100;
334
[9110]335  if( this->bJump && likely(this->getModel(0) != NULL))
336  {
337    if( this->jumpForce < 1.0f)
338    {
339      this->jumpForce = 300.0f;
[9003]340
[9110]341      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
342        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
343    }
344  }
345  else if(velocity.len() != 0.0f)
346  {
347    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
348      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
349  }
350  else
351  {
352    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
353      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
354  }
355
356
357  velocity.y += this->jumpForce;
358  if( this->jumpForce > 1.0f)
359    this->jumpForce *= 0.9f;
360
361
[9003]362  // physical falling of the player
363  if( !this->isOnGround())
364  {
365    this->fallVelocity += 300.0f * time;
366    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
[9211]367
[9212]368    PRINTF(0)("vel %f\n", this->fallVelocity);
[9003]369  }
370  else
371  {
372    this->fallVelocity = 0.0f;
373  }
374
[8685]375  this->shiftCoor( velocity*time );
376
377
378
379
[9003]380
381
382
[8693]383  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
384  {
385    ((InteractiveModel*)this->getModel(0))->tick(time);
[9110]386
387    // handle animations differently
388
389
390
391
392
[8685]393//     else if( this->bFire && likely(this->getModel(0) != NULL))
394//     {
395//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
396//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
397//     }
398//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
399//     {
400//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
401//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
402//     }
[8687]403//     else if (likely(this->getModel(0) != NULL))
404//     {
405//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
406//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
407//     }
[8693]408  }
[8685]409
[9148]410  this->setOnGround(false);
[9172]411  this->aimingSystem->flushList();
[8685]412}
413
414
415
[8686]416/**
417 *  draws the MD2Creature after transforming it.
418 */
419void FPSPlayer::draw () const
420{
421  // only draw if this entity is not the player since the player nevers sees himself
[8699]422  if( this->getCurrentPlayer() == NULL)
[8686]423    WorldEntity::draw();
424}
[8685]425
426
427
428/**
[8686]429 * process
[8685]430 */
431void FPSPlayer::process(const Event &event)
432{
433  Playable::process(event);
434
435  if( event.type == KeyMapper::PEV_LEFT)
436    this->bLeft = event.bPressed;
437  else if( event.type == KeyMapper::PEV_RIGHT)
438    this->bRight = event.bPressed;
439  else if( event.type == KeyMapper::PEV_FORWARD)
440    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
441  else if( event.type == KeyMapper::PEV_BACKWARD)
442    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
443  else if( event.type == EV_MOUSE_MOTION)
444  {
445    this->xMouse += event.xRel;
446    this->yMouse += event.yRel;
447  }
[8705]448  else if( event.type == KeyMapper::PEV_JUMP)
[9166]449  {
[9110]450    this->bJump = event.bPressed;
[9166]451  }
452  else if( event.type == KeyMapper::PEV_FIRE1)
453  {
454    this->bFire = event.bPressed;
455  }
[8685]456}
457
[9158]458
[9122]459void FPSPlayer::respawn( )
460{
[9151]461  if( State::isOnline())
462    toList( OM_PLAYERS );
[9122]463}
[8685]464
[9162]465void FPSPlayer::destroy( WorldEntity* killer )
466{
467  Playable::destroy( killer );
[9166]468
[9162]469  toList( OM_DEAD );
470}
471
Note: See TracBrowser for help on using the repository browser.