Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

less debug and better init

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