Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/scriptchanges/src/world_entities/creatures/fps_player.cc @ 10425

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

silvans complete patch

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