Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

different collision reactions for npcs and full walk ability

File size: 9.7 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"
[8776]31
[9110]32#include "aabb.h"
33
[8685]34#include "key_mapper.h"
35
[8687]36#include "debug.h"
[8685]37
[9110]38#include "shared_network_data.h"
[8685]39
[8687]40
[8731]41
[8685]42CREATE_FACTORY(FPSPlayer, CL_FPS_PLAYER);
43
[8894]44#include "script_class.h"
45CREATE_SCRIPTABLE_CLASS(FPSPlayer, CL_FPS_PLAYER,
46                        addMethod("setAbsCoor", ExecutorLua3<PNode,float,float,float>(&PNode::setAbsCoor))
47                            ->addMethod("getAbsCoorX", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorX))
48                            ->addMethod("getAbsCoorY", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorY))
49                            ->addMethod("getAbsCoorZ", ExecutorLua0ret<PNode, float>(&PNode::getAbsCoorZ))
50                       );
[8685]51
[8894]52
[8685]53/**
54 *  destructs the FPSPlayer, deletes alocated memory
55 */
56FPSPlayer::~FPSPlayer ()
57{
58  this->setPlayer(NULL);
59}
60
61
62/**
63 *  creates a new FPSPlayer from Xml Data
64 * @param root the xml element containing FPSPlayer data
65 *
66 */
67FPSPlayer::FPSPlayer(const TiXmlElement* root)
68{
69  this->init();
70
71  if (root != NULL)
72    this->loadParams(root);
73
74}
75
76
77/**
78 * initializes a FPSPlayer
79 */
80void FPSPlayer::init()
81{
82  this->setClassID(CL_FPS_PLAYER, "FPSPlayer");
83
84
85  this->bLeft = false;
86  this->bRight = false;
87  this->bForward = false;
88  this->bBackward = false;
89  this->bJump = false;
[9003]90  this->bPosBut = false;
[8731]91
[8685]92  this->xMouse = 0.0f;
93  this->yMouse = 0.0f;
94
95  this->setHealthMax(100);
96  this->setHealth(80);
97
[9003]98  this->fallVelocity = 0.0f;
[9110]99  this->jumpForce = 0.0f;
[8685]100
[8731]101  this->cameraNode.setParent(this);
102
103  this->attitude = this->getAbsDir().getAttitude();
104  this->heading = this->getAbsDir().getHeading();
105
[8685]106  //add events to the eventlist
107  registerEvent(KeyMapper::PEV_FORWARD);
108  registerEvent(KeyMapper::PEV_BACKWARD);
109  registerEvent(KeyMapper::PEV_LEFT);
110  registerEvent(KeyMapper::PEV_RIGHT);
111  registerEvent(KeyMapper::PEV_FIRE1);
[8687]112  registerEvent(KeyMapper::PEV_JUMP);
[8685]113  registerEvent(EV_MOUSE_MOTION);
114
115
116
[8776]117  // weapon manager for the fps
[8685]118  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
119
[8894]120  Weapon* wpRight = new FPSSniperRifle(0);
[8776]121  wpRight->setName("testGun Right");
[8894]122/*  Weapon* wpLeft = new TestGun(1);*/
[8776]123//   Weapon* wpLeft = new Turret();
[8894]124//   wpLeft->setName("testGun Left");
[8776]125
[8894]126//   this->addWeapon(wpLeft, 1, 0);
127  this->addWeapon(wpRight,1, 0);
[8776]128  this->getWeaponManager().changeWeaponConfig(1);
129
130  this->getWeaponManager().setSlotCount(2);
[9003]131//   this->getWeaponManager().setSlotDirection(0, Quaternion(M_PI_2, Vector(0,1,0)));
[8776]132  this->getWeaponManager().setSlotCapability(0, WTYPE_ALLDIRS | WTYPE_DIRECTIONAL);
[9110]133  this->getWeaponManager().setSlotDirection(1, Quaternion(M_PI_4*.5, Vector(1,0,0)));
134  this->getWeaponManager().setSlotPosition(0, Vector(1.5, -0.7, 1.1));
[9003]135  this->getWeaponManager().setSlotPosition(1, Vector(5.0, 0.0, 0.0));
[8776]136
[9110]137
[8844]138  this->getWeaponManager().setParentNode(&this->cameraNode);
[8846]139  this->cameraNode.addNodeFlags(PNODE_PROHIBIT_CHILD_DELETE);
140
[8776]141  this->getWeaponManager().getFixedTarget()->setParent(&this->cameraNode);
142  this->getWeaponManager().getFixedTarget()->setRelCoor(1000,0,0);
143
144
[8686]145  // network registration
[8685]146  registerVar( new SynchronizeableBool( &bLeft, &bLeft, "bLeft", PERMISSION_OWNER ) );
147  registerVar( new SynchronizeableBool( &bRight, &bRight, "bRight", PERMISSION_OWNER ) );
148  registerVar( new SynchronizeableBool( &bForward, &bForward, "bForward", PERMISSION_OWNER ) );
149  registerVar( new SynchronizeableBool( &bBackward, &bBackward, "bBackward", PERMISSION_OWNER ) );
[9110]150  registerVar( new SynchronizeableFloat( &heading, &heading, "heading", PERMISSION_OWNER ) );
151  registerVar( new SynchronizeableFloat( &attitude, &attitude, "attitude", PERMISSION_OWNER ) );
[9126]152
153    //subscribe to collision reaction
154  this->subscribeReaction(CREngine::CR_PHYSICS_FULL_WALK, CL_BSP_ENTITY);
[8685]155}
156
157
158/**
159 * loads the Settings of a FPSPlayer from an XML-element.
160 * @param root the XML-element to load the Spaceship's properties from
161 */
162void FPSPlayer::loadParams(const TiXmlElement* root)
163{
164  Playable::loadParams(root);
165}
166
167void FPSPlayer::setPlayDirection(const Quaternion& quat, float speed)
168{
[8731]169  this->attitude = this->getAbsDir().getAttitude();
170  this->heading = this->getAbsDir().getHeading();
[8685]171}
172
173
174void FPSPlayer::reset()
175{
176  this->bLeft = false;
177  this->bRight = false;
178  this->bForward = false;
179  this->bBackward = false;
180  this->xMouse = 0.0f;
181  this->yMouse = 0.0f;
182
183  this->setHealth(80);
184}
185
186
187void FPSPlayer::enter()
188{
[8731]189  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( true );
190
191  State::getCameraNode()->setParentSoft(&this->cameraNode);
192  State::getCameraTargetNode()->setParentSoft(&this->cameraNode);
193
194  this->getWeaponManager().getFixedTarget()->setParent(State::getCameraTargetNode());
195  this->getWeaponManager().getFixedTarget()->setRelCoor(0,0,0);
[9123]196
[8685]197}
198
199void FPSPlayer::leave()
200{
201  dynamic_cast<Element2D*>(this->getWeaponManager().getFixedTarget())->setVisibility( false);
202  this->detachCamera();
203}
204
205
206
207/**
208 *  the function called for each passing timeSnap
209 * @param time The timespan passed since last update
210 */
211void FPSPlayer::tick (float time)
212{
[9110]213
[9123]214  if( this->bJump)
[9003]215  {
[9110]216    printf("mechanic2:walkTo( %f, mtheight, %f)\n",this->getAbsCoorX(),this->getAbsCoorZ());
[9003]217  }
[9110]218
[8685]219  Playable::tick( time );
220
[9110]221  if( ( xMouse != 0 || yMouse != 0 ) && (this->getOwner() == SharedNetworkData::getInstance()->getHostID() || !State::isOnline() ) )
[8685]222  {
[8687]223    xMouse *= time ;
224    yMouse *= time ;
[8685]225
[8731]226    heading -= xMouse;
227    attitude-= yMouse;
[8685]228
[8731]229    if ( attitude > 2.05 )
230      attitude = 2.05;
[8685]231
[8731]232    else if ( attitude < -1.15 )
233      attitude = -1.15;
[8685]234
235    xMouse = yMouse = 0;
236  }
237
[9110]238  this->setAbsDir(Quaternion(heading, Vector(0,1,0)));
239  this->cameraNode.setRelDir(Quaternion( attitude, Vector( 0, 0, 1 ) ));
[8685]240
241  Vector velocity;
242
243  if ( this->bForward )
244  {
245    velocity += this->getAbsDirX();
246  }
247
248  if ( this->bBackward )
249  {
250    velocity -= this->getAbsDirX();
251  }
252
253  if ( this->bRight )
254  {
255    velocity += this->getAbsDirZ();
256  }
257
258  if ( this->bLeft )
259  {
260    velocity -= this->getAbsDirZ();
261  }
262
[9003]263
[8685]264  velocity *= 100;
265
[9110]266  if( this->bJump && likely(this->getModel(0) != NULL))
267  {
268    if( this->jumpForce < 1.0f)
269    {
270      this->jumpForce = 300.0f;
[9003]271
[9110]272      if( ((InteractiveModel*)this->getModel(0))->getAnimation() != JUMP)
273        ((InteractiveModel*)this->getModel(0))->setAnimation(JUMP);
274    }
275  }
276  else if(velocity.len() != 0.0f)
277  {
278    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != RUN)
279      ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
280  }
281  else
282  {
283    if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
284      ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
285  }
286
287
288  velocity.y += this->jumpForce;
289  if( this->jumpForce > 1.0f)
290    this->jumpForce *= 0.9f;
291
292
[9003]293  // physical falling of the player
294  if( !this->isOnGround())
295  {
296    this->fallVelocity += 300.0f * time;
297    velocity -= Vector(0.0, 1.0, 0.0) * this->fallVelocity;
298  }
299  else
300  {
301    this->fallVelocity = 0.0f;
302  }
303
304
[8685]305  this->shiftCoor( velocity*time );
306
307
308
309
[9003]310
311
312
[8693]313  if( likely(this->getModel(0) != NULL) && this->getModel(0)->isA(CL_INTERACTIVE_MODEL))
314  {
315    ((InteractiveModel*)this->getModel(0))->tick(time);
[9110]316
317    // handle animations differently
318
319
320
321
322
[8685]323//     else if( this->bFire && likely(this->getModel(0) != NULL))
324//     {
325//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != ATTACK)
326//         ((InteractiveModel*)this->getModel(0))->setAnimation(ATTACK);
327//     }
328//     else if( fabs(move.len()) > 0.0f && likely(this->getModel(0) != NULL))
329//     {
330//       if( ((InteractiveModel*)this->getModel(0))->getAnim() != RUN)
331//         ((InteractiveModel*)this->getModel(0))->setAnimation(RUN);
332//     }
[8687]333//     else if (likely(this->getModel(0) != NULL))
334//     {
335//       if( ((InteractiveModel*)this->getModel(0))->getAnimation() != STAND)
336//         ((InteractiveModel*)this->getModel(0))->setAnimation(STAND);
337//     }
[8693]338  }
[8685]339
340}
341
342
343
[8686]344/**
345 *  draws the MD2Creature after transforming it.
346 */
347void FPSPlayer::draw () const
348{
349  // only draw if this entity is not the player since the player nevers sees himself
[8699]350  if( this->getCurrentPlayer() == NULL)
[8686]351    WorldEntity::draw();
352}
[8685]353
354
355
356/**
[8686]357 * process
[8685]358 */
359void FPSPlayer::process(const Event &event)
360{
361  Playable::process(event);
362
363  if( event.type == KeyMapper::PEV_LEFT)
364    this->bLeft = event.bPressed;
365  else if( event.type == KeyMapper::PEV_RIGHT)
366    this->bRight = event.bPressed;
367  else if( event.type == KeyMapper::PEV_FORWARD)
368    this->bForward = event.bPressed; //this->shiftCoor(0,.1,0);
369  else if( event.type == KeyMapper::PEV_BACKWARD)
370    this->bBackward = event.bPressed; //this->shiftCoor(0,-.1,0);
371  else if( event.type == EV_MOUSE_MOTION)
372  {
373    this->xMouse += event.xRel;
374    this->yMouse += event.yRel;
375  }
[8705]376  else if( event.type == KeyMapper::PEV_JUMP)
[9110]377    this->bJump = event.bPressed;
[8685]378}
379
[9122]380void FPSPlayer::respawn( )
381{
382  Playable::respawn();
[9123]383
[9122]384  AABB* box = this->getModelAABB();
385  if( box != NULL)
386  {
387    float f = 1.3f;
388    State::getCameraNode()->setRelCoor(0, box->halfLength[1] * f, 0);
389    State::getCameraTargetNode()->setRelCoor(10, box->halfLength[1] * f, 0);
[8685]390
[9122]391    this->getWeaponManager().setSlotPosition(0, Vector(1.5, box->halfLength[1] * f - 0.7, 1.1));
392    this->getWeaponManager().setSlotPosition(1, Vector(5.0, box->halfLength[1] * f, 0.0));
393  }
394}
[8685]395
396
[9122]397
398
Note: See TracBrowser for help on using the repository browser.