Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

only some renicement

File size: 12.1 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  this->addWeapon(wpRight,1, 0);
133  this->getWeaponManager().changeWeaponConfig(1);
134
135  this->getWeaponManager().setSlotCount(2);
136  this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_4*-0.55f, Vector(0,0,1)));
137  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
138  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
139  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
140  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
141
142//   this->getWeaponManager().getFixedTarget()->setRelDir(Quaternion(M_PI_4*-0.6f, Vector(0,0,1)));
143
144
145  this->getWeaponManager().setParentNode(&this->cameraNode);
146  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
147
148  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
149  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
150
151  this->aimingSystem = new AimingSystem(this);
152  //this->addChild(this->aimingSystem);
153  wpRight->addChild(this->aimingSystem);
154//   this->getWeaponManager().sl
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(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY);
168
169  this->initWeapon = false;
170  this->damageTicker = 0.0f;
171
172  if( State::isOnline())
173    toList( OM_PLAYERS );
174}
175
176
177/**
178 * loads the Settings of a FPSPlayer from an XML-element.
179 * @param root the XML-element to load the Spaceship's properties from
180 */
181void FPSPlayer::loadParams(const TiXmlElement* root)
182{
183  Playable::loadParams(root);
184}
185
186void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
187{
188  this->attitude = this->getAbsDir().getAttitude();
189  this->heading = this->getAbsDir().getHeading();
190}
191
192
193void FPSPlayer::reset()
194{
195  this->bLeft = false;
196  this->bRight = false;
197  this->bForward = false;
198  this->bBackward = false;
199  this->xMouse = 0.0f;
200  this->yMouse = 0.0f;
201
202  this->setHealth(80);
203}
204
205
206void FPSPlayer::enter()
207{
208  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
209
210  State::getCameraNode()->setParentSoft(&this->cameraNode);
211  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
212
213  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
214  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
215
216  if ( !State::isOnline() )
217  {
218    this->respawn();
219  }
220}
221
222void FPSPlayer::leave()
223{
224  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
225  this->detachCamera();
226}
227
228
229
230/**
231 *  the function called for each passing timeSnap
232 * @param time The timespan passed since last update
233 */
234void FPSPlayer::tick (float time)
235{
236
237  if ( !this->initWeapon )
238  {
239    this->initWeapon = true;
240
241    this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE);
242
243    this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
244    this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
245    this->getWeaponManager().getFixedTarget()->setParentMode(PNODE_ROTATE_AND_MOVE);
246
247
248    this->aimingSystem->toList(OM_GROUP_01);
249    this->aimingSystem->setParent(&this->cameraNode);
250//     this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE);
251    this->aimingSystem->setRelDir(Quaternion(M_PI_4*-0.58f, Vector(0,0,1)));
252    this->aimingSystem->setRelCoor(0, -1, -1);
253
254
255    AABB* box = this->getModelAABB();
256    if( box != NULL)
257    {
258      float f = 1.0;
259      this->cameraNode.setRelCoor(0, box->halfLength[1] * f, 0);
260//       this->cameraNode.setRelCoor(10, box->halfLength[1] * f, 0);
261
262      float v = 0.1f;
263      this->getWeaponManager().setSlotPosition(0, Vector(-8.0, box->halfLength[1] * v, 1.1));
264      this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * v, 0.0));
265    }
266  }
267
268
269  this->getWeaponManager().tick(time);
270  if( this->bFire)
271  {
272    this->getWeaponManager().fire();
273
274//     WorldEntity* target = this->aimingSystem->getNearestTarget();
275//     if( target != NULL)
276//     {
277//       PRINTF(0)("hit hit hit, got: %s\n", target->getClassName());
278//     }
279//     else
280//     {
281//       PRINTF(0)("nothing hit\n");
282//     }
283  }
284
285
286  //dealing damage
287
288  if ( State::isOnline() && SharedNetworkData::getInstance()->isGameServer() )
289  {
290    this->damageTicker -= time;
291
292    if ( this->damageTicker <= 0.0f && this->beFire() )
293    {
294      this->damageTicker = 0.25;
295
296      WorldEntity * victim = aimingSystem->getNearestTarget();
297
298      if ( victim )
299      {
300        PRINTF(0)("FIRE: hit %s\n", victim->getClassName());
301        victim->hit( 20, this );
302      }
303      else
304      {
305        PRINTF(0)("FIRE: nothing hit\n");
306      }
307    }
308  }
309
310
311  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
312  {
313    xMouse *= time ;
314    yMouse *= time ;
315
316    heading -= xMouse;
317    attitude-= yMouse;
318
319
320    if ( attitude > 1.95 )
321      attitude = 1.95;
322    else if ( attitude < -1.07 )
323      attitude = -1.07;
324
325    xMouse = yMouse = 0;
326  }
327
328  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
329  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
330
331  Vector velocity;
332
333  if ( this->bForward )
334  {
335    velocity += this->getAbsDirX();
336  }
337
338  if ( this->bBackward )
339  {
340    velocity -= this->getAbsDirX();
341  }
342
343  if ( this->bRight )
344  {
345    velocity += this->getAbsDirZ();
346  }
347
348  if ( this->bLeft )
349  {
350    velocity -= this->getAbsDirZ();
351  }
352
353
354  velocity *= 100;
355
356  if( this->bJump && likely(this->getModel(0) != NULL))
357  {
358    if( this->jumpForce < 1.0f)
359    {
360      this->jumpForce = 300.0f;
361
362      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
363        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
364    }
365  }
366  else if(velocity.len() != 0.0f)
367  {
368    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
369      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
370  }
371  else
372  {
373    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
374      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
375  }
376
377
378  velocity.y += this->jumpForce;
379  if( this->jumpForce > 1.0f)
380    this->jumpForce *= 0.9f;
381
382
383  // physical falling of the player
384  if( !this->isOnGround())
385  {
386    this->fallVelocity += 300.0f * time;
387    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
388
389//     PRINTF(0)("vel %f\n", this->fallVelocity);
390  }
391  else
392  {
393    this->fallVelocity = 0.0f;
394  }
395
396  this->shiftCoor( velocity*time );
397
398
399
400
401
402
403
404  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
405  {
406    ((InteractiveModel*)this->getModel(0))->tick(time);
407
408    // handle animations differently
409
410
411
412
413
414//     else if( this->bFire && likely(this->getModel(0) != NULL))
415//     {
416//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
417//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
418//     }
419//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
420//     {
421//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
422//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
423//     }
424//     else if (likely(this->getModel(0) != NULL))
425//     {
426//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
427//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
428//     }
429  }
430
431  this->setOnGround(false);
432  this->aimingSystem->flushList();
433}
434
435
436
437/**
438 *  draws the MD2Creature after transforming it.
439 */
440void FPSPlayer::draw () const
441{
442  // only draw if this entity is not the player since the player nevers sees himself
443  if( this->getCurrentPlayer() == NULL)
444    WorldEntity::draw();
445}
446
447
448
449/**
450 * process
451 */
452void FPSPlayer::process(const Event &event)
453{
454  Playable::process(event);
455
456  if( event.type == KeyMapper::PEV_LEFT)
457    this->bLeft = event.bPressed;
458  else if( event.type == KeyMapper::PEV_RIGHT)
459    this->bRight = event.bPressed;
460  else if( event.type == KeyMapper::PEV_FORWARD)
461    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
462  else if( event.type == KeyMapper::PEV_BACKWARD)
463    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
464  else if( event.type == EV_MOUSE_MOTION)
465  {
466    this->xMouse += event.xRel;
467    this->yMouse += event.yRel;
468  }
469  else if( event.type == KeyMapper::PEV_JUMP)
470  {
471    this->bJump = event.bPressed;
472  }
473  else if( event.type == KeyMapper::PEV_FIRE1)
474  {
475    this->bFire = event.bPressed;
476  }
477}
478
479
480void FPSPlayer::respawn( )
481{
482  if( State::isOnline())
483    toList( OM_PLAYERS );
484
485  this->damageTicker = 0.0f;
486
487  Playable::respawn();
488}
489
490
491void FPSPlayer::destroy( WorldEntity* killer )
492{
493  Playable::destroy( killer );
494
495  toList( OM_DEAD );
496}
497
Note: See TracBrowser for help on using the repository browser.