Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

muchos debugos inofromationos

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