Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

better cleanup and stuff

File size: 11.2 KB
RevLine 
[8685]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"
[8731]20#include "state.h"
[8685]21
22#include "src/lib/util/loading/factory.h"
23
[9110]24#include "md2/md2Model.h"
25
[8776]26#include "weapons/weapon_manager.h"
27#include "weapons/test_gun.h"
28#include "weapons/turret.h"
29#include "weapons/cannon.h"
[8894]30#include "weapons/fps_sniper_rifle.h"
[9163]31#include "weapons/aiming_system.h"
[8776]32
[9110]33#include "aabb.h"
34
[9166]35
[8685]36#include "key_mapper.h"
37
[8687]38#include "debug.h"
[8685]39
[9110]40#include "shared_network_data.h"
[8685]41
[8687]42
[8731]43
[8685]44CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
45
[8894]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                       );
[8685]53
[8894]54
[8685]55/**
56 *  destructs the FPSPlayer, deletes alocated memory
57 */
58FPSPlayer::~FPSPlayer ()
59{
60  this->setPlayer(NULL);
[9191]61
62  if( this->aimingSystem)
63    delete this->aimingSystem;
[8685]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;
[9003]95  this->bPosBut = false;
[9166]96  this->bFire = false;
[8731]97
[8685]98  this->xMouse = 0.0f;
99  this->yMouse = 0.0f;
100
101  this->setHealthMax(100);
102  this->setHealth(80);
103
[9003]104  this->fallVelocity = 0.0f;
[9110]105  this->jumpForce = 0.0f;
[8685]106
[8731]107  this->cameraNode.setParent(this);
108
109  this->attitude = this->getAbsDir().getAttitude();
110  this->heading = this->getAbsDir().getHeading();
111
[8685]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);
[8687]118  registerEvent(KeyMapper::PEV_JUMP);
[8685]119  registerEvent(EV_MOUSE_MOTION);
120
121
122
[8776]123  // weapon manager for the fps
[8685]124  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
125
[8894]126  Weapon* wpRight = new FPSSniperRifle(0);
[8776]127  wpRight->setName("testGun Right");
[8894]128/*  Weapon* wpLeft = new TestGun(1);*/
[8776]129//   Weapon* wpLeft = new Turret();
[8894]130//   wpLeft->setName("testGun Left");
[8776]131
[8894]132//   this->addWeapon(wpLeft, 1, 0);
133  this->addWeapon(wpRight,1, 0);
[8776]134  this->getWeaponManager().changeWeaponConfig(1);
135
136  this->getWeaponManager().setSlotCount(2);
[9003]137//   this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0)));
[8776]138  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[9110]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));
[9003]141  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
[8776]142
[9110]143
[8844]144  this->getWeaponManager().setParentNode(&this->cameraNode);
[8846]145  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
146
[8776]147  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
148  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
149
[9168]150  this->aimingSystem = new AimingSystem(this);
[9166]151  this->addChild(this->aimingSystem);
[8776]152
[9166]153
[9168]154
[8686]155  // network registration
[8685]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 ) );
[9139]160  registerVar( new SynchronizeableBool( &bJump, &bJump, "bJump", PERMISSION_OWNER ) );
[9110]161  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
162  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
[9126]163
164    //subscribe to collision reaction
165  this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY);
[9148]166
[9139]167  this->initWeapon = false;
[8685]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{
[8731]182  this->attitude = this->getAbsDir().getAttitude();
183  this->heading = this->getAbsDir().getHeading();
[8685]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{
[8731]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);
[9123]209
[9136]210  if ( !State::isOnline() )
211  {
212    this->respawn();
213  }
[8685]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{
[9148]230
[9139]231  if ( !this->initWeapon )
232  {
233    this->initWeapon = true;
[9148]234
[9139]235    AABB* box = this->getModelAABB();
236    if( box != NULL)
237    {
238      float f = 1.3f;
239      State::getCameraNode()->setRelCoor(0, box->halfLength[1] * f, 0);
240      State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * f, 0);
[9110]241
[9158]242      State::getCameraNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
[9186]243      this->cameraNode.setParentMode(PNODE_ROTATE_AND_MOVE);
244      this->getWeaponManager().getParentNode()->setParentMode(PNODE_ROTATE_AND_MOVE);
[9158]245
246
[9139]247      this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * f - 0.7, 1.1));
248      this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * f, 0.0));
[9168]249
[9189]250      this->aimingSystem->toList(OM_LIST(this->getOMListNumber() + 1) /*OM_GROUP_01*/);
[9190]251      this->aimingSystem->setParent(&this->cameraNode);
[9189]252      this->aimingSystem->setParentMode(PNODE_ROTATE_AND_MOVE);
[9139]253    }
254  }
255
[9123]256  if( this->bJump)
[9003]257  {
[9110]258    printf("mechanic2:walkTo( %f, mtheight, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ());
[9003]259  }
[9110]260
[9151]261
[9166]262  this->getWeaponManager().tick(time);
263  if( this->bFire)
264  {
[9188]265    //this->getWeaponManager().fire();
[9151]266
[9166]267    WorldEntity* target = this->aimingSystem->getNearestTarget();
268    if( target != NULL)
269    {
270      PRINTF(0)("hit hit hit, got: %s\n", target->getClassName());
271    }
272    else
273    {
274      PRINTF(0)("nothing hit\n");
275    }
276  }
[9151]277
[8685]278
[9166]279
280
[9110]281  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
[8685]282  {
[8687]283    xMouse *= time ;
284    yMouse *= time ;
[8685]285
[8731]286    heading -= xMouse;
287    attitude-= yMouse;
[8685]288
289
[9158]290    if ( attitude > 0.32 )
291      attitude = 0.32;
[8685]292
[9158]293    else if ( attitude < -2.1 )
294      attitude = -2.1;
295
[8685]296    xMouse = yMouse = 0;
297  }
298
[9110]299  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
300  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
[8685]301
302  Vector velocity;
303
304  if ( this->bForward )
305  {
306    velocity += this->getAbsDirX();
307  }
308
309  if ( this->bBackward )
310  {
311    velocity -= this->getAbsDirX();
312  }
313
314  if ( this->bRight )
315  {
316    velocity += this->getAbsDirZ();
317  }
318
319  if ( this->bLeft )
320  {
321    velocity -= this->getAbsDirZ();
322  }
323
[9003]324
[8685]325  velocity *= 100;
326
[9110]327  if( this->bJump && likely(this->getModel(0) != NULL))
328  {
329    if( this->jumpForce < 1.0f)
330    {
331      this->jumpForce = 300.0f;
[9003]332
[9110]333      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
334        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
335    }
336  }
337  else if(velocity.len() != 0.0f)
338  {
339    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
340      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
341  }
342  else
343  {
344    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
345      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
346  }
347
348
349  velocity.y += this->jumpForce;
350  if( this->jumpForce > 1.0f)
351    this->jumpForce *= 0.9f;
352
353
[9003]354  // physical falling of the player
355  if( !this->isOnGround())
356  {
357    this->fallVelocity += 300.0f * time;
358    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
359  }
360  else
361  {
362    this->fallVelocity = 0.0f;
363  }
364
[8685]365  this->shiftCoor( velocity*time );
366
367
368
369
[9003]370
371
372
[8693]373  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
374  {
375    ((InteractiveModel*)this->getModel(0))->tick(time);
[9110]376
377    // handle animations differently
378
379
380
381
382
[8685]383//     else if( this->bFire && likely(this->getModel(0) != NULL))
384//     {
385//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
386//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
387//     }
388//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
389//     {
390//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
391//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
392//     }
[8687]393//     else if (likely(this->getModel(0) != NULL))
394//     {
395//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
396//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
397//     }
[8693]398  }
[8685]399
[9148]400  this->setOnGround(false);
[9172]401  this->aimingSystem->flushList();
[8685]402}
403
404
405
[8686]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
[8699]412  if( this->getCurrentPlayer() == NULL)
[8686]413    WorldEntity::draw();
414}
[8685]415
416
417
418/**
[8686]419 * process
[8685]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  }
[8705]438  else if( event.type == KeyMapper::PEV_JUMP)
[9166]439  {
[9110]440    this->bJump = event.bPressed;
[9166]441  }
442  else if( event.type == KeyMapper::PEV_FIRE1)
443  {
444    this->bFire = event.bPressed;
445  }
[8685]446}
447
[9158]448
[9122]449void FPSPlayer::respawn( )
450{
[9151]451  if( State::isOnline())
452    toList( OM_PLAYERS );
[9122]453}
[8685]454
[9162]455void FPSPlayer::destroy( WorldEntity* killer )
456{
457  Playable::destroy( killer );
[9166]458
[9162]459  toList( OM_DEAD );
460}
461
Note: See TracBrowser for help on using the repository browser.