Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain.old/src/world_entities/creatures/fps_player.cc @ 10637

Last change on this file since 10637 was 9406, checked in by bensch, 18 years ago

orxonox/trunk: merged the proxy back

merged with commandsvn merge -r9346:HEAD https://svn.orxonox.net/orxonox/branches/proxy .

no conflicts

File size: 12.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
22#include "src/lib/util/loading/factory.h"
23
24#include "md2/md2Model.h"
25
26#include "weapons/weapon_manager.h"
27#include "weapons/test_gun.h"
28#include "weapons/turret.h"
29#include "weapons/cannon.h"
30#include "weapons/fps_sniper_rifle.h"
31#include "weapons/aiming_system.h"
32
33#include "aabb.h"
34
35
36#include "key_mapper.h"
37
38#include "debug.h"
39
40#include "shared_network_data.h"
41
42
43
44CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
45
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                       );
53
54
55/**
56 *  destructs the FPSPlayer, deletes alocated memory
57 */
58FPSPlayer::~FPSPlayer ()
59{
60  this->setPlayer(NULL);
61
62  if( this->aimingSystem)
63    delete this->aimingSystem;
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;
94  this->bPosBut = false;
95  this->bFire = false;
96
97  this->xMouse = 0.0f;
98  this->yMouse = 0.0f;
99
100  this->setHealthMax(100);
101  this->setHealth(80);
102
103  this->fallVelocity = 0.0f;
104  this->jumpForce = 0.0f;
105
106  this->cameraNode.setParent(this);
107
108  this->attitude = this->getAbsDir().getAttitude();
109  this->heading = this->getAbsDir().getHeading();
110
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);
117  registerEvent(KeyMapper::PEV_JUMP);
118  registerEvent(EV_MOUSE_MOTION);
119
120
121
122  // weapon manager for the fps
123  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
124
125  Weapon* wpRight = new FPSSniperRifle(0);
126  wpRight->setName("testGun Right");
127/*  Weapon* wpLeft = new TestGun(1);*/
128//   Weapon* wpLeft = new Turret();
129//   wpLeft->setName("testGun Left");
130
131//   this->addWeapon(wpLeft, 1, 0);
132  if( State::isOnline())
133    this->addWeapon(wpRight,1, 0);
134  this->getWeaponManager().changeWeaponConfig(1);
135
136  this->getWeaponManager().setSlotCount(2);
137  this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1)));
138  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
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));
141  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
142
143//   this->getWeaponManager().getFixedTarget()->setRelDir(Quaternion(M_PI_4*-0.6f, Vector(0,0,1)));
144
145
146  this->getWeaponManager().setParentNode(&this->cameraNode);
147  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
148
149  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
150  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
151
152  this->aimingSystem = new AimingSystem(this);
153  //this->addChild(this->aimingSystem);
154  wpRight->addChild(this->aimingSystem);
155//   this->getWeaponManager().sl
156
157
158  // network registration
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 ) );
163  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
164  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
165  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
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 );
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{
189  this->attitude = this->getAbsDir().getAttitude();
190  this->heading = this->getAbsDir().getHeading();
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{
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
217  if ( !State::isOnline() )
218  {
219    this->respawn();
220  }
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{
237
238  if ( !this->initWeapon )
239  {
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    }
267  }
268
269
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//     {
278//       PRINTF(0)("hit hit hit, got: %s\n", target->getClassCName());
279//     }
280//     else
281//     {
282//       PRINTF(0)("nothing hit\n");
283//     }
284  }
285
286
287  //dealing damage
288
289  if ( State::isOnline() && (SharedNetworkData::getInstance()->isMasterServer() || SharedNetworkData::getInstance()->isProxyServer()))
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      {
301        PRINTF(0)("FIRE: hit %s\n", victim->getClassCName());
302        victim->hit( 20, this );
303      }
304      else
305      {
306        PRINTF(0)("FIRE: nothing hit\n");
307      }
308    }
309  }
310
311
312  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
313  {
314    xMouse *= time ;
315    yMouse *= time ;
316
317    heading -= xMouse;
318    attitude-= yMouse;
319
320
321    if ( attitude > 1.95 )
322      attitude = 1.95;
323    else if ( attitude < -1.07 )
324      attitude = -1.07;
325
326    xMouse = yMouse = 0;
327  }
328
329  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
330  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
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
354
355  velocity *= 100;
356
357  if( this->bJump && likely(this->getModel(0) != NULL))
358  {
359    if( this->jumpForce < 1.0f)
360    {
361      this->jumpForce = 300.0f;
362
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
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;
389
390//     PRINTF(0)("vel %f\n", this->fallVelocity);
391  }
392  else
393  {
394    this->fallVelocity = 0.0f;
395  }
396
397  this->shiftCoor( velocity*time );
398
399
400
401
402
403
404
405  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
406  {
407    ((InteractiveModel*)this->getModel(0))->tick(time);
408
409    // handle animations differently
410
411
412
413
414
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//     }
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//     }
430  }
431
432  this->setOnGround(false);
433  this->aimingSystem->flushList();
434}
435
436
437
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
444  if( this->getCurrentPlayer() == NULL)
445    WorldEntity::draw();
446}
447
448
449
450/**
451 * process
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  }
470  else if( event.type == KeyMapper::PEV_JUMP)
471  {
472    this->bJump = event.bPressed;
473  }
474  else if( event.type == KeyMapper::PEV_FIRE1)
475  {
476    this->bFire = event.bPressed;
477  }
478}
479
480
481void FPSPlayer::respawn( )
482{
483  if( State::isOnline())
484    toList( OM_PLAYERS );
485
486  this->damageTicker = 0.0f;
487
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.