Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/creatures/fps_player.cc @ 10152

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

merged network back to trunk

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